Connecting Tech Pros Worldwide Forums | Help | Site Map

A big problem with getline

Member
 
Join Date: Aug 2006
Posts: 51
#1: Sep 21 '06
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. . . . . . .

Familiar Sight
 
Join Date: Sep 2006
Posts: 144
#2: Sep 21 '06

re: A big problem with getline


I can't reproduce your problem. This code runs fine for me.
Familiar Sight
 
Join Date: Sep 2006
Posts: 144
#3: Sep 21 '06

re: A big problem with getline


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.
Member
 
Join Date: Aug 2006
Posts: 51
#4: Sep 21 '06

re: A big problem with getline


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. . . .
Familiar Sight
 
Join Date: Sep 2006
Posts: 144
#5: Sep 21 '06

re: A big problem with getline


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.
Familiar Sight
 
Join Date: Sep 2006
Posts: 144
#6: Sep 21 '06

re: A big problem with getline


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.  
Member
 
Join Date: Aug 2006
Posts: 51
#7: Sep 21 '06

re: A big problem with getline


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?
Familiar Sight
 
Join Date: Sep 2006
Posts: 144
#8: Sep 21 '06

re: A big problem with getline


Quote:

Originally Posted by candexis

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".
Member
 
Join Date: Aug 2006
Posts: 51
#9: Sep 21 '06

re: A big problem with getline


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. . . . . . . . .
D_C D_C is offline
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 294
#10: Sep 21 '06

re: A big problem with getline


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.
Familiar Sight
 
Join Date: Sep 2006
Posts: 144
#11: Sep 21 '06

re: A big problem with getline


Quote:

Originally Posted by 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.

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.
Newbie
 
Join Date: Jan 2008
Posts: 1
#12: Jan 18 '08

re: A big problem with getline


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.
Reply