--Originally published at My life @ TEC
This program actually gave me a hard time, because I didn’t know how to declare a random number, so I read the book, I looked for answers online, I asked my dad and I also asked a friend, so when I finally understood how to do it, everything was easy from there until I tried to give the user chances to guess the number. I had to erase all the code to start again, it’s just that I wasn’t concentrated at all so it was hard, at the end I relaxed and it was easier that way.
Here’s my code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
int num1,num2,cont;
std::string resp;
do{
srand(time(NULL));
num1=1+rand()%(101);
cont=9;
cout<<“Hey! I’m thinking of a number between 0 and 100, guess wich one it is, you would have 10 chances to guess.”<<endl;
cin>>num2;
while (num2!=num1 & cont>0){
cont=cont-1;
if (num2>num1){
cout<<“sorry but “<<num2<<” is too high, try again.”<<endl;
cin>>num2;
}
else{
cout<<“sorry but “<<num2<<” is too low, try again.”<<endl;
cin>>num2;
}
}
if(num2!=num1){
cout<<“You couldn’t do it the number I was thinking was “<<num1<<endl;
}
else{
cout<<“¡Congratulations! “<<num2<<” is actually the number I was thinking”<<endl;
}
cout<<“Would you like to play again? (yes/no)”<<endl;
cin>>resp;
}while(resp!=”no”);
return 0;
}
and here’s how it works: