Question:
Switch statement help C++, Char values?
Konnor
2011-03-10 19:19:40 UTC
I'm writing a program now that at the very beginning uses a switch statement that asks the user if they would like to see the instructions. Either 1 for yes and 2 for no, i then run it through a switch statement and it sends the user to the proper function. If they enter a wrong number it repeats the question and clears CIN. But for some reason whenever the user (me) enters a Char value, it sends the switch statement into an infinite loop. I'd copy the code but there's no room for the whole switch statement. I've tried setting a case for every letter in the alphabet but obviously that was a waste of time because nothing changed. Also you might want to know that the switch statement is within a Do statement, i declared a BOOL value DONE to FALSE, if the input sends you to the right case then DONE = TRUE and you exit the loop otherwise it loops again... Hope that was detailed enough... Thanks in advance.
Four answers:
Cubbi
2011-03-11 02:50:56 UTC
When the user enters something other than an integer in your case, the operation "cin >> inst;" fails, setting the cin's failbit (which you later attempt to clear(), correctly), but what you don't realize is that nothing is written to instr and no input is consumed. The user may have entered 'a', but inst is still zero, and switch 'a' will never be executed. Because no input is consumed, when you attemt to execute cin >> inst; again, it fails again and you're in an endless loop. Remember to cin.ignore() when you cin.clear()!



Maintaining your logic as much as possible, you could check if the value of inst is still zero (or, in fact, anything other than 1 or 2: the user could have entered 100)



     int inst = 0;

     cin >> inst;

     bool done = false;

     do

     {

         switch(inst)

         {

             case 1:

                 instructions();

                 done = true;

                 break;

             case 2:

                 cout << "\n\tGood luck!\n\n";

                 system("pause");

                 game();

                 done = true;

                 break;

             default:

                 cout << "\nYou need to input number 1 or 2. ";

                 cin.clear();

                 cin.ignore(100, '\n');

                 done = false;

                 cin >> inst;

                 continue;

         }

     } while (!done);



although a more idiomatic way is to check the return value of "cin >> inst" or the statis of cin after the input
green meklar
2011-03-10 21:21:35 UTC
First, you are trying to use char values in a switch that is checking an integer variable. That might technically work, but it is a bad idea. It will probably not do what you want it to.



It seems that what you are trying to do is detect if the user enters a non-number input, and keep asking them to enter something else if they do. What you have is not only an incredibly inefficient approach to doing this, it also just plain won't work. The reason for this is that the error condition generated by a non-number input is already present once cin>> has completed, and has nothing to do with the switch statement or the character values. Even if it did work, it would be vastly more efficient simply to use a default statement.



It IS possible to produce a 'safe' version that will process numbers and non-numbers correctly. The way to do this is to avoid using cin>> on an integer completely. Instead, take the input to a char array and parse that using atoi().



Here is the relevant section of your code (everything from the cin>> to the end of the do while loop) rewritten using the above suggestions:



char* input=(char*)malloc(64);

gets(input);

input[63]='\0';

inst=atoi(input);

do

{

switch(inst)

{

case 1:

instructions();

done = true;

break;

case 2:

cout << "\n\tGood luck!\n\n";

system("pause");

game();

done = true;

break;

default:

cout << endl << "You need to input a number.";

cin.clear();

done = false;

cin >> inst;

continue;

}

}

while(!done);



Note that it is still not perfectly safe. Although it will work for both numbers and non-numbers, it is not safe for inputs larger than 63 characters in length. Since all valid 32-bit integers in base 10 are at most 11 digits in length, this shouldn't be a problem.



It is actually possible to write code which IS safe for inputs of any arbitrary length. However, doing so is quite complicated and probably way outside the bounds of what you are considering doing here.
anonymous
2016-10-28 07:30:20 UTC
it isn't any longer sparkling what you're asking. The code will do precisely what it says it is going to do. If that's no longer what you choose it to do, i'm certain how we can help you without understanding what you choose. i visit claim, although, that it would not quite make experience. Your assignments solid a 'pointer to thread' to a personality, that would have unusual and unpredictable outcomes. maximum probably, you choose 'const char *day;' quite of 'char day;' because you're assigning suggestion that would want to characters that won't be able to be changed to 'day'. yet without understanding what the code is meant to do, it isn't basic to make certain.
Silent
2011-03-10 19:25:27 UTC
It's basically impossible for us to tell you what's wrong with code we can't see.



If there's not enough room for it here, post it on http://www.pastebin.com and post the link here.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...