|
-
Feb 20th, 2002, 11:37 AM
#1
getline and cin
I have a problem using cin and getline:
Code:
string str;
getline(cin, str);
This requires the user to hit enter twice. It seems that there is some buffer storing one string and getline retrieves only what is pushed out of the buffer. This is because if I do a
cin.putback('\n');
first, the user needs to hit enter only once after his input, but the retrieved string is only the \n I pushed to the input buffer. Likewise, when I do
Code:
string str;
cout << "Enter a string:" << endl;
getline(cin, str);
cout << str << endl;
cout << "Enter another string:" << endl;
getline(cin, str);
cout << str << endl;
the program first asks for a string. Then it waits two times for input ( the user will probably just hit enter ), then output the first line, then ask for another string. While this time the user needs only input one string, the program will then output not this string, but rather the second one from the first getline call.
Any ideas how to solve this?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 20th, 2002, 12:30 PM
#2
Hyperactive Member
i've posted a question for this before, search the thread
i've gotten an answer for it but it didn't work...i've attributed the
answers that I've gotten to mean the people didn't know
exactly what my problem was.
I've never figured out the reason for this and I've just decided
not to expect user input from a cmd line anymore :P
Bababooey
Tatatoothy
Mamamonkey
-
Feb 20th, 2002, 12:39 PM
#3
the strange thing is that when you asked it, it worked for me...
Usually I don't do console either, but I want to solve this "hard homework" in a thread near this.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 20th, 2002, 12:44 PM
#4
Hyperactive Member
I know i've fixed this problem in the past too......
just don't remember what I had to do
I knew if susan begged enough you'd help her with the problem
shame on you
Bababooey
Tatatoothy
Mamamonkey
-
Feb 20th, 2002, 01:04 PM
#5
Unless I'm missing something - you're trying to use String::getline without a delimiter. I think it won't respond until it sees end of stream. In unix this is ctrl-d. You could make it a carriage return:
I dunno what the default end of stream is for console mode - ctrl/c
? anybody?
Code:
String str;
char delimiter = '\r'; // use carriage return as end of stream
getline(cin,str, delimiter);
-
Feb 20th, 2002, 02:18 PM
#6
no, I'm calling std::getline which differs from std::istream::getline because it can take the string class as argument. It is overloaded to two functions:
streamtemplate& getline(streamtemplate& strea, stringtemplate& strin, chartype delim);
where strea is the stream to extract from, strin is the string object to receive the string and delim is the delimiter.
The second function is an inline that doesn't have a delimiter and just calls the first one with \n as delimiter.
noble: this forum is my hobby. I'm helping everyone here, even you
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 20th, 2002, 02:30 PM
#7
I usually implement my own form of getline, using the read() method.
Z.
-
Feb 23rd, 2002, 05:05 PM
#8
Addicted Member
Im not sure if I understand your problem correctly but I think I might be able to help. I've had problems with getline before too. Try declareing a "dummy" string and using it like this:
Code:
string str, dummy;
cout<<"Enter first string."<<endl;
getline(cin, str);
getline(cin, dummy);
cout<<"Enter second string."<<endl;
getline(cin, str);
That fixed my problem with getline before...
To protect time is to protect everything...
-
Feb 24th, 2002, 05:07 AM
#9
Yes that would work but it won't free the user from the need to hit enter more than once.
Anyway, it's too late now. Thx to all that tried to help.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 12th, 2002, 03:52 PM
#10
Stuck in the 80s
Did anyone ever find an answer on how to do this?
-
Nov 12th, 2002, 04:37 PM
#11
Stuck in the 80s
I just found the solution here: http://support.microsoft.com/default...;en-us;240015& if anyone's interested.
-
Nov 12th, 2002, 04:56 PM
#12
Monday Morning Lunatic
Whoa, deja vu. I thought we'd already determined this was a compiler bug, CB?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 12th, 2002, 04:58 PM
#13
Stuck in the 80s
Originally posted by parksie
Whoa, deja vu. I thought we'd already determined this was a compiler bug, CB?
You may have. This is a few month old thread that I dug up while seeking out an answer.
I guess it's just a bug in <string> that required a very easy fix. And it has been fixed in .Net as well.
I just wish I would have consulted MSDN sooner instead of wasting my time on various forums.
-
Nov 12th, 2002, 05:01 PM
#14
Monday Morning Lunatic
Ahhh I see. 
This is one of the problems with C++ at the moment, the quality of Standard Library implementations is still not up to scratch. They're getting pretty close, but nobody's got a fully-working implementation yet. Not MS, not SGI, Sun, GNU, nobody.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 13th, 2002, 06:40 AM
#15
Might be a project: the VBF-STL, a complete implementation of the STL, both for windows and POSIX...
If anyone has got the time...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 13th, 2002, 07:21 AM
#16
Monday Morning Lunatic
Not sure. There's already STLport, and the GNU implementation which is currently the most complete.
It would take years to build up another.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 13th, 2002, 09:58 AM
#17
Probably.
What's STLport?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 13th, 2002, 10:14 AM
#18
Monday Morning Lunatic
It's another implementation of the STL, for non-compliant compilers and stuff.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 13th, 2002, 11:38 AM
#19
Hyperactive Member
Parksie...........................
REBOOT!!!!!!!!!!!!!!!
Bababooey
Tatatoothy
Mamamonkey
-
Nov 13th, 2002, 12:47 PM
#20
An implementation for compliant compilers would be more interesting - by the time we'd be done even MSVC would hopefully be compliant.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|