Why does my program not generate a new number even though i specify srand(time(0))???

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
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <string>
using namespace std;
int main(){

    int number = rand()%100+1;
    int guess;
    int trys;
    string comp_guess;
    string answer;
    
    srand(time(0));
	
    cout<<"Type \"computer\" to guess the computers number."<<endl;
	cout<<"Type \"user\" to make the computer guess your number."<<endl;
	cin>>comp_guess;
	
    if(comp_guess == "computer"){
		cout<<"Guess my hidden number"<<endl;
		cin>>guess;
		
        for (int i = 0; i < 101; i++){
			if (guess == number){
				cout<<"CONGRATULATIONS!!! YOU WIN!!!"<<endl;
				cout<<"It took you "<<trys + 1<<" trys to guess correctly."<<endl;
                break;
			}
			else if (guess < number){
				cout<<"Too Low"<<endl;
				cin>>guess;
                trys++;
			}
			else if (guess > number){
				cout << "Too High"<<endl;
				cin>>guess;
                trys++;
			}
		}
    }
}



My problem is that i always get 8 as the correct number. Why is this happening????


Type "computer" to guess the computers number.
Type "user" to make the computer guess your number.
computer
Guess my hidden number
8
CONGRATULATIONS!!! YOU WIN!!!
It took you 1 trys to guess correctly.
Last edited on
closed account (Dy7SLyTq)
call srand before you call rand. srand seeds it and gives you a random one
That doesn't work. this is suppose to be for the bracketing search exercise and i can't get it to select a different number besides 8. i have been at it for more than a day thinking of what to do and i don't know what it is. this is really aggravating can someone please help.
closed account (Dy7SLyTq)
post your code
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
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <string>
using namespace std;

int main(){

    int number = rand()%100+1;
    int guess;
    int control_num = 101;
    int control = -1;
	int trys;
    string comp_guess;
    string answer;
    string high = "higher";
    string low = "lower";

    
    cout<<"Type \"computer\" to guess the computers number."<<endl;
	cout<<"Type \"user\" to make the computer guess your number."<<endl;
	cin>>comp_guess;

    if(comp_guess == "computer"){
		srand(time(0));
        cout<<"Guess my hidden number"<<endl;
		cin>>guess;

        for (int i = 0; i < 101; i++){
			srand(time(0));
            if (guess == number){
				cout<<"CONGRATULATIONS!!! YOU WIN!!!"<<endl;
				cout<<"It took you "<<trys + 1<<" trys to guess correctly."<<endl;
                break;
			}
			else if (guess < number){
				cout<<"Too Low"<<endl;
				cin>>guess;
                trys++;
			}
			else if (guess > number){
				cout << "Too High"<<endl;
				cin>>guess;
                trys++;
			}
		}
    }
}


i called it srand() right before rand().
Last edited on
closed account (Dy7SLyTq)
no you didnt. you called it when they enter a guess not when you call rand. put one call to srand before you call randd
ok wait. Now i feel stupid. i didn't realize i called rand in the very beginning thanks.
closed account (Dy7SLyTq)
no problem
Topic archived. No new replies allowed.