Mar 22, 2018 at 8:39pm UTC
Hey all! I need help with my first program! I made a guess game and i want to have 5 tries and when all 5 tries are false to ask me if i want to play again or no. Thanks!
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
enum DIFFICULTY {EASY=1, MEDIUM, HARD, EXPERT, IMPOSSIBLE};
int diff, rand_num, guess, chances=0 ;
char answer;
int get_difficulty( )
{
int difficulty;
// Get difficulty
cout << " \t\t\tWelcome to the number guessing game! Pick a difficulty:" << endl;
cout << "--------------------------------------------------------" << endl;
cout << " 1) Easy -- recommended\n" << endl;
cout << " 2) Medium -- intermediate\n" << endl;
cout << " 3) Hard -- advanced\n" << endl;
cout << " 4) Expert -- only 2% can do\n"<< endl;
cout << " 5) Impossible -- Nobody has ever done this\n" << endl;
cout << " 0) Exit -- For pussies\n" << endl;
cout << "--------------------------------------------------------" << endl;
cin >> difficulty;
cout<< "---------------------------------------------------------" << endl;
if(difficulty==1){
cout << "You've chosen Easy difficulty" << endl;
}else if(difficulty==2){
cout << "You've chosen Medium difficulty" << endl;
}else if(difficulty==3){
cout << "You've chosen Hard difficulty" << endl;
}else if(difficulty==4){
cout<< "You've chosen Expert difficulty" << endl;
}else if(difficulty==5){
cout<< "You've chosen Impossible difficulty" << endl;
}
cout << "-------------------------------------" << endl;
switch (difficulty)
{
case (EASY):
return 20;
break;
case (MEDIUM):
return 50;
break;
case (HARD):
return 100;
break;
case (EXPERT):
return 500;
break;
case (IMPOSSIBLE):
return 1000;
break;
default:
exit( 0 );
}
}
int main( )
{
system("color 2");
chances = 5;
diff = get_difficulty( );
guess;
srand(time(0));
rand_num = rand() % diff + 1;
while ( true )
{
cout << "My number is between 1 and " << diff << ": " << endl;
cout << chances << " CHANCES LEFT!!" << endl;
cout << "-------------------------------" << endl;
cin >> guess;
cout << "-------------------" << endl;
if(guess > rand_num) {
cout << "Too high! Try again.\n";
} else if(guess < rand_num) {
cout << "Too low! Try again.\n";
} else {
break;
}
chances--;
}
if(chances == 0) {
cout << "You ran out of tries!\n\n";
} else{
cout << "You guessed the correct number!" << endl;
cout << "My secret number was" << rand_num <<endl;
}
while(true) {
cout << "Would you like to play again (Y/N)? ";
cin >> answer;
cin.ignore();
// Check if proper response.
if(answer == 'n' || answer == 'N' || answer == 'y' || answer == 'Y') {
break;
} else {
cout << "Please enter \'Y\' or \'N\'...\n";
}
// Check user's input and run again or exit;
if(answer == 'n' || answer == 'N') {
cout << "Thank you for playing!";
break;
} else {
cout << "\n\n\n";
}
}
// Safely exit.
cout << "\n\nEnter anything to exit. . . ";
cin.ignore();
return 0;
}
Last edited on Mar 23, 2018 at 8:25am UTC
Mar 22, 2018 at 10:21pm UTC
Yeah sorry! Kinda new here. Can you give me an example with that new dunction and where exactly to put it in? I would be more than happy!
Mar 23, 2018 at 5:02am UTC
keskiverto i still can't manage to do that.... Still going with -1 -2 -3 and when i guess the number then it says YOU RAN OUT OF TRIES. Please somebody rebuild if possible :( been stuck here for 2 3 days already
Last edited on Mar 23, 2018 at 5:47am UTC