cin to a string- infinite loop

So I've got a menu driven by switch-case.. inside a do/while loop. one option of the menu is to add a last name into something.

That option is set to input to a string like
string lastname;

When I use:
cin >>lastname;

It works fine.. except if there happens to be a space. How can I fix this to either throw an error with a space, or to accept spaces?
Try using gets(lastname) instead of cin >> lastname.
I'm not totally sure though.
Lol.

It's not:

gets(lastname)

I believe it's:
 
getline (cin, lastname);


Make sure you have that semi-colon!
Let me know if It works for you. :)
No, do not use gets(), ever! [edit] It is a bad, dangerous function -- even though it is in the standard library.[/edit]

There are several issues to getting input, but the foremost is that the user will always press ENTER after every input. Therefore, make sure that your inputs account for that.

When you get a number, use something like:

1
2
3
4
int n = 7;  // (or whatever your default value is)
cin >> n;  // try to get the number
if (!cin) cin.clear();  // recover from errors
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );  // get rid of the ENTER key 

When you get a string, use something like:

1
2
string s;
getline( cin, s );  // reads the entire line of input and gets rid of the ENTER key 


Hope this helps.
Last edited on
man gets wrote:
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security.
Oh, sorry, didn't hear anything about gets up until now.
Is gets_s dangerous too? I have it in VS10 - Thank you.
No, because it requires you to give as argument the available length of the target buffer.
It's MS-specific, though.
Besides being MS-specific, i thought it would have been secured exactly because you must insert the size of the buffer -What's wrong with it?-
You wrote:
Is gets_s dangerous too?
I wrote:
No,
Then I wrote:
because it requires you to give as argument the available length of the target buffer.
So I dont know why you wrote:
i thought it would have been secured exactly because you must insert the size of the buffer -What's wrong with it?-
Why would it be dangerous if you have to insert the buffer size? If besides that it still has security problems that's all another thing. But inserting the buffer size in an "input" isn't a negative thing.
I'm not sure why you are having such a hard time understanding me. If you are just trolling then you should know right now that I don't intend to post again on this topic, so you might as well give up now. If not, you should look back and see if you can find exactly where it was that I suggested that gets_s() is dangerous. (You can't, because I very explicitly answered that it was not -- this makes three times now.)
from what i understanded , u want the program to accept the space or to prevent user from hitting space ? well lemme try to code some
cin >> x
if (x != " ")
or ummm if character length != 6*2 or 6*4 ?
@andrew= cin >> x gets just a word, and if there is a space it gets nothing more.
At Duoas:

Edit: Lol I just understood it right now. Sorry.
Last edited on
Topic archived. No new replies allowed.