Back to DFS's C Page


Getting Good Data with the Do-While Loop II

Computers are excellent at doing the same operation over and over again -- people frequently are not. We can also get the computer to wait around until the user supplies a valid input, i.e., no letters in something that we plan on being an integer. This is an ideal situation for using a Do-While loop, because we know that the loop must be executed at least once to get a good value from the user. If we don't get one, we ask again.

We will use another modification of the area-of-a-triangle program to illustrate this. The problem and pseudocode are as follows:

The Problem: Write a program to calculate the area of a triangle, allowing only positive values to be entered for the base and height.

The Pseudocode:
1. Simple introduction and instructions;
2A. Repeatedly
    a) get the base
    while not positive;
2B. Repeatedly
    a) get the height
    while invalid or not positive;
3. Calculate the area;
4. Print out the results.

Please note that the loops for getting the base and height are NOT the same. Both begin by prompting the user for a value.

For the base, we here assume that the user will not input any invalid characters. If the value given for the base is not positive, we simply request another.

Because users frequently mistype things, the loop for obtaining the value for the height also checks to make sure that only characters that are acceptable for an integer have been typed. scanf() reports on invalid characters by returning a value of zero (0) if we are trying to read in a single value. Unless we assign this value to a variable or check it as it is returned, this information is lost and C does not change the value of the destination variable. scanf() will simply stop processing the input stream, which results in the newline character remaining in the buffer. To resume processing, we need to clear the buffer, which is done here by reading the remainder of the input into a throw-away string variable.

#include <stdio.h> /* standard I/O header file */
void main()
{
   int b, h; /* base, height */
   float A; /* Area */
   int badinputflag;
   char dummystr[25];
   /* -------------------Introduction------------------ */
   printf("This program calculates and prints the area of a triangle\n");
   printf("after you enter its dimensions.\n");
   printf("When asked to, type in a dimension and hit the ENTER key.\n");
   printf("\n");
   /* -------------------Inputting Data------------------*/
   /*------User-Friendly------*/
   do {
      printf("What is the length of the triangle's base? ");
      scanf("%d", &b);
      if (b <= 0)
         printf("The base needs to be GREATER than zero.\n");
   } while (b <= 0);
   /*------VERY User-Friendly------*/
   printf("What is the triangle's height? ");
   do {
      badinputflag = scanf("%d", &h);
      if (badinputflag == 0)
      {
         scanf("%s", dummystr); /* Clear the input buffer */
         printf("A typo was found for height.\n");
         printf("Please re-enter a value for the height: ");
      }
      else if (h <= 0)
      {
         printf("The height needs to be GREATER than zero.\n");
         printf("Please re-enter a value for the height: ");
      }
   } while ( (badinputflag == 0) || (h <= 0) );
   /* -------------------Calculation------------------ */
   A = b * h / 2.0;
   /* -------------------Printing Results------------------ */
   printf("\n");
   printf("The area of a triangle with a base of %d units and\n", b);
   printf("a height of %d units is %.1f square units.\n", h, A);
}

© DFStermole: 4 Oct 98
HTMLified: 4 Oct 98