Getline problems in C + +

Source: Internet
Author: User

When using C + + 's getline function, two problems are encountered, summarized as follows:


1. Sometimes we will find Getline (CIN,STR) When we write the program, such statements are not executed, but are skipped directly,
The general solution is to getline a sentence before adding cin.ignore () or a sentence cin.get (), or repeating geiline

Explain:

Check carefully, before you write Getline () function, must have entered the ENTER key, whether you enter a character, number or return, space, getline () function are received
and cin>> This input method is to ignore the carriage return, if you in Getline () before the number of CIN, the carriage return was ignored by CIN, but was received by the Getline function, feeling that this statement was skipped So the solution is to use getline once before the Getline function, the carriage return in the input stream is received, and then the normal input

For example, this piece of code
int a
cout<< "Please enter a" <<endl;
cin>>a;
Char *ch;
A=new Char[a];
cout<< "Please input string ch" <<endl;
Cin.getline (ch,100);

You will find Cin.getline (ch,100) and do not perform as cin>>a; after you have a return key stuck in the buffer, when using Cin.getline (a,100); Read the carriage returns, I think the input is over, so, It didn't seem to work.
Should be
cin>>a;
Cin.get (); Add this sentence.


2.while (Getline (cin,line)) How can not jump out of the loop

In C++premer, the second section of the standard string type is "reading entire lines of text with Getline". The procedure given in the book is as follows:
int main ()
{
String line:
while (Getline (Cin,line))
cout<<line<<endl;
return 0;
}
You'll see how the runtime doesn't jump out of the loop,

Let's analyze the while (Getline (cin,line)) statement

Note that this default carriage return stops reading, press CTRL + Z or type EOF enter to exit the loop.

In this statement, first getline reads the characters from the standard input device, and then returns to the input stream cin, looking at the prototype, the return type is IStream, the corresponding is CIN in this statement, so the true judgment object of the While judgment statement is CIN, is to determine whether there is currently a valid input stream. In this case, I think as long as your computer is not poisoned not insane how can your input stream not be effective? So in this case no matter how you enter the loop, because your input stream is valid, cannot jump out of the loop.
However, some students mistakenly think that while the judgment of the sentence is line (that is, line is empty), and then want to go through the direct carriage (that is, enter an empty line) out of the loop, but found how also can not jump out of the loop. This is because your return will only terminate the read-in operation of the Getline () function. The Getline () function terminates with a while () judgment (that is, if the input stream is valid, your input stream is of course valid and satisfies the condition), so the Getline () function is run. This is the cycle of time and time again ~ ~ ~

Getline problems in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.