samedi 28 mai 2022

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

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



Aucun commentaire:

Enregistrer un commentaire