Friday, February 26, 2010

Intro to Computer Programming - Chapter 6: Communicating with the Computer


Introduction

    As stated in Chapter 4, one of the most common things a computer can do is print information to the screen. The next most common thing a computer does is ask the user for input.

    Input has many places it could come from. It could come from the keyboard, the mouse, the sound card, a touch screen, modem, a file, among many others. The most common of these is the keyboard. This chapter will deal with keyboard based input from the user.

Keyboard input with Qbasic

    Qbasic has several keyboard input statements. The two most common for most programs is the input statement and the line input statement. Both of these commands wait for the user to press the enter key before it accepts any information from the user. After the user presses enter, the information is then transferred into a variable for later processing.

Input Statement

    The input statement is one of Qbasic's most versatile of keyboard input commands. It allows the programmer to request multiple pieces of information from a user and automatically checks to see that the entered data was valid. The input statement also allows the programmer to include a prompt along with the request for information.

    The input statement allows the programmer to use multiple variables by using the same input statement. For example, if the programmer wanted an integer, a string, and a float entered, one input statement could be used. The way the statement is formed is by putting the statement input followed by a list of variables, each separated by a comma (,), that will hold what the user typed in. After the enter key is pressed, Qbasic automatically verifies the information to make sure the user actually typed a number when a number was requested instead of a string. If the user accidentally put the wrong type of information for an input, Qbasic will ask the user to redo the input by printing the words "Redo from Start" on the screen and it will ask again for the input from the user. It will continue to do this until the user inputs the correct information.

    The input statement has several options that can be included. These include a prompt, keeping on the same line, and inserting a question mark after the prompt.

The first option that can be used is the use of a prompt. A prompt can be included in the input statement so that the user will know what information is requested by the program. The prompt is simply enclosed in quotes after the input statement, just as is done with the print statement. Notice that the prompt can not be any type of expression but must be a string. The prompt must also come before the input variable list. You may ask why isn't the prompt just printed using a print statement, the answer is because if the user enters the incorrect information, the prompt will not be displayed again unless it is part of the input statement.

The second option is the ability to continue on the same line. This is similar to the way the print statement can stay on the same line after it displays its output. The option is also similar to the print statement in that a semicolon (;) is used. With the print statement, the semicolon is used after the output, but in the case of the input statement, the semicolon must be directly after the input command and before the prompt, if there is one.

The third option is the ability to automatically include a question mark (?) after the prompt. This option is automatically performed if a prompt is not included in the statement. The way to prevent a question mark from appearing as part of the prompt is to place a comma between the prompt and the input variable list. If a question mark is requested, the programmer should place a semicolon between the prompt and the input variable list.

The user of a program that uses the input statement should know that numbers should not contain commas because the comma is considered to be a delimiter. A delimiter is any character or set of characters that marks the end of a word, number, or phrase (for example, the delimiters for words are spaces, and most punctuation symbols excluding the apostrophe (')). With the input statement, commas are always considered to be a delimiter for both numbers and for strings (with the input statement, a string can not contain a comma unless the string is enclosed in quotes). So, for example, if the user enters the numbers 45,167 the computer would accept the 45 as the first input and the 167 as the second. If the computer was expecting only one number, it would request the user to enter the information again which could possibly frustrate the user.

Also, when the program is expecting a number, symbols other than numbers, negative or positive signs, or the decimal point are illegal. For instance if the program is asking for how much the user makes per hour and the user enters $4.75, the program would reject the input because the dollar sign is an illegal symbol for a number.

The following program and dialog between the computer and user demonstrate the uses of the input statement.

INPUT "What is your name";A$
PRINT "Hello, ";A$;"."
INPUT "Enter 2 numbers separated by commas:",A,B
PRINT A$;", ";"the sum of";A;" and";B;" is";A+B
PRINT "I would like to calculate about how much your"
PRINT "next paycheck will be."
PRINT "How many hours did you work the past 2 weeks?";
INPUT ;"",hours
PRINT " at how much per hour";
INPUT wage
PRINT A$; ",";"you will be getting paid approximately";
PRINT "$";hours*wage 
What is your name?
Joe
Hello, Joe.
Enter 2 numbers separated by commas: 4, 8
Joe, the sum of 4 and 8 is 12
I would like to calculate about how much your
next paycheck will be.
How many hours did you work the past 2 weeks? 40 at how much per hour? 5.75
Joe, you will be getting paid approximately $ 230
What is your name?
Doe, John

Redo from start
What is your name?
John Doe

Hello, John Doe.
Enter 2 numbers separated by commas: 4, 8, 7

Redo from start
Enter 2 numbers separated by commas: 7, 5
John Doe, the sum of 7 and 5 is 13
I would like to calculate about how much your
next paycheck will be.
How many hours did you work the past 2 weeks? 4 at how much per hour? $6.00

Redo from start
at how much per hour? 6.00
John Doe, you will be getting paid approximately $ 24
Program 16 – Qbasic Input Demonstration

Line Input Statement

    In comparison to the input statement, the line input statement is very limited in its uses. This limitation has its advantages though. The line input statement has no options and also is only able to input strings. The advantage to this is that line input does not respond to any delimiters with the exception of the enter key so any character is allowed to be entered and the program will not come back and say "Redo from Start".

    Program 17 demonstrates the use of the line input statement.

LINE INPUT A$
PRINT
PRINT A$ 
"Hello, how are you doing?"

"Hello, how are you doing?" 
Program 17 - Qbasic Line Input Program

Keyboard input with C

    Once again, the complexity of C raises its ugly head with keyboard input as it did with the printf command, which hasn't yet been completely covered. The keyboard input command for C is just as complex as its output function printf.

    The scanf function is C's version of keyboard input. It is very similar in style to the printf function. With the scanf function, the type of the variables to be inputted must be specified by using a type specifier.

    Because the scanf function is only input, it cannot display any information on the screen, but the command format is similar to the printf function in that the type specifiers are enclosed in quotes, and after the quotes are a list of the input variables. One difference between the printf function and scanf functions is that the input variables of the scanf function must be preceded by an ampersand (&) [at least at this point in the book]. The reason for the ampersand will be explained in a later chapter.

The type specifiers of the scanf (and printf) functions are preceeded by a percent sign (%) and then the specifier itself. The following table lists the type specifiers for the scanf and printf functions (notice that the percent sign is already included). One difference between the printf and scanf type specifiers is that the scanf type specifiers should be preceeded by a space.

Type Specifier 
Description 
%d 
Integer 
%f 
Floating point number 
%e 
Floating point in
scientific notation
(for printf) 
%g 
Shortest of either
floating point or
scientific notation
(for printf) 
%c 
Character 
%s 
String [don't use yet!]
Table 24 – Scanf and printf type specifiers

 

As with most of C/C++'s functions, an include file must be specified in order to allow the program to use scanf. The include file for scanf is the same as it is for the printf function, stdio.h.

The scanf function is unlike the Qbasic input command in that it does not do any checking of what the user typed in as input nor does it automatically create a prompt for the user. Both of these tasks must be accomplished by the programmer. The scanf function [with the information you know so far] can also only input a single character, unlike the input or line input commands of Qbasic which can input words or entire lines of characters. The scanf function is similat to input in that the user must press enter or return before the computer will accept the input.

    The following program demonstrates the use of the scanf and printf statements.

#include <stdio.h>

int main()
{
int j;
char t;
float y;
int a1,b1;
printf("Please enter an integer:");
scanf(" %d",&j);
printf("Please enter a non-integer number:");
scanf(" %f",&y);
printf("Please enter a character:");
scanf(" %c",&t);
printf("You entered the integer number %d\n",j);
printf(" the non-integer number %g\n",y);
printf(" the character %c\n",t);
printf("\n");
printf("Please enter 2 integer numbers separated by a space:");
scanf(" %d %d",&a1,&b1);
printf("The sum of %d and %d is %d\n",a1,b1,a1+b1);
return 0;
}
Please enter an integer:4
Please enter a non-integer number:1.5
Please enter a character:x
You entered the integer number 4
the non-integer number 1.5
the character x

Please enter 2 integer numbers separated by a space:1 4
The sum of 1 and 4 is 5
Program 18 - Scanf and printf example program
    Notice how the type specifiers can be "chained" together in both the printf and scanf statements. In the printf function, the type specifiers do not require a space between them but the scanf function requires a space [technically the space is not required but is recommended to prevent input problems].

    Just as wish Qbasic, the scanf statement includes automatic delimiters of space, comma, and enter.

    It is the programmer's responsibility to check the input that the user typed to make sure that it is valid. For example, the user could type in a decimal point (.) instead of a number and scanf will continue without warning, or the user could've entered a floating point number instead of an integer. The programmer needs to be aware that scanf does not check the data that has been entered. The next chapter will explain some ways of how to check for valid data.

Keyboard input with C++

    Once again, C++ is a mixture of the advantages of C and Qbasic combined. The cin statement in C++ is similar to the printf statement in C in that a prompt is not outputted automatically and that the data is not checked for the correct type. The cin statement has the advantage of being simplier than the C printf function in that the type specifiers are not used and the cin statement's structure is similar to that of the print statement in Qbasic.

    In an earlier chapter, you learned that the cout statement uses the double-less-than operator (<<) between the cout statement and the requested output. The cin statement is similar in that it uses the opposite operator, the double-greater-than (>>) between the cin statement and the variables used for input. As with the cout statement, the operators can be chained together.

    The cin statement falls under the same category as the cout statement and therefore requires the same include file, iostream.h, to be included in the program.

    Just as with the input statement and scanf function, the cin statement includes delimeters of space, comma, and enter. Also, just as the scanf statement, the input is not checked for correctness nor correct type therefore the programmer must ensure the user entered the correct data.

    The following program illustrates the general use of the cin statement. At this time, as with scanf, strings are not able to be used by cin.

#include <iostream.h>

int main()
{
int j;
char t;
float y;
int a1,b1;
cout<<"Please enter an integer:";
cin>>j;
cout<<"Please enter a non-integer number:";
cin>>y;
cout<<"Please enter a character:";
cin>>t;
cout<<"You entered the integer number "<<j<<endl;
cout<<" the non-integer number "<<y<<endl;
cout<<" the character "<<t<<endl;
cout<<endl;
cout<<"Please enter 2 integer numbers separated by a space:";
cin>>a1>>b1;
cout<<"The sum of "<<a1<<" and "<<b1<<" is "<<a1+b1<<endl;
return 0;
}
Please enter an integer:4
Please enter a non-integer number:1.5
Please enter a character:x
You entered the integer number 4
the non-integer number 1.5
the character x

Please enter 2 integer numbers separated by a space:1 4
The sum of 1 and 4 is 5

 

Conclusion

    Each language has some sort of command to get input from the user, the most common type of input being input from the keyboard. The commands seen so far have required the user pressing the enter key in order to tell the computer the user was finished entering data.

    The input command in Qbasic is very versatile in that it allows for automatic prompting of the user as well as inputting various types of data such as integers and strings. It also does a check to verify that all data is at least the correct type.

    The line input statement in Qbasic is useful for inputting strings that could contain commas or other types of punctuation that could be considered a delimiter when used with the input command. It does not allow automatic prompting.

    The scanf function in C is similar to the printf function in its command structure. A quoted string of type specifiers (shown in table 24) describes the input and a list of variables follows the string. The scanf function does not check the user inputted data against the type specifiers nor does it allow for automatic prompting. The scanf statement also requires an ampersand before the names of the variables and a space should separate the type specifiers within the input string.

Type Specifier 
Description 
%d 
Integer 
%f 
Floating point number 
%e 
Floating point in
scientific notation
(for printf) 
%g 
Shortest of either
floating point or
scientific notation
(for printf) 
%c 
Character 
%s
String [don't use yet!] 
Table 25 – Scanf and printf type specifiers

 

    The cin statement is similar to the cout statement except it uses the double-greater-than operator (>>) between the cin and the variables. The cin statement also does not check the input to verify that it is the correct type requested by the program nor does it allow for automatic prompting.

Problems



  1. Write a program to calculate the sum, difference, product, quotient, and remainder of two numbers from the user. Be sure the user knows exactly what to input (i.e. use a prompt).


  2. Modify Problem 1 to calculate the logical and, logical or, bitwise and, bitwise or, and bitwise xor of two numbers given by the user. Be sure to use a prompt.


  3. Write a program to set x equal to the remainder of a1 and a2, set y equal to the quotient of b1 and b2 and set z equal to the sum of x and y (the sum should not equal 0!). Print out each variable as well as a prompt on a line by themselves. The values of a1, a2, b1, and b2 should be given by the user.

No comments:

Post a Comment