-
getline() problem
I've been having problems with getline lately everytime i use getline it just skips over it...
Code:
CODE:
string name;
int age;
string About;
cout << "Your name: ";
getline(cine, name, '\n');
cout << "Age: ";
cin >> age;
cout << "About Yourself: ";
getline(cin, About, '\n');
OUTPUT:
Your name: Martin Bakiev
Age: 14
About Yourself: Press any key to continue...
Whats wrong with it!
-
-
>>Whats wrong with it!
Nothing. It's acting like a good getline() should. The "problem" is calling >> before you call getline(). The call to >> frequently leaves a newline char in the input buffer when it's done, which is what it is supposed to do, too, by the way. The newline char is frequently the terminating char for getline(). Thus after a call to >> when you call getline() the first char it sees in the input buffer is newline, so it "skips" over the variable and the program continues on. You can outflank this irritating combination in several ways, but the easiest is to call ignore() after >> and before getline(). We can debate the best set of parmaters to use in ignore, but that's the most common "fix". If you search the boards you'll find this problem discussed often.
-
Hey it happened to me too!.
When I mixed up cin>> and cin.getline() in the same program....
-
getline(cine, name, '\n'); shouldent cine be cin
-