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;
}
Niklas B.
  • 92,950
  • 18
  • 194
  • 224
user1064913
  • 335
  • 2
  • 8
  • 19

5 Answers5

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!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
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.

unwind
  • 391,730
  • 64
  • 469
  • 606
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);
ikuramedia
  • 6,038
  • 3
  • 28
  • 31
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");
}
Mohanraj
  • 4,056
  • 2
  • 22
  • 24
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.

JonH
  • 32,732
  • 12
  • 87
  • 145