2

When my code checks if an input is an integer or string it goes into an infinite loop of outputting "invalid input" and "guess a number between 0-9" without giving the user a chance to input something new.

the code below is what I have

// Created on: Oct 2023    
// This program allows the user to guess a number between 0-9  
#include <stdlib.h>  
#include <stdio.h>  
#include <time.h>  

int main() {  
    // this function allows the user to guess a number  
    // and the program decides if the user is correct  

    unsigned int seed = time(NULL);  
    int randomNumber = rand_r(&seed) % 9;  
    int num = 0;  
    int scanerrorcode = 0;  
    
    while (1)  
    {  
        printf("\nGuess a number between 0-9: ");  
        scanf("%d", &num);
        if (num < 0 || num > 9) {
            printf("\n%d is not between 0-9", num);
        } else if (num == randomNumber) {
            printf("\nYou guessed correctly!");
            break;
        } else if (num > randomNumber) {  
            printf("\nYou guessed too high!");  
        } else if (num < randomNumber) {  
            printf("\nYou guessed too low!");  
        } else {  
            printf("\nError, %d is not a number", num);  
        }  
    }  

    printf("\nDone.");  
}

I am looking for a way to make this game using only a while loop and a break statement. The code should keep asking the user for an input until the random number is guessed

2
  • Sorry, I cannot duplicate your error. The code works for me. Specifically, it keeps looping and asking me for a number until I enter the correct number. When I enter the correct number, it prints "Done." and exits. Commented Nov 6, 2023 at 2:02
  • "checks if an input is an integer or string" No it does not. scanf() isn't satisfied when the mischievous user enters "foobar" so it leaves that in the input buffer only to find it again and again and again. Learn to use fgets() and strtol() to both empty the buffer in one operation and to verify the user's input is what the program needs to work correctly. Move away from scanf(). It's not for beginners... Commented Nov 6, 2023 at 2:23

2 Answers 2

0

I tried out your code and indeed, the program did get stuck in an endless loop if some characters were entered as data. In viewing your code, it looks like it's time for you to familiarize yourself with conventional character and string functionality (e.g. checking if a character is a digit, converting strings to integers, and so forth).

With that in mind, following is a refactored version of your code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <ctype.h>

int main() {
    // This function allows the user to guess a number
    // and the program decides if the user is correct

    int i;
    unsigned int seed = time(NULL);
    int randomNumber = rand_r(&seed) % 9;
    char num[5];

    while (1)
    {
        printf("Guess a number between 0-9: ");
        i = scanf("%s", num);                                   /* Input user entry into a character array                          */

        for (i = 0; i < strlen(num); i++)                       /* Ensure no non-digits are entered in the for loop test            */
        {
            if ((isdigit(num[i]) == 0))
                break;
        }

        if ((isdigit(num[i]) == 0) && (i < strlen(num))) {
            printf("Error, %s is not a number\n", num);       /* Move non-digit test as the first test                            */
            continue;
        }

        i = atoi(num);                                          /* Use conventional string function to convert string to an integer */

        if ((i < 0) || (i > 9)) {
            printf("%d is not between 0 - 9\n", i);
        } else if (i == randomNumber) {
            printf("You guessed correctly!\n");
            break;
        } else if (i > randomNumber) {
            printf("You guessed too high!\n");
        } else {
            printf("You guessed too low!\n");
        }
    }

    printf("Done.\n");

    return 0;
}

Comments have been added, but here are some key points.

  • the "string.h" and "ctype.h" include files have been added to utilize standard character manipulation.
  • a character array is utilized in lieu of an integer for data entry in the "scanf" function (note that there is no ampersand).
  • the character array entry is then validated, making sure, only digits were entered, that the value entered is within the guess range, and then where the guess falls in relation to the random number.

Following was a test run at the terminal.

craig@Vera:~/C_Programs/Console/Guess/bin/Release$ ./Guess 
Guess a number between 0-9: huh
Error, huh is not a number
Guess a number between 0-9: 11
11 is not between 0 - 9
Guess a number between 0-9: 5
You guessed too high!
Guess a number between 0-9: 2
You guessed too low!
Guess a number between 0-9: 3
You guessed correctly!
Done.

The take away from this is probably reference some good tutorial literature on the usage of string and character functions.

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

Comments

-1

You are getting garbage values when you try and enter a value that is not 0 to 9 (so characters like a, b , c etc) You need to get it as a char and convert it to the asci ii value by applying an offset.

int main() {  
unsigned int seed = time(NULL);
int randomNumber = rand_r(&seed) % 9;
int scanerrorcode = 0;

while (1)
{
    int num = 0; //need to reset ever time we loop
    char ch;
    printf("\nGuess a number between 0-9: ");  
    scanf(" %c", &ch);  

    //ascii 2 char for 0 - 9 is 48 - 57! https://www.ascii-code.com/
    printf("The ASCII value of %c is %d \n", ch, ch);

    // To make a digit
    num = num * 10 + (ch - 48);

    //Hey look it's valid!
    if (ch >= 48 && ch <= 57) {
        printf("\n%d is valid and between 0-9", num);
    }

    //not valid!
    if (ch> 57 || ch < 48){
        printf("\n%d is NOT between 0-9!", num);
    } else if (num == randomNumber) {
        printf("\nYou guessed correctly!");
        break;
    } else if (num > randomNumber) {
        printf("\nYou guessed too high!");
    } else if (num < randomNumber) {
        printf("\nYou guessed too low!");
    } else {
        printf("\nError, %d is not a number", num);
    }
}  

printf("\nDone.");

}

2 Comments

'Twould be better if you didn't encourage use of magic numbers in an answer. What's wrong with '0' instead of 48, likewise for '9'...
I'm assuming this is a project for a new student. He will be ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.