C++ Programming
04-23-2009, 10:41 AM
I've got an assignment that I've been struggling with all day, and after re-doing the program several times I believe I've narrowed down my problem to this:
#include
#include
int random;
int guess;
char play = 'y';
int game = 1;
int main()
{
while(game == 1)
{
if(play == 'y')
{
random = 1 + rand() % 1000;
printf("\nI have a number between 1 and 1000.\nCan you guess my number?\nType your first guess:");
play = 'v';
}
scanf("%i",&guess);
if(guess == random)
{
printf("\nExcellent! You guessed the number!\nWould you like to play again?(y/n)");
scanf("%c",&play);
if(play == 'n')
{
game = 0;
}
}
else
{
if(guess < random && guess != random)
{
printf("\nToo low, try again!");
}
if(guess > random && guess != random)
{
printf("\nToo high, try again!");
}
}
}
return 0;
}
When I enter the if(guess == random){} block and answer the (y/n) question, the program falls apart. No matter what I seem to do, it'll shut down, stop functioning, or repeat that block once more before it moves on to what it's supposed to do. I don't understand what I'm doing wrong.
#include
#include
int random;
int guess;
char play = 'y';
int game = 1;
int main()
{
while(game == 1)
{
if(play == 'y')
{
random = 1 + rand() % 1000;
printf("\nI have a number between 1 and 1000.\nCan you guess my number?\nType your first guess:");
play = 'v';
}
scanf("%i",&guess);
if(guess == random)
{
printf("\nExcellent! You guessed the number!\nWould you like to play again?(y/n)");
scanf("%c",&play);
if(play == 'n')
{
game = 0;
}
}
else
{
if(guess < random && guess != random)
{
printf("\nToo low, try again!");
}
if(guess > random && guess != random)
{
printf("\nToo high, try again!");
}
}
}
return 0;
}
When I enter the if(guess == random){} block and answer the (y/n) question, the program falls apart. No matter what I seem to do, it'll shut down, stop functioning, or repeat that block once more before it moves on to what it's supposed to do. I don't understand what I'm doing wrong.