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

Edited 4 Years Ago by Karlwakim: n/a

char level;

its suppose to be an integer if your gonna use it in your switch case

Edited 4 Years Ago by zeroliken: n/a

@zeroliken

Chars are integers. No problem there.

@Karlwakim

I think your problem is srand() is inside your loop. Put it before the loop and you should 'go random'.

Rule of thumb: srand() allways only once at the beginning of your main.

@Caligulaminus

"Chars are integers. No problem there."
Your'e right, but there's a problem.
I think what

zeroliken

means is that when you type in 1 and save it in level the key-event will save it as a integer with a value of 49 (see ASCII)
So a switch with the case 1 doenst make any sense.
Just change

case 1:

to

case '1':

or

case 49 :

(And the others according to the ASCII table )

@Karlwakim
If you don't know ASCII then you really should look it up.
What cin returns is defined by ASCII

Thanks everybody,
I found out that even if i put srand() before the loop, the same problem occurs but Dman01 and caligulaminus are perfectly right. It works also if i use

int level;

instead of

char level;

Thanks again.

but why bothering yourself about char for switch???
Use int which is very clean and really the best for switch.

logic means less complications but efficient.

int asn;
cin>>ans;
switch(ans){
  case 1:
   break;
  case 2:
   break;
 default:
  break;
}

very simple. why ASCII for a simple algo.? Why make it complex if you can make it simple and effective.?

This question has already been answered. Start a new discussion instead.