I've recently started working with C++ and wondered if there was a "right" way for getting input from the console window. From what I've seen whenever I attempted to implement it more than once, cin>>string seems to fail on items of the "string" type. The use of getch()/scanf() isn't really satisfactory because getch() requires lots of garbage add ons to recognize the end of the string, and backspacing, while scanf() seems to run into the same problem as cin>>string. I've tried dozens of different methods for handling input from the console window, and most of them seem to have seemingly random errors if implemented more than once in the program. If anyone could provide suggestions or examples for getting input for the "string" type I would appreciate it.
Getting input the rightway?
Collapse
X
-
Originally posted by BanfaLook up getline in you C++ reference bookComment
-
I do not think flush is a member of basic_istream (type of cin) (at least not in standard C++) it is a member of basic_ostream.
This makes sense all mirrors the badness of calling fflush on an input stream in C (it's bad don't do it).
I think the cin.ignore may be the function to use.Comment
-
-
Originally posted by BanfaI do not think flush is a member of basic_istream (type of cin) (at least not in standard C++) it is a member of basic_ostream.
This makes sense all mirrors the badness of calling fflush on an input stream in C (it's bad don't do it).
I think the cin.ignore may be the function to use.Comment
-
Originally posted by xlsmntcin.ignore() causes the function to eat the first character of every string after the first string, and I have no idea why. The upside is it is now providing me with strings instead of random ascii symbols.
Code:string getstring() { string input; getline(cin, input); return input; }
Comment
-
Be careful; using getline like that, the program will look for any and all text from the start of the stream to the newline character ('\n, it's made when you press <Enter> or <Return>). It grabs that text, removes it from the stream, and returns it, but it leaves the '\n' still in the stream. This might mess you up if you call cin.get() or getline again after the original getline call. I ran into this problem a lot when I was learning C++.Comment
-
Originally posted by Ganon11Be careful; using getline like that, the program will look for any and all text from the start of the stream to the newline character ('\n, it's made when you press <Enter> or <Return>). It grabs that text, removes it from the stream, and returns it, but it leaves the '\n' still in the stream. This might mess you up if you call cin.get() or getline again after the original getline call. I ran into this problem a lot when I was learning C++.
Also I was wondering whether this
Code:void getstring(string& ptr) { getline(cin, ptr); } int main() { string * ptr; string mystring; ptr = &mystring; getstring(*ptr); }
Code:string getstring() { input string; getline(cin, string); return string; } int main() { string mystring; mystring = getstring(); }
Comment
Comment