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)
#include <iostream>
#include <ctime>
usingnamespace 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(unsignedint(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!
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(unsignedint(time(NULL))); I wouldn't normally put in the unsigned int when actually calling the function.