guessing game help!

im trying to do this exercise where i have to create a number guessing game. basically let user enter a number and the program will either say too high or too low until the user guess the right number. i think i got the guessing part okay. but i would like to let the user decide if he wants to keep continuing the game or not, and that part doesnt seem to work. could someone help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Ex 6.38 (Guess the Number Game)

#include <iostream>
#include <cstdlib>
using namespace std;

int runtest(int g, int n);

int main (){
    
    int num, guess;
    char ans;
    do{
         srand(time(0));
         num = 1+rand()%1000;
         cout << "Guess a number from 1 to 1000!\n";
         cin >> guess;
         if (guess != num){
                  guess = runtest(guess,num);
         }
         else if (guess == num){
                  cout <<"You guessed a right number!\n"
                       <<"Continue? (y/n)\n";
                  cin >> ans;
         }
         else 
             cout << "error!\n";
    }
    while(ans!='n');
}

int runtest(int g, int n){
    while (g!=n){
          if (g<n){
                   cout << "Too Low! Try again!\n";
                   cin >> g;
          }
          if (g>n){
                  cout << "Too High! Try again!\n";
                  cin >> g;
          }
    }
    return g;
}
oh, i found the problem.

i thought the after the first if, when guess=num, it should still run to the else if (guess==num) to perform the necessary cout, or am i mistaken?

anyway, here is my new code. i guess i kinda fix it, but i feel like im cheating, am i? please correct me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Ex 6.38 (Guess the Number Game)

#include <iostream>
#include <cstdlib>
using namespace std;

int runtest(int g, int n);
char again();

int main (){
    
    int num, guess;
    char ans;
    do{
         srand(time(0));
         num = 1+rand()%1000;
//         cout << num;
         cout << "Guess a number from 1 to 1000!\n";
         cin >> guess;
         if (guess != num){
                  guess = runtest(guess,num);
                  ans = again();
         }
         else if (guess == num){
                  ans = again();
         }
         else 
             cout << "error!\n";
    }
    while(ans!='n');
}

int runtest(int g, int n){
    while (g!=n){
          if (g<n){
                   cout << "Too Low! Try again!\n";
                   cin >> g;
          }
          if (g>n){
                  cout << "Too High! Try again!\n";
                  cin >> g;
          }
    }
    return g;
}
char again(){
     char ans;
     cout <<"You guessed a right number!\n"
          <<"Continue? (y/n)\n";
     cin >> ans;
     return ans;
}      

you can prevent data loss by replacing
srand(time(0))

with
1
2
unsigned int seed = static_cast<int>(time(0));
srand(seed);


just a little hint that's a good practice, it drives me nuts seeing it done that way :P
how does it prevent data loss?

actually i dont really understand that line : srand(time(0)), i just saw it on text book that it could give random number. and what is the difference with the other lines?
closed account (z05DSL3A)
Technically is doesn’t prevent data loss.

What I think Kenner is getting at is this. The time function returns a value of type time_t, this type is either a 32bit or 64bit integer (32bit is deprecated due to the integer value rolling over in the year 2038).

The function srand, takes an unsigned int as its parameter. When you pass the time_t value into srand it is implicitly converted to the required type for the function, this conversion may result in data loss i.e. 64bits do not fit into 32bits so half the bits are lost. Your compiler should warn you of such possible data losses.

static_cast just dose an explicit conversion of the same data types. It in no way prevents data loss, but does stop the compiler warning of it.
Last edited on
Topic archived. No new replies allowed.