Conventional coding for validating inputs in C programming

When validating inputs from a user and doing it through functions in C programming, will you have a lot if if statements that checks 1 or 0 that is returned from the validating functions?

If you don't understand what I mean, then below is code I typed strictly as an example. It's definitely not being used anywhere else.

#include <stdio.h>

int checkIfZero(int x){
  int result = 1;
  if (x ==0){
    printf ("You typed in zero for your age. Try again.\n\n");
    result = 0;
   }
return result;
}

int checkUpper(char x){
   int result = 1;
  if (x > 96){
    printf("Iniitial is not a uppercase. Try again\n\n");
    result  =0;
    }
return result;
}

int main(int argc, const char * argv[])
{
int age;
char initial;
int correct = 0;

do {
    int counter; // holds returned result of first function 
    int counter2; // holds returned result of the second function 

    printf("Please type your age and the initial of your first name in Uppercase\n");
    scanf("%d %c", &age, &initial);

    counter = checkIfZero(age);
    if(!counter){
        continue;
    }
    counter2 = checkUpper(initial);
    if (!counter2){
        continue;
    }
    correct = 1;
    printf("Correct\n");
} while (correct==0);

   return 0;
}

If you notice, I have 2 functions that validate the inputs. Later, I have to create different variables that either will have a 1 or 0 form what these functions return and check them using if statements.

Now let's say I create more like 10 of validating functions

Does that mean I have to create 10 different variables to catch the returned result of the functions and then type 10 if statements?

I'm okay with that if that's how people usually do it, but is that the case?

Answers


You don't have to place the result in a variable:

counter = checkIfZero(numSiblings);
if(!counter){
  continue;

Can become:

if (!checkIfZero(numSiblings))
  continue;

Define a standard of how it works, and stick to it.

But don't mix up validation ("Is the value OK?") with error message output ("fprintf(...)"). If you sometime later want to, say, check a set of three variables and give a single error message ("Illegal name" after checking given, middle, and last name) this will get in the way. Make sure you can redefine messages (i.e., for your future Greek version of the program, or to reuse the "is this a valid UTF-8 string" validation for different input data; think about combining tests, like UTF-8 and only letters) and that the error output stream can be redirected.

Perhaps you should look around at largeish open source projects, particularly ones that have to validate varied user input. You should be able to find a framework that you can, er, steal.


Need Your Help

Searching a file and returning value - Super Fast

c search

I have a set of data which has a name, some sub values and then a associative numeric value. For example:

Java HashMap indexed on 2 keys

java data-structures map hashmap

I want to create a HashMap in java for users with preferences. This would be easy to do in a database, but unfortunately I can't use a database. What I need is a way to find a user by name in the H...