Aside from using switch statements, do-while loops, and if-else trees, is there a good way to complete error checking after a user input from the keyboard?

What do you mean by complete error checking after a user input from the keyboard ?

Could you give an example?

sorry that might have been a little vague, an example might be if the program was asking for a number between 10 and 50 and the user entered 8. That entry is not what the program was asking for so error checking needs to be put in place so that the program doesn't just take the number 8 and use it.

i.e.

int myint;

printf("Enter a number between 10 and 50: ");
scanf("%d", &myint);

//check if number is between 10 and 50, then return some value that determines true or false

Well you could do this:

return (myint > 10 && myint < 50) ?  1 : 0; // 1, 0  indicating true/false

Is this what you were looking for?

Perhaps something like this?

printf("Enter a number between 10 and 50: ");
if ( scanf("%d", &myint) == 1 && myint >= 10 && myint <= 50 ) {
  // do stuff
}

But if you wanted to really make it bomb-proof, you would need to use fgets() to read a line of input into a buffer, and use strtol/strtoul to convert the string to an integer, and check the return results of both functions.

Note: scanf conversions do NOT detect overflow/underflow, which the strto.. functions will.

Even after you use fgets you will still have to use some if elses to check if the input violates the range restrictions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.