srand help

Hi. I want to write a program that, for example, picks a number and keeps that value until I guess it and then asks if I want to play again at which point (if i picked yes) it picks a new variable. I can get a rand number but not a new one. I am using xcode for mac. why does this not work:

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
#include <iostream>
#include <ctime>
#include <cmath>
#include <stdlib.h>

using namespace std;

int main()
{
    int randnumb, usrguess, guessplus = 0, usraction = 1;
    
    cout<<"\nWelcome to Aramil's number guessing game! Please pick a number between 1-10.\n";
    
    while(usraction == 1)
    {
        srand((unsigned)time(0));
        randnumb = 10+rand()%1;
        
        while(usrguess != randnumb)
        {
            cout<<"\nWhat is your guess?\n>";
            cin>> usrguess;
            
            if(usrguess > randnumb)
            {
                cout<<"\nThat is too high. Try again.\n";
                
                guessplus++;
            }
            
            if(usrguess < randnumb)
            {
                cout<<"\nThat is too low. Try again.\n";
                
                guessplus++;
            }
        }
        
        if(usrguess == randnumb)
        {
            cout<<"\nCongratulations! You got it in "<< guessplus <<" tries! Do you want to play again?\n1-Yes\n2-No\n>";
            cin>> usraction;
        }
    }
    
    if(usraction == 2)
    {
        cout<<"\nThank you for playing.\n";
        abort();
    }
}
call srand only once at the start of main.
i did that now it doesnt seed randnumb unless I move it out of my while loop and if I do that it wont give it a new value at the start of the loop.
Oh, this is wrong 10+rand()%1;
actually its not, but what the problem is it won't (no matter where i put srand and or randnumb) it wont redeclare its value
It IS the problem. As it is now randnumb has only one possible value.

You also might want to change usrguess to something outside the range when you choose to play again so that the user don't get right before start guessing. guessplus should also be restored.
Im sorry if that sounded disrespectful. I thought you meant how i wrote that line. I know that it only has one value, so where do i put srand and randnumb so that it changes it value at the start of the first loop? and what do you mean by changing usrguess? once again it sounded like you were attacking how I wrote that line so I am sorry. I would be grateful if you would still help me
I don't think you get it. 10+rand()%1; can only be 10 no matter what value rand() return.

what do you mean by changing usrguess?

This is unrelated to your other problem but if you choose to play again and randnumb happens to be the same as before it will jump straight to the "Congratulations" again without having to guess.
Last edited on
thank you based on your help I got it working
Topic archived. No new replies allowed.