< previous page page_143 next page >

Page 143
volved in writing interactive programs that has to do with instructions for the user (the person executing your program).
To get data into an interactive program, we begin with input prompts, printed messages that explain what the user should enter. Without these messages, the user has no idea what to type into a program. A program also should print out all of the data values typed in so that the user can verify that they were entered correctly. Printing out the input values is called echo printing. Here's a program segment showing the proper use of prompts:
cout << Enter the part number: << endl;             // Prompt
cin >> partNumber;
cout << Enter the quantity of this part ordered:    // Prompt
     << endl;
cin >> quantity;
cout << Enter the unit price for this part:         // Prompt
     << endl;
cin >> unitPrice;
totalPrice = quantity * unitPrice;
cout << Part  << partNumber                        // Echo print
     << , quantity  < quantity
     < , at $  << setprecision(2) << unitPrice
     <   each << endl;
cout << totals $  << totalPrice << endl;
And here's the output, with the user's input shown in color:
Enter the part number:
4671
Enter the quantity of this part ordered:
10
Enter the unit price for this part:
27.25
Part 4671, quantity 10, at $ 27.25 each
totals $ 272.50
The amount of information you put into your prompts depends on who is going to be using a program. If you are writing a program for people who are not familiar with computers, your messages should be more detailed. For example, Type a four-digit part number, then press the key marked RETURN. If the program is going to be used frequently by the same people, you could shorten the prompts: Enter PN. and Enter Qty. If the program is for very experienced users, you can prompt for several values at once and have them type all of the values on one input line:
Enter PN, Qty, Unit Price:
4176 10 27.25

 
< previous page page_143 next page >