Hi everybody,
I’m having trouble trying to figure out how to create a loop when the ‘difficulty’ takes a value that is not an integer. I want a player to get the ‘default’ statement presented in the switch . The ‘default’ statement seems to make an appearance but it will not execute the ‘cin >> difficulty’ when ‘cin >> difficulty’ equals anything but an int. I tried using the ‘cin.ignore(difficulty);’ but it doesn’t appear to work. I’m I not using it properly?
I’m just learning how to program so I lack plenty of experience.
Here is the code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Welcome to 'CAN YOU GUESS MY NUMBER'\n" <<endl;
cout << "Choose your difficulty: \n" <<endl;
cout << "1: Easy" <<endl;
cout << "2: Normal" <<endl;
cout << "3: Hard\n" <<endl;
//Difficulty loop
int difficulty;
cin >> difficulty;
bool pass1 = true;
switch (difficulty)
{
case 1:
cout << "You will play the game in easy\n" << endl;
pass1 == true;
break;
case 2:
cout << "You will play the game in Normal difficulty\n" << endl;
pass1 == true;
break;
case 3:
cout << "You will play the game in Hard\n" << endl;
pass1 == true;
break;
default:
cout << "You have made an invalid choice." << endl;
cout << "Choose a difficulty level: \n" << endl;
cout << "'1' for Easy" << endl;
cout << "'2' for Normal" << endl;
cout << "'3' for Hard\n\n" << endl;
cin.ignore(difficulty);
cin >> difficulty;
pass1 == false;
} while (!pass1);
// Easy loop
// Normal loop
// Hard loop
return 0;
}