Hi everybody,
I wrote a game in which you have to guess a number generated randomly,but when i try to assign a random number to the variable, it just doesn't work but it compiles fine.
The code :
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main () {
string replay;
do {
srand(time(0));
char level;
int number;
cout << endl << endl <<"Welcome to the guess game" << endl;
cout << "Choose your level :" << endl << "1 : very easy (1-50)" << endl;
cout << "2 : Easy (1-100)" <<endl;
cout << "3 : Moderate (1-250)" <<endl;
cout << "4 : Hard (1-500)" <<endl;
cout << "5 : Very hard (1-1000)" <<endl;
cout << "6 : Extreme (1-2500)" <<endl;
cout << "7 : IMPOSSIBLE (1-10000)" <<endl;
cin >> level;
switch (level) {
case 1:
number = 1+(rand()%50);
break;
case 2:
number = 1+(rand()%100);
break;
case 3:
number = 1+(rand()%250);
break;
case 4:
number = 1+(rand()%500);
break;
case 5:
number = 1+(rand()%1000);
break;
case 6:
number = 1+(rand()%2500);
break;
case 7:
number = 1+(rand()%10000);
break;
}
int ans = 0;
int chances = 20;
while(ans!=number && chances!=0) {
cout << "Enter your number" << endl << endl;
cin >> ans;
if (ans < number) {
cout << "Bigger" << endl;
--chances;
cout << chances << " chances left." << endl;
} else if (ans > number) {
cout << "Smaller" << endl;
--chances;
cout << chances << " chances left." << endl;
} else {
cout << "You guessed it !! Congratulations !! with " << chances << " chances left !!" << endl;
cout << "Replay ? (y/n)" << endl;
cin >> replay;
}
}
cout << "You lost !" << endl;
cout << "Replay ? (y/n)" << endl << endl;
cin >> replay;
} while(replay == "y");
}
i will appreciate any help, thanks