is this fuction surposed to stop until i enter something until i press enter, cause it just jumps to the next line of code.
getline(cin, bookers[number_of_bookers].name, '\n');
thanks dac,
is this fuction surposed to stop until i enter something until i press enter, cause it just jumps to the next line of code.
getline(cin, bookers[number_of_bookers].name, '\n');
thanks dac,
getline(cin????????????????? ,check the syntax
Perfectly valid syntax for the version of getline which works with string objects.Originally Posted by qqqqxxxx
What compiler/IDE do you use. There is a known issue with the string version of the getline function in MSVC 6 that can be resolved by applying the fixes found at this site or for that fix and even more MSVC 6 fixes (highly suggested) you can go here.Originally Posted by dac
Alternately it might not be that at all, but rather a problem with there being a newline character leftover in the input stream from a previous input operation. The following would demonstrate this problem:
The cin >> operation will get a character from the input stream and wait for a newline (which will not be read) to be pressed. When the getline function is reached it reads up until the first newline character and moves on. Since there is already a newline still left in the input stream from the previous cin >> the getline function immediately exits and we move on to the next line of code making it look like the getline call was skipped (really wasn't just looks that way). The solution to this is to clear the input stream before each getline function call that comes after a cin >> type of operation. This is done by the use of the stream's ignore member function (as mentioned by arjunajay):Code:char var1; string str1; cin >> var1; getline(cin,str1,'\n'); // This line will get "skipped"
You need to include <limits> for numeric_limits.Code:char var1; string str1; cin >> var1; cin.ignore(numeric_limits<streamsize>.max(),'\n'); getline(cin,str1,'\n'); // This line will not get "skipped"
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
syntax check on register 5: http://www.cppreference.com/cppstring/getline.htmlOriginally Posted by qqqqxxxx
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
Another common variant to take care of newlines in the input is
std::ws is a manipulator that accepts only whitespace. You can think of it as a sort of dummy variable. The only time when you would not want to use it is if leading whitespace is significant. With the above " Hello\n" and "Hello\n" are the same. Normally you would want this.Code:if(std::getline(std::cin >> std::ws,line) { // Process line }
1) your syntax is wrong
2) yuck.
I'd stick with using ignore after a cin
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT
Some further explanation...The cin >> operation will get a character from the input stream and wait for a newline (which will not be read) to be pressed. When the getline function is reached it reads up until the first newline character and moves on. Since there is already a newline still left in the input stream from the previous cin >> the getline function immediately exits and we move on to the next line of code making it look like the getline call was skipped
When you use a line like:
cin>>firstName;
the program looks for some input to read from cin(cin represents input from the keyboard). Since cin starts off empty, there is no input to read, and the program must wait for the user to enter some input. After the user enters some input, say 'Bob', and hits return, the return causes a newline(\n) to be added to the input, so this is what's in cin:
Bob\n
The operation 'cin>>' is defined to stop reading input when it encounters whitespace(spaces, tabs, newlines), so 'Bob' is read from cin, and then cin>> stops reading input, leaving the trailing '\n' in cin. Any unread data in cin remains there and will be read during the next read operation.
getline() works differently than cin>>. getline() reads in input until after it reads in a \n whereupon it immediately stops. So with a \n remaining in cin, getline() does not have to wait for more user input--there is already input in cin that getline() can read: the \n. Therefore, getline() reads the \n and stops, and execution continues with the next line in your program--making it seem like getline() was skipped.
Note that if you had used cin>> the second time you read from cin instead of getline(), the result would have been different. The operation 'cin>>' is defined to skip all leading whitespace, so cin>> would have skipped over the \n remaining in the input stream. Since cin would be unable to find any input to read after the \n, cin>> would have to wait for the user to enter more input.
So, the lesson is: when you use both cin>> and getline() in a program, you have to be aware that the operation 'cin>>' can leave a trailing \n remaining in cin, which will mess up a subsequent getline(). You can solve that problem by erasing any previous input remaining in cin using ignore().
Last edited by 7stud; 03-10-2006 at 03:39 PM.