8)

Program 7 - user input

Program 7 User input Source code - prog7.c
This program introduces user input, which will make future programs a lot more interactive and useful. If you're still here, and reached program 7 then it all really starts here. After this step you should have the ability and understanding to write small useful interactive programs, using the scanf function to capture user keyboard input.

The code achieves the same as the previous program, but instead of the number of drams being "hard-coded" into the program, this time the user enters the drams and the program outputs the lb's, oz's and dr's.

 

Program code- new parts shown in red

#include <stdio.h>

void main(void)
{

int drams,lbs,ozs,drs;
printf("Please enter the amount of drams to convert ");
scanf("%d",&drams);
drs=drams;
lbs=drs/256;
drs=drs%256;
ozs=drs/16;
drs=drs%16;
printf("\n%d drams is %d lbs %d ozs %d drs",drams,lbs,ozs,drs);

}

 

Description of the program code
The new line in this code is the scanf("%d",&drams); line. The scanf statement can be used to accept user keyboard input and store that input in a variable. There should be a familiar look to the statement, as it closely resembles the printf statement previously used. When the program reaches this line, it will stop and wait for the user to type something in and press the enter key. Whatever number is typed in by the user is stored in the drams variable.

The format specifer %d says that the scanf statement is expecting an integer. The strange ampersand & in front of the variable name is not a mistake, it is required, but an explanation of it now would be mind blowing. Later when "pointers" are covered it will explained in full, but for now, just assume it has to be there.

With the scanf statement you can retrieve information more than one input from the user at a time. For example if two variables are declared called var1 and var2, the following statement scanf("%d%d",&var1,&var2); will store the first number typed in the variable var1 and the second number typed by the user in var2. Each time though the user of the program will need to press the enter key after entering each number. There is a way of accepting multiple input from the user and only requiring them to press the enter key at the end. Type in and try the following program :

#include <stdio.h>

void main(void)
{

int drams,lbs,ozs,drs;
printf("Enter pounds ounzes and drams, each seperated by a space, then press enter\n");
scanf("%d %d %d",&lbs,&ozs,&drs);
drams=(lbs*256)+(ozs*16)+drs;
printf("\n%d lbs, %d ozs, %d drs = %d drams",lbs,ozs,drs,drams);

}

If you look closely at the scanf("%d %d %d",&lbs,&ozs,&drs); line, you can see that there is a space between each of the %d's. Without this space the user would have to press the enter key after entering the lb's oz's and drams each time.

 

Summary
This program showed how a C program can retrieve input from a keyboard using the scanf statement. The programs that have been written so far have all been "deterministic", in other words, the output of the program was known and the same each time the program was run. Now though things become a lot trickier, because there is no way of knowing what data users of a program will type in. For example, there is nothing to stop a user typing in his or her name instead of the value of drams. Therefore as programs allow interaction, a big part of the program code that gets written is based around validating user input to make sure that the data they've entered is in a form that can be used by the program. No validating of the input is performed with the above program, nor subsequent programs for a while.

If the scanf statement was the only way to retrieve user input via the keyboard, this would present a problem with things like menu systems. For example, imagine the menu system below :

Menu

1.... Add
2.... Amend
3.... Delete
4.... Exit

Please select a menu option from above :

Using the scanf function, the user of this menu would have to enter the selection ( 1 - 4 ) and then press the enter key on the keyboard. Typically menu systems don't work this way, they do not require the enter key to be pressed after a menu selection, as soon as the user presses the key, the program continues on. So in short the scanf statement is useful for retrieving input from the user where the input consists of more than 1 character, but for a menu system it is not all that suitable. Therefore, the next program goes on to look at a way of getting "key presses".

 

Tasks

7.1) Write a program to let the user input the ages of two people, and it outputs the combined age of both ?

7.2) Write a program that converts fahrenheit temperatures to celcius. The equation celcius = (5/9) * (fahrenheit-32). You will need to use the float data type for all the variables. You can choose to either hard-code the fahrenhiet data into the variable or accept it from a user input. If you allow the user to input the data you will need to use the %f format specifier instead of the %d used above in the scanf statement.

Note : enter the above formula as ( 5.0 / 9.0 ) * ( fahrenheit-32 ).

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001