Skip to content

Instantly share code, notes, and snippets.

@jesuscmadrigal
Created September 1, 2017 17:13
Show Gist options
  • Save jesuscmadrigal/53852841b06a5cafd1cfbeb91eeb18ab to your computer and use it in GitHub Desktop.
Save jesuscmadrigal/53852841b06a5cafd1cfbeb91eeb18ab to your computer and use it in GitHub Desktop.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main ()
{
int Random, Num ,Guess;
Guess = 0;
srand(time(0)) ;
Random = (rand()%100+1) ; //Generates a random number and saves it as Random Guess = 0 ;
cout << "sssssh, secret is "<< Random << endl;
cout << "I have a number between 1 and 100." << endl ;
cout << "Please guess a number between them:" << endl ;
cin >> Num ;
Guess = Guess + 1 ;
while(Num != Random){ //While Num is different from Random...
if (Num < Random) {
cout << "Oops, " << Num << " is too low, try again:" << endl ;
} else {
cout << "Oops, " << Num << " is too high, try again:" << endl ;
}
cout << "Please guess a number between them:" << endl ;
cin >> Num ;
Guess = Guess + 1 ;
}
// If Num = Random then the program ends
cout << "Congratulations! " << Num << " is the right answer." << endl ;
cout << "You made " << Guess << " guesses to get the right answer." << endl ;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment