1

I have an integer input for my I.D. My teacher says I need to error handle all my inputs and basically display appropriate error messages if input entered is invalid. So I'm trying to get an error message to pop up if the user enters a real number or number less than one. But what I've tried is not working!

  printf("Enter client Id--->");
  scanf("%d", &client.id);
  while ((client.id < 1) || (client.id % 1 != 0)) {
    printf("Invalid I.d entered\n");
    printf("Enter client Id--->");
    scanf("%d", &client.id);
  } // endwhile`
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256

3 Answers3

1
scanf("%d", &client.id);

You have to test the return value of scanf. If the result is 1, it has successfully scanned your integer. All you have to do next is to test that the value is >=1.

So, something like that:

   while( 1 ) {

     printf("Enter client Id--->");
     int scanned = scanf("%d", &client.id);

     if( scanned != 1 || client.id < 1 ) {
        // note: infinite loop for invalid input, see note below
        printf("Invalid I.d entered\n");     
     }
     else {
        break; // OK
     }
   }

NB(1) see there for a an explanation of why scanf is unsafe and ways of making it safe(r).

NB(2) you haven't included the client.id declaration, but it has to be an int type. Even if you wanted to scan a float instead of an int, x % 1 != 0 is a no-no.

Community
  • 1
  • 1
Ilya
  • 5,377
  • 2
  • 18
  • 33
0

You can add a printf after the scanf to ensure scanf is working as expected.

Still, regardless of what value is entered, the program exits without entering while() body.

Of the two while() conditions, the first is innocuous, but the second is suspect because it is false for all values.

Curt
  • 470
  • 3
  • 7
-1

To check that a number is not an integer, the easiest way (the way without function calls) would be client.id != (int)client.id.

But first, you must read the number as a floating-point type, precisely so you can catch that error. My code:

    double x;
    printf("Enter client Id--->");
    scanf("%lf", &x);
    while((x < 1.0) || (x != (int)x))
    {
        printf("Invalid I.d entered\n");
        printf("Enter client Id--->");
        scanf("%lf",&x);
    } // endwhile
    client.id = (int)x;
John Sensebe
  • 1,386
  • 8
  • 11
  • 2
    Note that because it's an int (presumably, because it's being read with %d) it's ***always*** an integer. – user253751 Jan 13 '16 at 22:59
  • Ah, yes. It has to be read in as a float or double. I'll add that to the answer. – John Sensebe Jan 13 '16 at 23:02
  • yes! thank you so much your code worked except that i had to change double to float but thank you so much! :) – Bhana Jameelah Jan 13 '16 at 23:46
  • @BhanaJameelah see this comment: http://stackoverflow.com/questions/34778575/programming-in-c-error-handling-an-integer-value/34778617#comment57303349_34778617 – Ilya Jan 13 '16 at 23:53
  • `double x; ... scanf("%f", &x);` is undefined behavior. Wrong specifier for the type. – chux - Reinstate Monica Jan 14 '16 at 00:08
  • Fixed. I've rarely used `scanf`, so I didn't realize it needed a different specifier, but it makes perfect sense now that I think about it, because it's stuffing the value into a reference. – John Sensebe Jan 14 '16 at 14:45