do while looping problems

Hi all, this is a very novice post (and my very first post), but I am having a problem getting my do while loop to work. I think I have it set up correctly, but every time I run the program it ends instead of looping.

Here is the code the loop I am having trouble with is the response part. Everytime I put in "Y" it ends the program. Thanks for the help in advanced!

#include <iostream>
#include <ctime>
using namespace std;
int main (void) {
int compnum; //Random number generated by computer
int guess; //player's guess
int score=0; //player's score
char response='y'; //player's response to play again
srand(unsigned int(time(NULL)));
compnum = (rand() % 10) +1;
do
{
do
{
cout << "Enter Random Number:";
cin >> guess;
if (guess==compnum) {
cout << " You are correct!" << compnum;
score=score+5;
}
else {
if (guess > compnum) {
cout << " Your guess is too high";
score=score-1;
if (score<0) {
score=0;
}
}
else {
cout << " Your guess is too low";
score=score-1;
if (score<0) {
score=0;
}
}
}
cout << " Your guess is" << guess;
cout << "Would you like to play again? (Y/N)";
cin >> response;
}while (guess!=compnum);
}while ((response=='y')||(response=='Y'));
if ((response=='n')||(response=='N'))
{
cout << " Your score is:" << score;
}
system("PAUSE");
}
Do you mean to just give the user one shot at guessing the number or are they supposed to be able to guess until they get the right one, then they get the option to play again?

srand is in <cstdlib> - are you including that?

(I put in the forum's code tags to make it easier to read here)

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

int main (void) 
{

	int compnum; //Random number generated by computer
	int guess; //player's guess
	int score=0; //player's score
	char response='y'; //player's response to play again

	srand(unsigned int(time(NULL)));
	compnum = (rand() % 10) +1;

	do{
	
		do{
		cout << "Enter Random Number:";
		cin >> guess;
		if (guess==compnum) 
		{
			cout << " You are correct!" << compnum;
			score=score+5;
		}
		else 
		{
			if (guess > compnum) 
			{
				cout << " Your guess is too high";
				score=score-1;
				if (score<0) 
				{
					score=0;
				}
			}
			else 
			{
				cout << " Your guess is too low";
				score=score-1;
				if (score<0) 
				{
					score=0;
				}
			}
		} 

		cout << " Your guess is" << guess;
		cout << "Would you like to play again? (Y/N)";
		cin >> response;
		}while (guess!=compnum); 

	}while ((response=='y')||(response=='Y'));


	if ((response=='n')||(response=='N')) 
	{
		cout << " Your score is:" << score;
	} 

	system("PAUSE");
}
Right now as it is setup, the user gets as many shots at the right number as they want. the problem I am having is the is that the "would you like to play again" part isnt working. everytime I input 'y' or 'n' it just closes the program... no idea why!

edit: thanks for formatting that!!
Last edited on
OK - so if they aren't supposed to see

cout << "Would you like to play again? (Y/N)";

until they've guessed correctly - then I would pull that line out of the inner while loop that controls whether they've guessed the right number, and put it in the outer while loop that controls whether they want to continue playing.


You don't need the extra sets of parentheses here.

((response=='y')||(response=='Y'))

(response=='y' || response=='Y')


I'm getting a compile error on this - but I guess it seems to be working for you?
srand(unsigned int(time(NULL))); I wouldn't normally put in the unsigned int when actually calling the function.
Last edited on
Thanks I got it now!
Topic archived. No new replies allowed.