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;

}

Yes, a while loop would work, or better yet a do-while.

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?

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');

ok.I got it now.thanks

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.