READING DATA FROM cin(Sec 3-2)
Suppose a History professor asks me to write a program to do averages for him. Do I want to put his students’ grades in my code? Are they the same each term?
Data read by a program can come from two places: the keyboard or a data file (see Section 9). Initially, our programs will read data from the keyboard, entered by interactive data entry, so called because the user interacts with the computer. The programmer writes the program so that the computer asks for data when it is ready to read. The computer pauses, waiting for a value. Once the user types in the requested value, the computer reads it and assigns it to a variable.
To read an integer into the variable num and then print out this value, use the following code:
int num;
cin > num;
cout < num < endl;
Suppose that the user types in the value 1234, followed by <Enter>, the program reads 1234 and stores it in num, replacing what was previously there
num
┌────────┐
│ 1234 │
└────────┘
Just as cout is an output stream, cin is an input stream (actually it is an object of class iostream, as is cout—whatever that means). We use the extraction operator to extract a value from the stream and store it in the variable whose name follows the operator. Again, think to remember which way it points, from cin to the variable.
Prompts
When the computer executes the statement that reads from cin, the cursor blinks on the screen. The program does not continue until the user types something. Another person may be running your program, and that person won't have a clue what to type. A considerate programmer always precedes a cin with a prompt, a cout that tells the user what to do. (SKIP: The prompt appears on the screen as the result of sending a message to cout or to cerr—see text, if interested).
cout < "Please enter a number: ";
cin > num;
cout < “You entered” < num < endl;
As a result of running this code, the following prompt appears on the screen, followed on the same line by the cursor (indicated by the underscore in the display):
Please enter a number: _
As the input value is typed, it appears immediately after the prompt on the same line. At this point, if the user types in 7 and then presses <Enter>,num will be assigned7 when the program is run.The nalue is assigned bu the user; the programmer doesn’t need to know the value.
Style: When the prompt and number are short, I put them both on one line. Otherwise put endl at the end of the prompt.
Let's read in the length and width of a rectangular room and then compute and print the area. The dimensions of the room may have decimal places, so the variables must have type double:
double length,width,area;
cout > “Enter length: ”;
cin > length;
cout > “Enter width: ”;
cin > width;
area = length * width;
cout < area;
Assume the user types in 14.5 for the length and 12.4 for the width. The first statement reads 14.5 fromcin and stores it in length. The second reads 12.4 and stores it in width. The value 179.8 is computed, stored in area, and then printed.
To read values into more than one variable, add an extraction operator and a name for each variable. I advise against this (SKIP:and may not debug your program if you do it).
SEEcin.cpp