Reading Multiple Inputs

Reading in single chunks of input is great, but being able to read in multiple pieces of input at once would be better. This can be accomplished using the scanf() function, which can read multiple inputs. For example:

int num1, num2;
scanf("%d %d", &num1, &num2);

That line of code will read in two integer values, separated by a space, and assign them to the num1 and num2 variables.

To demonstrate this, the following example will take both a number (the temperature) and the letter F or C (which determines whether that value is in degrees Fahrenheit or degrees Celsius). The application will then convert that number to the other format.

To read in multiple inputs at once

1.
Create a new file or project in your text editor or IDE.

2.
Type the standard beginning lines of code (Script 5.4):

/* temp_conversion.c - Script 5.4 */
#include <stdio.h>
int main (void) {

Script 5.4. This well-rounded application takes both a numeric temperature and the letter C or F, and then converts the temperature accordingly.


3.
Declare the required variables:

float temp_i, temp_o;
char which_i, which_o;

There are four variables in this application. Two are floats representing the inputted temperature (in degrees Celsius or Fahrenheit) and the outputted temperature (converted from the input). The other two variables are single characters, storing C or F. Again, one is to record the inputted value and the other stores the outputted format.

4.
Prompt the user for the required information:

printf ("Enter a temperature and
 indicate if it's Fahrenheit or
 Celsius [##.# C/F]: ");

When taking multiple inputs—particularly when they are of different types—it's very important to adequately indicate to the user how the information should be keyed. Again, the square brackets are used to suggest the formatting.

5.
Read the input and store this in the appropriate variables:

scanf ("%f %c", &temp_i, &which_i);

This function will look for a float and store that in temp_i. Next it will look for a character, which will be stored in which_i.

6.
Create the switch statement, wherein conversion will take place:

switch (which_i) {

If the entered value is in degrees Fahrenheit, then degrees Celsius must be determined, and vice versa. This switch will use the value of which_i (which should be C, F, c, or f) to decide the action that should take place.

7.
Add the first two cases:

case 'C':
case 'c':
     temp_o = (temp_i * (9.0/5.0)) + 32;
     which_o = 'F';
     break;

The first two cases perform the same calculation if the user entered either C or c. The applicable case statements will take the entered degrees in Celsius and convert this to Fahrenheit, assigned to temp_o. The which_o variable is assigned the value of F, to mark the format of the output.

8.
Repeat the cases for Fahrenheit:

case 'F':
case 'f':
    temp_o = (temp_i - 32) * (5.0/9.0);
    which_o = 'C';
    break;

9.
Set the default case and close the switch:

     default:
            which_o = 0;
            break;
} // End of switch.

If which_i is not equal to one of the four allowed values, the conversion shouldn't take place. In this case, which_o is set to 0. This value will let the conditional at the end of the application know there was a problem.]

10.
Print the results of the conversion, assuming that it took place:

if (which_o) {
    printf ("%0.1f %c is %0.1f
 %c.
", temp_i, which_i, temp_o,
 which_o);
} else {
    printf ("You failed to enter 'C'
 or 'F' to indicate the current
 temperature.
");
}

If which_o is true (because it has a value other than 0), the results of the conversion will be printed. The print statement outputs both the starting values and the converted ones. If which_o is false (because it has a value of 0, assigned within the switch's default case), a message stating that the input was not of the proper format is sent.

11.
Complete the main function:

    getchar();
    getchar();
    return 0;
}

12.
Save the file as temp_conversion.c, compile, and debug as necessary.

13.
Run the application, entering different values (Figures 5.9 and 5.10) to see the results.

Figure 5.9. Converting Celsius to Fahrenheit.


Figure 5.10. Converting Fahrenheit to Celsius.


✓ Tips

  • If the user doesn't input the right number and type of arguments, the user will most likely see results like those in Figure 5.11. The next section of this chapter will demonstrate how to better react in such a case.

    Figure 5.11. The application doesn't register that an F or C was entered because the first argument (which should have been a float) could not be properly read.

  • If the submitted temperature is 32 degrees Fahrenheit, the outputted temperature will be 0.0 degrees Celsius. This still passes through the if (which_o) conditional because while 0 is false, 0.0 is not.

  • Although the previous example suggested that scanf() isn't the best way to read in single numeric values, it is the best way to read in multiple values, if even those multiple values may include numbers.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset