views:

135

answers:

4

Hi. I'm working with strings in C++. I recently came across a problem when entering strings. I'm using cin >> string; to get my string as user input. When the user enters a space into the string, the next input is automatically filled out with the remaining letters, or sometimes left blank. As the next input string is often an integer, this will result in an unpleasant bug. What's a good fix for this?

EDIT: Here's the current code:

cout << "Please print the enemy's name: ";
getline(cin, enemyName);
+9  A: 

You probably want to get all input into the string up until the user presses enter. In that case, it can be said that what you really want is to read a "line" of text. To do that, you'd use std::getline, like so:

std::getline(cin, enemyName);

That is assuming enemyName is defined as an std::string. If enemy name is a c-style charater array, you'd want to use cin.getline, like this:

cin.getline(enemyName, sizeof(enemyName));

But, try to avoid using C-style character arrays at all in C++.

SoapBox
@SoapBox Sorry, I'm not familiar with that function. If I'm using namespace std, would I need the first line at all?
Elliot Bonneville
The first line defines a string named "str". Now that you've added example code I can give you a better example, what you want is: std::getline(cin, enemyName);... I'll edit the response.
SoapBox
Correct: you could leave out the "std::" if you're using the "using namespace std;" line in your code
advs89
Although I believe most people recommend that you not habitually do that (the "using namespace std;" thing) - (I'm not any kind of C++ authority though, so don't take my word for it)
advs89
Ah, thanks. Wow; as a new user, I'm impressed with all the activity this single question has had in about ten minutes!
Elliot Bonneville
@SoapBox I've entered your code. However, it wont pause when I ask it to give me the line. I'll update my question (again). Is this intentional, and due to a lack of knowledge on my part?
Elliot Bonneville
it should work as intended. Did you read anything else from cin before the call to getline? (Like an integer, for example?) If you read an int, the "return" the user pressed after the int is still in the buffer when getline is called, so it doesn't wait for more input. You can fix that by adding: cin.ignore(100); The 100 here is the most characters you want to ignore from the input, so this will skip all input until it finds the end of the line, or stop at 100 characters skipped if there was no end of line.
SoapBox
@SoapBox Oops, it was due to a previous programming error, which I've located. Thanks a lot! I can't accept your answer because I don't have enough reputation, else I would.
Elliot Bonneville
@SoapBox Thanks again, I've accepted your answer.
Elliot Bonneville
+2  A: 

Use getline(cin, string); instead.

Jon Purdy
+2  A: 

Use getline() to read in an entire line at a time.

getline (cin, string);
Dave Hinton
+3  A: 

The behavior of >> with strings is intentional; it interprets whitespace characters as delimiters to stop at, so it's really best at chomping words. std::getline() (#include <string>) uses '\n' as the delimiter by default, but there's also a version of std::getline() that takes a custom delimiter character if you need it.

Owen S.
@Owen S. Thanks a lot. Could you (I feel stupid right now) show me how to use that in relation to my current code (using namespace std)?
Elliot Bonneville
http://www.cplusplus.com/reference/string/getline/ - This article has the syntax, and a few examples. getline ( std, somestring, '\t' ) would stop at a tab, for example...
advs89
@Adam Doyle Thanks a lot, that page is invaluable. If I could accept your answer as well, I would.
Elliot Bonneville