Validation in C

Length Check

This example is the equivalent of our Pascal demonstration of a length check.

#include <stdio.h>
#include <string.h>

int main()
{
  char code[20];
  do
  {
    printf("Please enter the 6 character product code: ");
    scanf("%s", &code);
  }
  while (strlen(code) != 6);
  printf("%s is the correct length.");
  return 0;
}    

Input and output:

Please enter the 6 character product code: ST892
Please enter the 6 character product code: STNY892
Please enter the 6 character product code: STN892
STN892 is the correct length.

Range Check

This example is the equivalent of our Pascal demonstration of a range check.

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int age;
  do
  {
    printf("Please enter the age of the student: ");
    scanf("%d", &age);
  }
  while ((age < 11) || (age > 19));
  printf("%d has passed the range check.\n", age);
  return 0;
}    
Please enter the age of the student: 10
Please enter the age of the student: 20
Please enter the age of the student: 19
19 has passed the range check.

Presence Check

This example is based our Pascal demonstration of a presence check. The routine fgets captures the whole line of input from the standard (keyboard) input stdin. The function sscanf with a single string specifier returns 1 when it is able to place a string starting at the address supplied by its third parameter (in this case the address of surname).

#include <stdio.h>
#include <string.h>

int main()
{
  char surname[20];
  char line[100];
  int len;
  int result;
  do
  {
    printf("Please enter your surname: ");
    fgets(line, sizeof(line), stdin);
    result = sscanf(line, "%s", &surname);
  }
  while (result != 1);
  printf("%s accepted", surname);
  return 0;
}
    

Result of pressing Enter (failing the presence check) followed by entering the letter M:

Please enter your surname:
Please enter your surname: M
M accepted

(This is a presence check only and does not include a length check).

Type Check

We demonstrate a type check for an integer, which is not as easy as using the val procedure in Pascal. The functions scanf and sscanf accept an integer followed by other characters. In this example we check that there are no appendages to the entered integer by comparing the length of the input with the number of digits in the integer that sscanf has obtained from the input string.

#include <stdio.h>
#include <stdlib.h>

int main()
{
  char line[50];
  int rainfall;
  do
  {
    printf("Please enter the rainfall to the nearest mm: ");
    fgets(line, sizeof line, stdin);
  }
  // while integer not start of input or integer length not input length or integer negative
  while (sscanf(line, "%d", &rainfall) != 1 || strlen(line) != snprintf(0, 0, "%+d", rainfall) || rainfall < 0);
  printf("%d mm accepted", rainfall);
  return 0;
}        

Input and output (first key press is Enter)

Please enter the rainfall to the nearest mm:
Please enter the rainfall to the nearest mm: 12.34
Please enter the rainfall to the nearest mm: a123
Please enter the rainfall to the nearest mm: 123a
Please enter the rainfall to the nearest mm: 123
123 mm accepted

Character Check

This demonstration of a character check is based on our Pascal original.

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool validate_surname(char last_name[20])
{
  bool error = false;
  int count;
  char c;
  for (count = 0; count < strlen(last_name); count++)
  {
    c = last_name[count];
    if (!isalpha(c)  && c != '-' && c != 39)
      error = true;
  }
  return !error;
}

int main()
{
  char surname[20];
  do
  {
    printf("Please enter your surname: ");
    scanf("%s", &surname);
  }
  while (!validate_surname(surname));

  printf("%s accepted", surname);
  return 0;
}    

Input and output:

Please enter your surname: Baron4
Please enter your surname: 4Baron
Please enter your surname: Baron@
Please enter your surname: Baron-Cohen
Baron-Cohen accepted

Format Check

This demonstration of a format check on UK national insurance (NI) numbers is based on our Pascal original.

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool validate_ni(char ni_num[10])
{
  bool error = false;
  int count;
  if (strlen(ni_num) == 9)
  {
    if (!isalpha(ni_num[0]))
      error = true;
    if (!isalpha(ni_num[1]))
      error = true;
    for (count = 2; count < 8 ; count++)
       if (!isdigit(ni_num[count]))
         error = true;
    if (!isalpha(ni_num[8]))
      error = true;
  }
  else
    error = true; // length check
  return !error;
}

int main()
{
  char str_ni[20];
  printf("Your NI number must have 2 letters followed\n"
         "by 6 digits followed be one letter.\n");
  do
  {
    printf("Please enter your NI number: ");
    scanf("%s", str_ni);
  }
  while(!validate_ni(str_ni));
 
  printf("%s accepted", str_ni);
  return 0;
}
    

Input and output:

Your NI number must have 2 letters followed
by 6 digits followed be one letter.
Please enter your NI number: RY123456T1
Please enter your NI number: R123456ST
Please enter your NI number: 2Y123456T
Please enter your NI number: RY123456T
RY123456T accepted
Programming - a skill for life!

How to apply your knowledge of Pascal when learning C/C++