I made a certain program and I am now trying to get rid of some annoying little bugs. Going to post a part of the code so you guys can help me out .
Code:
#include <iostream
int main()
{
cout<<"1- New Game" <<endl;
cout<<"2- Exit" <<endl;
cout<<"Choose your option: " <<endl;
bool ValidOption = false;
while (ValidOption != true)
{
char option[100];
cin.clear();
cin>>option;
int int_option = atoi(option);
switch (int_option)
{
case 1:
ValidOption = true;
cout<<"ok.";
case 2:
ValidOption = true;
cout<<"ok";
default:
ValidOption = false;
cout<<"Error: ";
break;
}
}
}
Ok so the code above displays a simple menu and it will display "ok" whether you insert "1" or "2".
Now if you are to insert a char, string or anything else it will work, unless you separate it by a space. The loop will run twice considering cin stops at spaces (Which means it will display "error:" twice). Can anyone help me?
Example:
1- New Game
2- Exit
Choose your option: 3
Error: 4
Error: gfgdg
Error: ad
Error:^Z
Error:adsad adada
Error: Error: 1
"ok"