mardi 31 janvier 2017

Guess the random number game

I am making a number guessing game and I do not know how to incorporate a certain number of guesses the users has to get the correct answer. I want the user to have only 3 guesses to guess the number but after 3 guesses, they lose if they do NOT get it correct. Here is my code below:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
srand ( time(NULL) );

cout << "Select a difficulty: 1) Easy, 2) Medium, 3) Hard " << endl;
int userlevel;
int userinput;
int randomNumber;
cin >> userlevel;
{
if (userlevel==1)
  cout << "You chose Easy: 3 chances to guess correctly" << endl;
  cout << "Pick a number between 1 and 10: " << endl;

  cin >> userinput;
  randomNumber = rand() % 10 + 1;

  if (randomNumber==userinput)
    cout << "You, guessed correctly! You win!" << endl;
  else
    cout << "I'm sorry, that is not correct. You lose." << endl;
}


{

  if (userlevel==2)
   cout << "You chose Medium: 4 chanaces to guess correctly" << endl;
   cout << "Pick a number between 1 and 50: " << endl;

     cin >> userinput;
     randomNumber = rand() % 50 + 1;

   if (randomNumber==userinput)
     cout << "You, guessed correctly! You win!" << endl;
   else
    cout << "I'm sorry, that is not correct. You lose." << endl;
  }


   {
    if (userlevel==3)
     cout << "You chose Hard: 5 chances to guess correctly" << endl;
     cout << "Pick a number between 1 and 100: " << endl;

     cin >> userinput;
     randomNumber = rand() % 100 + 1;

  if (randomNumber==userinput)
       cout << "You, guessed correctly! You win!" << endl;
     else
       cout << "I'm sorry, that is not correct. You lose." << endl;

 }
  return 0;
 }

Aucun commentaire:

Enregistrer un commentaire