0

How do I grab an entire statement into a string?

I've tried getline(std::cin, str) but it doesnt even let me input anything, just continues on. I could do cin >> str, but I want the entire line. And if you are going to suggest multiple cin with multiple variables, I dont know how many words are going to come in, so I can't just concatenate that way. Any ideas to get the entire line? EDIT: solved. Problem was that I used a std::cin before hand. FIxed by using std::cin.ignore() before getline function

17th Apr 2017, 7:40 PM
JustSomeGuy
JustSomeGuy - avatar
5 Answers
+ 4
That's the right idea. If it's not letting you input anything, you're doing something wrong somewhere else. This works for me: std::string line; std::getline(std::cin, line); std::cout << line;
17th Apr 2017, 5:49 PM
Squidy
Squidy - avatar
+ 2
getline doesn't open an inputbox for me on android. As a workaround I just do this: std::getline(std::cin, abcd); if(false)std::cin >> abcd; That fixes it for me.
17th Apr 2017, 5:51 PM
Dennis
Dennis - avatar
+ 2
I don't know your code so I'm just guessing. Maybe you're using cin before getline. cin leaves a newline character behind after pressing enter, so getline just reads that and keeps going. You can use std::cin.ignore(); after cin to ignore that newline character
17th Apr 2017, 7:06 PM
Dennis
Dennis - avatar
0
Neither of those answers helps. I want to take the whole line of information, and cin >> only takes till the next space... As for std::getline, its not letting input anything in the first place. I'm on desktop working on a project in VS, so it should be a compiler thing. If I formatted wrong it would tell me. Maybe it's a header thing? I'm currently only using <isostream>, should I be using something else too?
17th Apr 2017, 6:53 PM
JustSomeGuy
JustSomeGuy - avatar
0
@Dennis Thank you so much! It was the new line that was generated. THe std::cin.ignore cleared up the problem. You're always here to help!
17th Apr 2017, 7:38 PM
JustSomeGuy
JustSomeGuy - avatar