cin to a string- infinite loop

Oct 4, 2011 at 10:01pm
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?
Oct 5, 2011 at 12:13am
Try using gets(lastname) instead of cin >> lastname.
I'm not totally sure though.
Oct 5, 2011 at 1:29am
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. :)
Oct 5, 2011 at 1:30am
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 Oct 5, 2011 at 1:31am
Oct 5, 2011 at 1:59am
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.
Oct 5, 2011 at 11:18pm
Oh, sorry, didn't hear anything about gets up until now.
Is gets_s dangerous too? I have it in VS10 - Thank you.
Oct 6, 2011 at 12:56am
No, because it requires you to give as argument the available length of the target buffer.
It's MS-specific, though.
Oct 14, 2011 at 7:39pm
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?-
Oct 14, 2011 at 8:49pm
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?-
Oct 15, 2011 at 9:09pm
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.
Oct 15, 2011 at 11:48pm
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.)
Oct 16, 2011 at 2:18am
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 ?
Oct 16, 2011 at 8:15pm
@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 Oct 16, 2011 at 10:19pm
Topic archived. No new replies allowed.