random numbers.

hello everyone im just messsing around making a program, and ive stumbled upon a problem. bassicly i wanna no if i can make the program generate a different number everytime you guess the number right and want to play again. it always generates the same number, thank you

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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;


int main()
{
	srand(time(0));

	int compNum = rand()%10+1, userNum;
	char userChoice;

	cout << "Lets play a guessing game, im a number between 1 and 10\n";
	do
	{
	cout << "Input your number: ";
	cin >> userNum;

	if(userNum == compNum)
	{
		cout << "You guess right, wanna play again?\nY or N\n";
		cin >> userChoice;
	}
		cout << "wrong try again? Y or N" << endl;
		cin >> userChoice;
		system("cls");
	 if(userChoice == 'n' || userChoice == 'N')
	 {
		 cout << "Good Bye\n";
		 break;
	 }
	}while(userChoice == 'y' || userChoice == 'Y');
	

	system("pause");
	return 0;
}


You should probably put line 11 inside of the do loop =p otherwise you are generating the random number outside of the loop I actually made one of these games about a week ago I would post the code but I'm watching a movie on Windows 7 and my file I made on my knoppix partition on my external hard drive so can't access it right now but if you would like to see my code I can send you it tomorrow.
thatd be cool thanks man, i would love to look at it, whenever is fine.
im change some stuff up so ima send you the new code, i encountered a new problem, now when i get the number right once it will jsut incrament Win once and thats it, i put it where i could see the number but anyways. could you look at it?
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;


int main()
{
	//Declaring
	srand(time(0));

	
	char userChoice;

	cout << "Lets play a guessing game, im a number between 1 and 10\n";
	////////////////Guess
	do{
	int compNum = rand()%10+1, userNum, Win=0, Loss=0;
	cout << compNum << endl;
	cout << "Input your number: ";
	cin >> userNum;
	
	if(userNum == compNum)
	{
		cout << "You guess right, wanna try again?Y or N";
		userChoice == ' ';
		Win++;
		cin >> userChoice;
		system("cls");
	}
	else
	{
		cout << "wrong try again? Y or N" << endl;
		cin >> userChoice;
		Loss++;
		system("cls");
	}
	 if(userChoice == 'n' || userChoice == 'N')
	{
		 cout << "Wins: " << Win << endl;
		 cout << "Losses: " << Loss << endl;
		 break;
	}
	}while(userChoice == 'y' || userChoice == 'Y');
	
	
	system("pause");
	return 0;
}
I think line 26 is unnecessary, and the reason you are getting 1 is because each time the loop runs you are setting the wins and losses to 0, move that outside of the loop. You only really need the random number inside of the loop I would suggest this:
1
2
3
4
5
6
7
8
9
10
11
int compNum, userNum, win = 0, loss = 0;
do {
compNum = //random number
//stuff
/stuff
win++;
//stuff
loss++
//stuff
//stuff
} 
Last edited on
Here's my code its probably more complicated than it needs to be but if you want to use it go ahead =p
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <stdlib.h>
void mainmenu(std::string);
void start(std::string);
void check(std::string&);
void convert(std::string, unsigned int&);
void rand(unsigned int, unsigned int, unsigned int&);
void guessed(std::string, unsigned int, char&, unsigned int&, unsigned int&);
void quit(std::string);

int main()
{
	std::string name;
	do {
		std::cout << "What is your name? " << std::flush;
		std::getline(std::cin,name);
		for(unsigned int i = 0; i<name.size(); i++) if(!isalpha(name[i])) name = "false";
		if(name == "false") std::cout << "Invalid name.\n" << std::flush;
	} while(name == "false");
	std::cout << "I'm glad you could play my game " << name << '\n' << std::flush;
	mainmenu(name);
	return(0);
}

void mainmenu(std::string a)
{
	std::string input;
	do {
		std::cout << a << " please enter \"P\" to play.\n" << a << " please enter \"Q\" to quit.\nChoice: " << std::flush;
		std::getline(std::cin,input);
		for(unsigned int i = 0; i<input.size(); i++) input[i] = toupper(input[i]);
		if(input != "P" && input != "Q") std::cout << "Invalid choice " << a << ".\n" << std::flush;
	} while(input != "P" && input != "Q");
	switch(input[0]){
		case 'P': start(a); break;
		case 'Q': quit(a); break;
	}
}

void start(std::string a)
{
	std::string low, high, guess;
	unsigned int l, h, r, i = 0;
	char g;
	do {
		std::cout << a << " what range would you like?(ex. 1-100)...\nLowest Value: " << std::flush;
		std::getline(std::cin,low);
		std::cout << "Highest Value: " << std::flush;
		std::getline(std::cin,high);
		check(low);
		check(high);
		convert(low, l);
		convert(high, h);
		if(low == "false" || high == "false" || l > h) std::cout << "Invalid range " << a << ".\n" << std::flush;
	} while(low == "false" || high == "false" || l > h);
	
	rand(l, h, r);
	
	do {
		i++;
		std::cout << a <<" please guess a number between " << l << " and " << h << "\nGuess " << i << ": " << std::flush;
		std::getline(std::cin,guess);
		check(guess);
		if(guess != "false") guessed(guess, r, g, l, h);
	    else std::cout << "Invalid guess " << a << '\n' << std::flush;
	} while (g != 't');
	if(i != 1) std::cout << "\nGreat job " << a << "!\nIt only took you " << i << " tries to guess the random number.\n\n" << std::flush;
	else std::cout << "\nGreat job " << a << "!\nIt only took you " << i << " try to guess the random number.\n\n" << std::flush;
	mainmenu(a);
}

void check(std::string &a)
{
	for(unsigned int i = 0; i<a.size(); i++) if(!isdigit(a[i])) a = "false";
}

void convert(std::string a, unsigned int &b)
{
	b = atoi(a.c_str());
}

void rand(unsigned int a, unsigned int b, unsigned int &c)
{
	srand(time(0));
	c = rand() % (b - a + 1) + a;
}

void guessed(std::string a, unsigned int b, char &c, unsigned int &d, unsigned int &e)
{
	unsigned int guess;
	convert(a, guess);
	if(guess < d || guess > e) std::cout << "Invalid guess.\n" << std::flush;
	if(guess > b && guess < e){ std::cout << "You guessed too high.\n" << std::flush; e = guess - 1; }
	if(guess < b && guess > d){ std::cout << "You guessed too low.\n" << std::flush; d = guess + 1; }
	if(guess == b) c = 't';
}

void quit(std::string a)
{
	std::cout << "Good bye " << a << ".\nPlease play again!" << std::flush;
}

yeah i relized that about line 26 and thanks i figured out that that was the reason too, im having another problem and i need some advice about it if you would, i figured that making you guess a number can be kind of hard cause the program now when you fail to guess it right and try again it changes the number i wanted only when you get it right, so i tryed to add a hint system this is what i got, im kind of still new so tell me if its stupid, im getting uninitialzed errors from this.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;


int main()
{
	//Declaring
	srand(time(0));
	char userChoice,userHint;
	int userNum, Win=0, Loss=0;
	cout << "Lets play a guessing game, im a number between 1 and 10\n";
	
	do{
	int compNum = rand()%10+1; 
	cout << compNum << endl;
	cout << "1: Input\n2: Hint\n";
	cin >> userHint;
	if(userHint == 1)
	{
	cout << "Input your number: ";
	cin >> userNum;
	
	if(userNum == compNum)
	{
		cout << "You guess right, wanna try again?Y or N";
		cin >> userChoice;
		Win++;
		system("cls");
	}
	else
	{
		cout << "wrong try again? Y or N" << endl;
		cin >> userChoice;
		Loss++;
		system("cls");
	}
	 if(userChoice == 'n' || userChoice == 'N')
	{
		 cout << "Wins: " << Win << endl;
		 cout << "Losses: " << Loss << endl;
		 break;
	}
	}
	if(userHint == 2)
	{
	 if(compNum == 1)
	 {
		 cout << " a - b = Guess if a is eqaul to 10,000 and b is equal to 10 / 1";
	 }
	 else if(compNum == 2)
	 {
		 cout << "50 + 49 - 97";
	 }
	 else if(compNum == 3)
	 {
		 cout << "3 * 3 * 3 / 9 / 3";
	 }
	 else if(compNum == 4)
	 {
		 cout << "3 + 100 - 99";
	 }
	  else if(compNum == 5)
	 {
		 cout << "10 / 2";
	 }
	  else if(compNum == 6)
	 {
		 cout << "1 + 5";
	 }
	   else if(compNum == 7)
	 {
		 cout << "3 + 3 + 1";
	 }
	   else if(compNum == 8)
	 {
		 cout << "4 * 2";
	 }
	    else if(compNum == 9)
	 {
		 cout << "19 - 10";
	 }
	    else if(compNum == 10)
	 {
		 cout << "9 + 1";
	 }
	}
	}while(userChoice == 'y' || userChoice == 'Y');
	
	
	system("pause");
	return 0;
}
@giblit

wow dude, i wish i could understand that, thats really complicated for me im trying to understand it but thank you ima save it in notepad and study it. thank you for your advice !!!!!!!!
not sure on the hint thing and did you try and compile the code I have if that is something like you want I could try and explain it more in details on the parts you don't understand. oh and I probably could of done it with out all the functions but I was testing out references at the time of creating it lol
closed account (18hRX9L8)
makeitloud wrote:
wow dude, i wish i could understand that, thats really complicated for me im trying to understand it but thank you ima save it in notepad and study it. thank you for your advice !!!!!!!!

giblit, you should put in comments so it is easier for him to understand
Last edited on
Topic archived. No new replies allowed.