Home > other >  Fixing do-while loop problem and how to add do you want to play again?
Fixing do-while loop problem and how to add do you want to play again?

Time:05-29

I started C courses today and teacher assigned to do:

Guess the number and if the number is higher say it is higher but if it is lower say it is lower and count every time you guess AND if you guess it ten times already then say do you want to try again?

I don't know why my code is stop when I just play it only 1 time and how to do the "do you want to play again?"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    int x;
    char name[20], result;
    int count = 0;
    int number;

    srand(time(NULL));
    x = rand() % 100   1;

    printf("What's your name :");
    scanf("%s", &name);
    printf("Hello!! %s\n", name);
    printf("Guess the number : ");
    scanf(" %d", &number);
    do {
        count  ;
        if (x > number) {
            printf("This is your count : %d\n",count);
            printf("The number is higher\n");
        } else
        if (x < number) {
            printf("This is your count : %d\n",count);
            printf("The number is lower\n");
        } else
        if (x == number) {
            printf("\nYou're right!!, the number is %d",x);
        }
    } while (count == 10);
}

CodePudding user response:

The code allows only 1 try because the test while (count == 10) is false at the end of the first iteration. You should have while (count < 10).

You should move the input call scanf(" %d", &number); inside the loop.

Also note that you should break from the loop if the number was found.

For the Do you want to play again? part, you could wrap this code in another loop.

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    char name[20] = "", ch;

    srand(time(NULL));
    printf("What's your name: ");
    scanf("s", &name);
    printf("Hello!! %s\n", name);

    for (;;) {
        int x = rand() % 100   1;
        int number;
    
        for (int count = 1; count <= 10; count  ) {
            printf("Enter your guess: ");
            if (scanf(" %d", &number) != 1)
                break;
            if (x > number) {
                printf("This is your count: %d\n",count);
                printf("The number is higher\n");
            } else
            if (x < number) {
                printf("This is your count: %d\n",count);
                printf("The number is lower\n");
            } else
            if (x == number) {
                printf("You're right!!, the number is %d\n",x);
                break;
            }
        }
        printf("Do you want to play again? ");
        if (scanf(" %c", &ch) != 1)
            break;
        if (ch != 'y' && ch != 'Y')
            break;
    }
    return 0;
}

CodePudding user response:

The loop condition in the line

} while (count == 10);

is wrong, because it will be false after the first loop iteration. It would be more meaningful to write

} while (count < 10);

Also, you probably want to put the lines

printf("Guess the number : ");
scanf(" %d", &number);

inside the loop, so that they get executed more than once. Otherwise, you will be processing the same user guess 10 times, which is not what you want.

Another issue is that you don't want the loop to always run 10 times. You only want it to run 10 times if the user hasn't guessed the number. If the user has guessed the number, you want to break out of the loop immediately, without waiting for count to reach 10. For this, you can use the break statement.

CodePudding user response:

I'm a VB guy mostly but can you use while count <= 10?
Perhaps Do Until?

  • Related