By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

A big problem with getline

P: 51
Hi, I am trying to read two string, one after another, and before I read the second one I clean the buffer using cout.flush(), but I don't know why the program does not read the second string, not even the the third value using cin, Please I need your help. . . . . . this is the program:

#include<stdio.h>
#include <cctype>
#include <iostream>
#include <string>
using std::string;
using namespace std;



int main(){
int x;
string a,b;

cout << "Enter your first name: " << endl;
cout.flush();
getline(cin,a);

cout << "Enter your surname: " << endl;
cout.flush();
getline(cin,b);

cout << "Enter your age: " << endl;
cout.flush();
cin >> x;
return 0;

}

Thanks for your help. . . . . . .
Sep 21 '06 #1
Share this Question
Share on Google+
11 Replies


100+
P: 144
I can't reproduce your problem. This code runs fine for me.
Sep 21 '06 #2

100+
P: 144
Something you may try is putting a "cin.clear()" after each getline. This would address the possiblity that the newline character is being left in the cin buffer.
Sep 21 '06 #3

P: 51
Hi!!!!!!! the code is not working properly, I did the modifications that you have told me and I Compile it using Microsoft Visual c++, and it does not work well, here is the code:

#include<stdio.h>
#include <cctype>
#include <iostream>
#include <string>
using std::string;
using namespace std;



int main(){
int x;
string a,b;

cout << "Enter your first name: " << endl;
cout.flush();
getline(cin,a);
cin.clear();

cout << "Enter your surname: " << endl;
cout.flush();
getline(cin,b);
cin.clear();

cout << "Enter your age: " << endl;
cout.flush();
cin >> x;

cout << "Your Name is: " << a << endl;
cout << "Your Surname is: " << b << endl;
cout << "Your age is: " << x << endl;


return 0;

}

And this is the output:

Enter your first name:
Edwin

Enter your surname:
Alvear
Enter your age:
Your Name is: Edwin
Your Surname is:
Your age is: -858993460
Press any key to continue

Thanks for your help. . . .
Sep 21 '06 #4

100+
P: 144
Sorry, I can't be of more help, but I'm a Linux guy. I tend to try and stay away from the iostream library. The implementation and behavior tends to vary from platform to platform. The fact that it runs for me under the GNU compiler, but not for you is a good example of this problem.
Sep 21 '06 #5

100+
P: 144
One last thought. Try using "cin.getline(a, MAX_LENGTH)" instead of "getline(cin, a)."

Expand|Select|Wrap|Line Numbers
  1. #define MAX_LENGTH 24   /* Arbitrary number I just picked */
  2.  
  3. int main(int argc, char **argv)
  4. {
  5. ...
  6. cout << "Enter your first name" << endl;
  7. cout.flush();
  8. cin.getline(a, MAX_LENGTH);
  9. ...
  10. }
  11.  
Sep 21 '06 #6

P: 51
Hi again, but if use cin.getline(a, MAX_LENGTH); how do I know the maximun length of the text?, becuause the user must enter a text of any length and the program must be able to read it, So how can I manage this?
Sep 21 '06 #7

100+
P: 144
Hi again, but if use cin.getline(a, MAX_LENGTH); how do I know the maximun length of the text?, becuause the user must enter a text of any length and the program must be able to read it, So how can I manage this?
I forgot that cin.getline actually expects a char* and not a string. As far as the max length is concerned you would normally pick a number that you expect to be bigger then what the user would possibly input.

One final thing you can try is the following "getline(cin, a, '\n')", or "cin >> a".
Sep 21 '06 #8

P: 51
Well, I tried those options that you told me, but it stills the same, if I use Cin, it is not possible to read the Space character, and I need to read it, and I have to use strings, because later will do some operation with this strings (is easier for me), so is there another way to read an string in this program? Thanks. . . . . . . . .
Sep 21 '06 #9

100+
P: 293
D_C
I think you should be flushing cin instead of cout. That's where the extra "\n" character should be. Replace all cout.flush() with cin.flush() and see what happens. You should be able to remove each cin.clear() then.
Sep 21 '06 #10

100+
P: 144
I think you should be flushing cin instead of cout. That's where the extra "\n" character should be. Replace all cout.flush() with cin.flush() and see what happens. You should be able to remove each cin.clear() then.
The std::istream class doesn't have a flush member. It only has clear. This code should work fine as it is (ie. it runs perfectly as expected for me). At this point I can only assume a faulty implementation of the istream class on the system in question.
Sep 21 '06 #11

P: 1
I was able to reproduce your "cin" streaming problem.
The following sequence solves your problem:

...
cout << "What's your name? ";
cout.flush(); // same result as adding "<< endl;" to previous code line
cin.ignore();
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
...

Kind regards.
Jan 18 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.