• Advertisement

Archived

This topic is now archived and is closed to further replies.

Bad Input (C++)

This topic is 5509 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my program the user has to input a char, and then my program processes it. If they input a string in said my program goes wrong. How can i stop this. PS I am using std::cin to get the char from the user.

Share this post


Link to post
Share on other sites
Advertisement
You can cin a string, and then check if the length is more than one. Otherwise proceed as planned.

...err... I'm still not sure what you mean. (Did I got it correct?) Pls clarify more.

[edited by - DerekSaw on January 24, 2003 4:41:33 AM]

Share this post


Link to post
Share on other sites
That''s the right idea, but i''m sure there''s a better way than having cin a string. It seems like a waste of memory if there is a better way

Share this post


Link to post
Share on other sites
Just to expand on what Zipster was saying, the user being a moron puts the stream into a "fail" state which needs to be cleared before input can continue. check out the iostream documentation for more details. Zipster''s code invokes the implicit void* conversion operator on cin, which returns NULL if the stream is in a fail state.


Don''t listen to me. I''ve had too much coffee.

Share this post


Link to post
Share on other sites
#include "conio.h"

void main() //Or whatever your functions called
{
int myInput = getch();//Reads one character from the keyboard
//Now myInput holds the ASCII code for that character
if (myInput==13) //ASCII-value 13 which means [ENTER]
//[do_something]
}

Share this post


Link to post
Share on other sites

  • Advertisement