Hi everyone, this is my first post on this site and I was wondering if anyone could help me with this. I'm sorry if my coding style is strange, I'm relatively new at this.
The program is supposed to prompt the user to guess a number between 1 and 1000, and tell them if they're too high or low after each guess.
That part works fine, though after they've won it prompts the user if they want to play again. The problem is, it prompts, goes to the switch's default case (Invalid Entry), and prompts again, without giving the user any chance to input in between. Any suggestions would be greatly appreciated. Thanks!

#include "stdafx.h"
#include "iostream"
#include "time.h"

enum Status {NEW, CONTINUE, QUIT, WIN};

int number, guess;

enum Status gameStatus;

void newGame();

int main(void)
{

srand((unsigned)time(NULL)); 

newGame();

do{

	printf_s("Guess a number 1-1000: ");
	scanf_s("%d", &guess);

if (guess == 1001){printf_s("%d\n", number);} //for debug 

if (guess == number){

	printf_s("You guessed it!\n");
	gameStatus = WIN;

}else if(guess > number && guess <= 1000){

	printf_s("Too high!\n");
	gameStatus = CONTINUE;

}else if(guess < number && guess > 0){

	printf_s("Too low!\n");
	gameStatus = CONTINUE;

}else{

	printf_s("Invalid entry\n");

} //end if

while(gameStatus == WIN){

	printf_s("Do you want to play again? (Y/N): ");

	guess = getchar();

	switch(guess){

		case 'y':
		case 'Y':
			newGame();
			break;
		case 'n':
		case 'N':
			gameStatus = QUIT;
			break;
		case '\t':
		case '\n':
		case ' ':
			break;
		default:
			printf_s("Invalid entry.");

		} //end switch

	} //end while loop

}  while(gameStatus != QUIT); //end do loop

	system("PAUSE");
	return 0;

} //end main

void newGame(){

	guess = -1;

	gameStatus = NEW;

	number = 1 + (rand() % 1000);

}

Hi everyone, this is my first post on this site and I was wondering if anyone could help me with this. I'm sorry if my coding style is strange, I'm relatively new at this.
The program is supposed to prompt the user to guess a number between 1 and 1000, and tell them if they're too high or low after each guess.
That part works fine, though after they've won it prompts the user if they want to play again. The problem is, it prompts, goes to the switch's default case (Invalid Entry), and prompts again, without giving the user any chance to input in between. Any suggestions would be greatly appreciated. Thanks!

#include "stdafx.h"
#include "iostream"
#include "time.h"

enum Status {NEW, CONTINUE, QUIT, WIN};

int number, guess;

enum Status gameStatus;

void newGame();

int main(void)
{

srand((unsigned)time(NULL)); 

newGame();

do{

	printf_s("Guess a number 1-1000: ");
	scanf_s("%d", &guess);

if (guess == 1001){printf_s("%d\n", number);} //for debug 

if (guess == number){

	printf_s("You guessed it!\n");
	gameStatus = WIN;

}else if(guess > number && guess <= 1000){

	printf_s("Too high!\n");
	gameStatus = CONTINUE;

}else if(guess < number && guess > 0){

	printf_s("Too low!\n");
	gameStatus = CONTINUE;

}else{

	printf_s("Invalid entry\n");

} //end if

while(gameStatus == WIN){

	printf_s("Do you want to play again? (Y/N): ");

	guess = getchar();

	switch(guess){

		case 'y':
		case 'Y':
			newGame();
			break;
		case 'n':
		case 'N':
			gameStatus = QUIT;
			break;
		case '\t':
		case '\n':
		case ' ':
			break;
		default:
			printf_s("Invalid entry.");

		} //end switch

	} //end while loop

}  while(gameStatus != QUIT); //end do loop

	system("PAUSE");
	return 0;

} //end main

void newGame(){

	guess = -1;

	gameStatus = NEW;

	number = 1 + (rand() % 1000);

}

You need to flush the stream (eg. fflush(stdin)) after calling scanf().

commented: Solved my problem perfectly! +0

You may place it at line 24.

That worked perfectly! Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.