0

I am trying to get the user to enter a integer to be later used in the program I want it to force user to enter an input until it is an integer. When I run this code it goes on an infinite loop like so:

Enter smallest operator: Enter smallest operator: Enter smallest operator: Enter smallest operator: Enter smallest operator: Enter smallest operator:..........

I think the issue is with the scanner but I am unsure as to how I should approach it.

    #include <stdio.h>
#include <ctype.h>

int main(int argc, const char * argv[]) {

    char operator;
    int largestOpperator, smallestOpperator;

    printf("Enter The Table Operator (+, -, *, /, %%, or R): ");
    scanf("%c",&operator);

    /*while(!(operator == '+' || operator == '-' || operator == '*' || operator == '/' ||operator == '%' || operator == 'R'))
    {

        printf("Enter Appropriate Table Operator (+, -, *, /, %%, or R): ");
        scanf(" %c",&operator); //requires space as a way to skip the enter when char is scanning!!
    }*/

    do{
        if(!isdigit(smallestOpperator))
            printf("Enter smallest operator: ");
        else{
            printf("Enter an actual number: ");
            scanf(" %d", &smallestOpperator);
        }

    }while(!isdigit(smallestOpperator));


    printf("Enter largest operator: ");
    scanf("%d", &largestOpperator);

    {
        printf("Enter a valid integer: ");
        scanf("%d", &largestOpperator);
    }


    return 0;
}
6
  • Can you post the entire program rather than a snippet? Commented Oct 16, 2018 at 14:50
  • Don't omit { braces } from code blocks. It's a magnet for errors; better just to never do it, and then you won't make those mistakes. Commented Oct 16, 2018 at 14:52
  • What is smallestOpperator, show how it is declared. And also: has smallestOpperator been initialized before your piece of code? If not, then your code is completely wrong anyway. Please show more relevant code. Read this: minimal reproducible example Commented Oct 16, 2018 at 14:56
  • Don't use scanf for user input. Read this: stackoverflow.com/questions/2144459/… Commented Oct 16, 2018 at 14:58
  • isdigit works on charactes, not on integers; however, you read an integer into smallestOpperator so isdigit will generally fail. Solution: read a char into smallestOpperator using %c in scanf. Commented Oct 16, 2018 at 15:01

1 Answer 1

1

Evidently isdigit(smallestOperator) is returning false, because that's the conditional on the while, and you're seeing an infinite loop.

The sensible way to check the value of smallestOperator is to run your code in a debugger -- this will show you the type of the variable, its value, its in-memory representation - everything you could possibly need.

A dirtier way to achieve the same thing, is to add debugging printfs to your code, to print the value before it's checked.

However, we can see that you're using scanf(" %d", ...) to read smallestOperator. The manpage for scanf says:

d - Matches an optionally signed decimal integer; the next pointer must be a pointer to int.

So you're getting an integer from scanf then testing to see whether it's a character that's a digit.

You have two options to fix this:

Read a char, test it, convert it

Get a char from scanf: scanf(" %c", &smallestOperatorAsChar) -- then test it with isDigit(), then convert it to an integer: Convert a character digit to the corresponding integer in C

(Better) Let scanf() tell you about conversion errors

You should always check the return value from IO calls anyway. scanf() returns the number of items it's successfully converted. If it's fewer than the number you expected, you can use that to trigger your error handling.

int rc = scanf("%d", &i);
char junkChar;
while(rc < 1) {
    printf("Invalid");
    scanf("%c", &junkChar);
    rc = scanf("%d", &i);
}

See: why does scanf not wait for user input after it fails one time? -- in which the accepted answer observes that scanf() isn't a great stdin input mechanism, so you shouldn't work too hard trying to work around it: either take control by writing your own routines around fgets() or bring in a cleverer library.

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

Comments

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.