C++ (IDE) My switch statement's default case repeats infinitely when triggered, any ideas why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

19th Jul 2019, 12:00 AM
Nate
Nate - avatar
11 Answers
+ 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
19th Jul 2019, 12:16 AM
Agent_I
Agent_I - avatar
+ 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!!
19th Jul 2019, 12:19 AM
Nate
Nate - avatar
+ 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
19th Jul 2019, 12:34 AM
Agent_I
Agent_I - avatar
+ 2
So you are saying that it won't ask for another input after you put a character as an input?
19th Jul 2019, 12:08 AM
Agent_I
Agent_I - avatar
+ 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...
19th Jul 2019, 12:11 AM
Nate
Nate - avatar
+ 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
19th Jul 2019, 12:19 AM
Mind To Machine ๐Ÿ’ป๐Ÿ•†
Mind To Machine ๐Ÿ’ป๐Ÿ•† - avatar
+ 2
I'm unsure what you mean by that... Does code behave differently on the playground then in an IDE?
19th Jul 2019, 12:21 AM
Nate
Nate - avatar
+ 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
19th Jul 2019, 12:25 AM
Mind To Machine ๐Ÿ’ป๐Ÿ•†
Mind To Machine ๐Ÿ’ป๐Ÿ•† - avatar
+ 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 ๐Ÿ˜‰
19th Jul 2019, 12:28 AM
Mind To Machine ๐Ÿ’ป๐Ÿ•†
Mind To Machine ๐Ÿ’ป๐Ÿ•† - avatar
+ 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!
19th Jul 2019, 12:39 AM
Zeke Williams
Zeke Williams - avatar
+ 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
19th Jul 2019, 12:15 AM
Mind To Machine ๐Ÿ’ป๐Ÿ•†
Mind To Machine ๐Ÿ’ป๐Ÿ•† - avatar