0

hey guys I have a question on this guessing game. I want to make this game to repeat. when the guess is correct, i wanted the user to input 'y' or 'n' and if yes it should repeat. How can i do that? Do I have to put the whole program in to another while loop?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
  
	int g;
	int x;
	char a;



	srand (time(NULL));
	g = rand() % 1000+1;

	printf("I have a number between 1 and 1000\nCan you guess my number\?\nPlease   type your first guess\n");

	scanf("%d", &x);

	while (g!=x){

		if (g<x)

			printf("Too high. Try again\n");

		else if (g>x) printf("your guess is less\n");

		scanf("%d", &x);

	}
		printf("Excellent!you guess the number\nWould you like to play again (y or n)?\n");
	    
	
return 0;

}
3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by heshangho
0

ok.thanks. however i need to continue the program just if the user wish to.(by typing yes).So can you help me with that?

0

What I would suggest is that you make a function of everything starting from "int g;" to the end of the while loop. Then in main() call the function inside another loop as

do
{
function_here();
printf("Excellent!you guess the number\nWould you like to play again (y or n)?\n");
scanf("%c",&a);
}while(a=='y'||a=='Y');
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.