1

I've created a simple guessing game program. My problem is I'm not sure how to loop the program back to the beginning of the program once the user has guessed a wrong number. I want the program to continue asking the user for a number until he gets it right. Can someone help me?

#include <stdio.h> int main (void) { int value = 58; int guess; printf("Please enter a value: "); scanf("%i", &guess); if (guess == value) { printf("Congratulations, you guessed the right value"); } else if (guess > value) { printf("That value is too high. Please guess again: "); scanf("%i", &guess); } else if (guess < value) { printf("That value is too low. Please guess again: "); scanf("%i", &guess); } return 0; } 
1

5 Answers 5

5

This looks like a great spot for a while loop and a break statement. You can use the while loop like this to loop infinitely:

while (true) { /* ... /* } 

Then, once some condition becomes true and you want to stop looping, you can use the break statement to exit the loop:

while (true) { /* ... */ if (condition) break; /* ... */ } 

This way, you can break out of the loop when the user guesses correctly.

Alternatively, you can use a do ... while loop whose condition checks whether the loop should exit:

bool isDone = false; do { /* ... */ if (condition) isDone = true; /* ... */ } while (!isDone); 

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

Downvoter- Could you please comment on what's wrong with this answer? I would love to improve it if possible.
Also note that you have to #include <stdbool.h> when using false/true.
That's as clean as it gets.
1

There are a number of looping constructs in the C syntax. They are:

  • for()
  • while()
  • do/while()

Either of these should be simple to look up in whatever reference material you're using, and either is possible to use to solve this problem.

2 Comments

Don't forget goto & setjmp()!
"goto & setjmp()" & recursion ... in C, main can be called recursively :)
1

Try this:

printf("Please enter a value: "); do { scanf("%i", &guess); if (guess == value) {         printf("Congratulations, you guessed the right value");     }     else if (guess > value) {         printf("That value is too high. Please guess again: ");     }     else if (guess < value) {         printf("That value is too low. Please guess again: "); } while (guess != value); 

Comments

0

use do { /*your code*/ } while(condition);

do { /*your code*/ char wannaPlayAgain; wannaPlayAgain = getchar(); } while(wannaPlayAgain=='y'); 

Of course you should fix it in case people enter Y rather then y, but the point is you need to wrap your program in a do while loop (it will execute at least once) or a while loop (get the initial value before you enter the condition) with an initial start condition.

Comments

0

Your expected program is

#include <stdio.h> void main (void) { int value = 58; int guess; do { printf("please enter a value : "); scanf("%i", &guess); if(guess > value) printf("this value is too big, "); else if(guess < value) printf("this value is too small, "); }while(guess != value); printf("Congradulation You Guessed The Right Number. \n"); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.