+6

C++ (IDE) My switch statement's default case repeats infinitely when triggered, any ideas why?

It's pretty much as explained above. My switch statement has 5 integer cases, which all work fine, but the default statement gets all funky if I input anything that ISN'T an integer. I'll drop the code down below, it's only 50 lines: #include <iostream> #include <cstdlib> #include <ctime> #include <windows.h> #include <stdlib.h> using namespace std; int main() { int input; int difficulty_select = 0; //Selection Screen cout << "CMD RPG \n" << endl; cout << "Select a difficulty to play on. If this is your first time playing, you should try the tutorial first.\n" << endl; cout << "1 > Tutorial\n2 > Easy\n3 > Normal\n4 > Hard\n5 > Legendary\n" << endl; cout << "The # > symbol indicates some sort of choice or command. Input the number to the left of the symbol to select that choice/command.\n" << endl; while (difficulty_select == 0){ cin >> input; switch (input) { case 1: cout << "Tutorial not available at this time...\n"; break; case 2: cout << "Easy difficulty not available at this time...\n"; break; case 3: cout << "Normal difficulty not available at this time...\n"; break; case 4: cout << "Hard difficulty not available at this time...\n"; break; case 5: cout << "Legendary difficulty not available at this time...\n"; break; default: cout << "Input not recognized, or unavailable at this time.\n"; break; } } return 0; } Like I said, all the actual cases work fine, but if I were to input the letter "a" or something, the default case just goes on infinitely. I really don't get it. I put a break there, so the only thing I could think of is maybe the "while (difficulty_selection == 0) { code }" keeps repeating and triggering the default.

7/19/2019 12:00:24 AM

Master_Sw0rd

13 Answers

New Answer

+9

The problem is in the input buffer, your input is supposed to be integer, but you give a character instead. When you do this, the character will stay in the input buffer. A way to solve this is by flushing the buffer. You can add cin.clear(); cin.ignore(); in the default statement, between the cout and break

+7

I'm going to write this as advice but it may look more like a rant (fair warning): As a programmer, it is imperative that you handle all special cases that could potentially break your program. Issues like wrong input should not be the reason your code ends up breaking. Think about all of the applications that have been released over the years. The most successful ones handle all special cases before being released, and when they fail they fail gracefully. For example, take your code: the computer is expecting an integer from you, yet you're entering a letter and you have not written anything in your code to address such a situation. As a result, the program has no choice but to break. This simply cannot happen in the professional world, or we developers would not last long in the field. For small programs that expect numerical input, I'll either reprompt the user, use random numbers when invalid characters are entered, or gracefully terminate after telling the user what to do next time.

+3

I think I understand, I'll just throw a warning out not to enter any letters then. I'm sure further down the road I'll learn something that might be a good alternative, but with my grand total of 104 exp on C++, I might be better off just learning more for now. Thank you guys for taking a look at it anyways!!

+3

Mind To Machine 💻🕆 Well even C++ standard library is way smaller than Java, it's still hard for one to know every bits of it, even for me. Anyway, those two functions are not used so often, so it's still pretty normal if someone doesn't know it

+2

So you are saying that it won't ask for another input after you put a character as an input?

+2

I wouldn't know because the default case rapid fires it's output at me. But yes, I can only assume the program is ignoring or skipping the request for input somehow...

+2

Master_Sw0rd you should edit your post to make clear that this code should be runned on an IDE with an interactive console not on sololearn in order to get what you are talking about, as some bigginners might be confused

+2

I'm unsure what you mean by that... Does code behave differently on the playground then in an IDE?

+2

Master_Sw0rd yep, try running it here and see, here you gotta input all your data BEFORE you can see code output, and its not ideal for this example code you posted

+2

Agent_I i learned a two C++ functions today 🤓, seriously i never heard about cin.clear() and cin.ignore(), i'm kind of a Java guy too though 😉

+2

Just to add, I would not default my while loop condition to run infinitely (int difficulty_select = 0). You should probably just use a break statement at that point and just use 'while(true)'. I think it should be solved already, but I thought I would throw in some "best practices" I've learned. Best of luck!

+1

the reason you are getting that wierd loop thats just executing the default case probably just some runtime error thing, because based on what i know C++ cant switch string types, "a" is a string. C++ can only switch char and int types