0

I need to make a guessing game in C++, and everything works except that srand(time(0)) doesn't reset the number after the user wants to play again. I also can't use std libraries.

Nothing I have done has worked so far. Am I doing the while loops wrong?

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

int main()
{
  //Initialize variables
  int input = 0;
  int playing = 1;
  char yninput[2];
  int count = 1;
  //While the player is playing the game
  while (playing == 1) {
    srand(time(0));
    int num = rand() % 101;
    //While the player hasn't guessed the number
    while (input != num) {
     //Prompt the player
       cout << "Enter your guess" << endl;
      cout << num << endl;
      cin >> input;
      //If the guess is greater than the number
      if (input > num) {
    cout << " Your guess is too high!" << endl;
   count++;
      } 
      //If the guess is less than the number
      else if (input < num) {
    cout << " Your guess is too low!" << endl;
    count++;
      }
      //If the player guesses the correct number
      else {
    cout << " You have guessed the number! It took you " << count << " 
guess(es)! Would you like to play again?" << endl;
    //Ask the play if they want to play again
    cin >> yninput[2];
    //If the player doesn't want to play again quit the program
    if (yninput[2] == 'n') {
      playing = 0;
      input = num;
        }
    //If the player wants to play again restart the program and 
     randomize the number
    else if (yninput[2] == 'y') {
       input = 0;
      count = 1;
         }
      }
    }
  }
}
Rorschach
  • 31,301
  • 5
  • 78
  • 129
madaniloff
  • 57
  • 9
  • 1
    What do you mean by _I also can't use std libraries_? By including `iostream` and `ctime`, you are using std libraries – Amadeus Sep 24 '19 at 02:03
  • `srand` is not something you normally want to call more than once. What you're doing here is telling the RNG to start serving up numbers based on the current time. If two iterations of the loop are too close together, within the same second, you'll get the same generated numbers. If they are more than a second apart you'll get a different set of random numbers. – user4581301 Sep 24 '19 at 02:09
  • If you want the same set of numbers, you should either use an constant number (always the same set of numbers. Period.) or cache the time at the start of the program and reuse this time on all calls to `srand` (same set of numbers per process--unless you run the program multiple times in quick succession. See above) – user4581301 Sep 24 '19 at 02:09
  • The compiler may be trying to point out `input != num;`. Whatever you wanted this line to do, it's not doing it. I can guarantee this without knowing what it's supposed to do because it doesn't do anything. If I knew what this line was supposed to do I could offer an alternative. – user4581301 Sep 24 '19 at 02:12
  • Unrelated: when working with loops, make darn sure you have the indentation well organized. If it isn't, the bugs cometh. – user4581301 Sep 24 '19 at 02:13
  • @user4581301 How do I get it so that when the user presses 'y' and the game resets that the random number also changes? As for input != num, I meant for it to clear the input, so I just changed it to input = 0; – madaniloff Sep 24 '19 at 02:23
  • @doug I tried srand(time(NULL)) and I get the same result – madaniloff Sep 24 '19 at 02:25
  • https://en.cppreference.com/w/cpp/chrono/c/time argument to `time` must be `nullptr` or `time*` Could be that 0 is expanded to `nullptr` – doug Sep 24 '19 at 02:28
  • @doug Unfortunately 0 looks exactly the same as a good ol' C `NULL` by the time the preprocessor is done, so `time` will gleefully eat 0 and treat it as `nullptr`. You can imagine how many stupid bugs this cause and why C++ added and recommends using `nullptr`. But for legacy reasons `NULL` lives on and this code is 100% valid, if silly looking. – user4581301 Sep 24 '19 at 02:31
  • Semi-duplicate: [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – user4581301 Sep 24 '19 at 02:33
  • @user4581301 Yep. Your right. Been too long since I've had to deal with NULL. – doug Sep 24 '19 at 02:33
  • @user4581301 — in C, `NULL` was typically `(void*)0`. In C++ `void*` can’t be implicitly converted to other pointer types, and the easiest solution was to change `NULL` to `0`. – Pete Becker Sep 24 '19 at 03:11
  • I stand kerected. – user4581301 Sep 24 '19 at 03:12
  • Possible duplicate of [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – phuclv Sep 24 '19 at 14:42

3 Answers3

2

As @user4581301 has pointed out, you shouldn't call srand(time(0)) more than once, as it will reset the random seed according to the current system time. If srand(time(0)) is called in rapid succession, the very big number that it will take as a seed (which I believe is the current epoch time) will be sufficiently close to the previous call that you might not observe significant difference in your RNG.

Simply moving the srand(time(0)); line out of the while loop should do the trick.

  • I moved it out of the while loop (The code has been updated) Although it still is not working. Are there any problems that you see. – madaniloff Sep 24 '19 at 02:40
  • I might have misunderstood something about your problem then. I take it that your code is identical to [this one ?](https://pastebin.com/8su2wVa8) If so then can you give me exemples of your problem ? [here is my output after playing around with it a bit](https://imgur.com/a/kRbfNrL) – Mathis Lamidey Sep 24 '19 at 02:41
  • Oh wow, looks like it works! It looks like the problem is on my end, I think I need to download the C++ libraries for OpenSUSE Linux! Thanks for going through the trouble of testing it and for your help! – madaniloff Sep 24 '19 at 02:46
0

How do I get it so that when the user presses 'y' and the game resets that the random number also changes?

You get the next number in the pseudo random sequence by calling rand without calling srand in between. If you set the random sequence to start from the current timestamp on every iteration, then you get the same number which changes once a second.

I also can't use std libraries.

srand, rand, time, cout and cin are all from the standard library.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

I think @Mathis has already pointed out the solution.

I am just sharing some insight as to how srand and rand are related. Consider the below code snippet:

#include <iostream>
#include <ctime>
int main()
{
    int i = 0;
    // Uncomment below line to generate new set of random numbers
    // on every execution.
    // srand(time(0));   
    while (i < 5)
    {
        std::cout<< rand() % 10 <<std::endl;
    }
}

Let's say the program generates numbers - 5, 7, 3, 0 and 4 on 1st run. If you run the program again, you will see the same set of numbers, i.e, 5, 7, 3, 0 and 4. So, although they are random (pseudo random to be precise), but on every program execution, the order of numbers will be same.

This is the reason we use srand to specify some seed value. Seed is any value which is different on each execution. When we use time(0) as parameter to srand, we make sure that on every program execution, we are providing a new and unique seed. This will make sure that we get truly random set of numbers.

Saket Sharad
  • 392
  • 2
  • 11