int test;
cin>>test;
switch(test)
{
case 1:
cout<<"this is fine";
default:
cout<<"This isn''t hit even if a char is entered";
}
Any help is much appreciated
~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
single charater console input... HELP!
Hello,
I''m having a problem using getch() and cin...
if I want to allow the user to input an integer, using either of the above, the program REALLY screws up if they enter a char =)
How do I protect against this?
WyrmSlayer RPG - In Early Development
I copied the code into a VC++ window on my computer and put the main() and #includes in and it worked fine. Ill put the whole code here if you want to try it out. I take it that the function was to return the "works fine" message on test=1 or otherwise "this isnt hit even if a char is entered". You should also put a break statemnt after case 1. At the moment that code will print both messages when a 1 is entered. Here is my full code:
#include <iostream.h>
main()
{
int test;
cin>>test;
switch(test)
{
case 1:
cout<<"this is fine";
break;
default:
cout<<"This isn''t hit even if a char is entered";}
return 0;
}
>>>>>>>>>>>>>>>>>Ilthigore<<<<<<<<<<<<<<<<<
Well it screws up because he''s trying to store a char in an int.
Hibiki
Wheres the any key?
find your element
at mutedfaith.com.
<º>
Hibiki
Wheres the any key?
find your element
at mutedfaith.com.
<º>
HibikiWheres the any key?www.geocities.com/dragongames123/home.html
find your elementat mutedfaith.com.<º>
find your elementat mutedfaith.com.<º>
Try this:
Have the input go into a string. Then check the string and make sure it only contains numbers. Then convert the string into an integer.
Hope this helps, I''m not sure the other guys were understanding your question ^_^. I have had similar problems with the console, and I am aware that it really screws up badly when charactors are in the input for an integer (seems like istream should handle this better?). .sen
Have the input go into a string. Then check the string and make sure it only contains numbers. Then convert the string into an integer.
Hope this helps, I''m not sure the other guys were understanding your question ^_^. I have had similar problems with the console, and I am aware that it really screws up badly when charactors are in the input for an integer (seems like istream should handle this better?). .sen
"I want to make a simple MMORPG first" - Fenryl
quote: Original post by Senses777
Try this:
Have the input go into a string. Then check the string and make sure it only contains numbers. Then convert the string into an integer.
Hope this helps, I''m not sure the other guys were understanding your question ^_^. I have had similar problems with the console, and I am aware that it really screws up badly when charactors are in the input for an integer (seems like istream should handle this better?). .sen
Thanks Senses, I think this will work. I''ll do it that way.
Ilthigore, I had the code basically that way, I just typed it in the post badly. Left a few things out. thanks for the help though =)
~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
WyrmSlayer RPG - In Early Development
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement