473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

A big problem with getline

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
11 9891
tyreld
144 100+
I can't reproduce your problem. This code runs fine for me.
Sep 21 '06 #2
tyreld
144 100+
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
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
tyreld
144 100+
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
tyreld
144 100+
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
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
tyreld
144 100+
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
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
D_C
293 100+
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
tyreld
144 100+
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
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

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

Similar topics

7
by: Shane | last post by:
Hi, Thanks in advance for the help. I have been to many websites and tried several solutions to my problem, but have fixed part of it. It's time to come humbly to the newsgroups for help :-) ...
7
by: truedos | last post by:
hello, I have a text file that looks like this: text numbers text numbers text numbers .. ..
5
by: allspamgoeshere3 | last post by:
Hi! I'm having trouble reading text lines from a file describing the levels for a simple game I'm creating. Inside the function that reads the content of the file there is a loop that basically...
16
by: Jordan Tiona | last post by:
Here is my code. main.cpp: #include "cddb.h" CDData* head; CDData* curData; int main(){
9
by: Thomas Helmke | last post by:
Hello NG. I'm trying to read in a textfile. I will give you an extract of my code: bool LogfileHandle::ReadFile( string filename ) { std::ifstream LogFile( filename.c_str() ); if(...
18
by: vermarajeev | last post by:
Hello everybody, This is my second query in this post. Firstly thankx to Banfa, for helping me solve my first query. Here is the code which I have written. #include<iostream>...
1
by: cecil258 | last post by:
Hey everyone... I'm in a beginning C++ class right now and below is the code I've written for my current assignment... What I need to do is put all of the input code (basically the huge chunk of...
2
by: manwanirg | last post by:
the function getline is a public member of istream and cin.getline can be used. Since ifstream is publicily derived from istream, getline shall be available in ifstream as well. However,on solaris...
6
by: ankit.kumar.agarwal | last post by:
I am facing a problem with getline I am reading a text file with a getline function the lines can have '|' as separator. everything works OK but in case if i have 2 delimitors in file '234||...
3
by: JackC | last post by:
Hi, How do i use stringstreams getline function to extract lines from an existing string? Say i have: string strlist = "line1\r\nLine2\r\nLine3\r\n"; I want to extract each line out into a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.