1.2.2. A program that uses the IO library
The program first displays the prompt on the user's screen:
Enter two numbers:
The program then waits for user input. If the user enters
3 7
Following a newline, the program produces the following output:
The sum of 3 and 7 is 10
The first line of the program is a preprocessing directive :
#include <iostream>
std::cout << "Enter two numbers:" << std::endl;
endl is a special value, called an operator, that has the effect of wrapping a line when it writes to the input and output streams, and flushes the buffer associated with the device.
Writing to a Stream
write to stream
The first statement in the main function body executes an expression . In C++, an expression consists of one or more operands and usually an operator. The expression for this statement uses the output operator ( << operator) to print a prompt on standard output:
std::cout << "Enter two numbers:" << std::endl;
In C++, every expression produces a result, usually the value produced by applying an operator to its operand. When the operator is an output operator, the result is the value of the left operand. That is, the value returned by an output operation is the output stream itself. Now that the output operator returns its left operand, we can chain output requests together. The statement that outputs the prompt is equivalent to (std::cout << "Enter two numbers:") << std::endl; Since ( (std::cout << "Enter two numbers:") ) returns its left operand std::cout , this statement is equivalent to std::cout << "Enter two numbers:"; std::cout << std::endl; endl is a special value called a manipulator that , when written to the output stream, has the effect of wrapping the output and flushing the buffer associated with the device . By flushing the buffer, the user can immediately see the output written to the stream.
|
Using Names from the Standard Library
use the name from the standard library
A side effect of the standard library's use of namespaces is that when we use a name from the standard library, we must explicitly express that we are using a name in the std namespace . The writing of std::cout uses the scope operator ( scope operator , :: operator), which means that the cout defined in the namespace std is used . We'll learn how to avoid this verbose syntax often used in programs in Section 3.1 .
Reading From a Stream
read stream
After the prompt is output, the data input by the user will be read. First define two variables named v1 and v2 to hold the input:
int v1, v2;
Define these variables as type int , a built-in type that represents integer values . These variables are uninitialized , meaning they are not assigned initial values. These variables are read in a value when they are first used, so they can have no initial value.
The next statement reads the input:
std::cin >> v1 >> v2;
The input operator (>>operator) behaves like the output operator. It accepts anistreamobject as its left operand and an object as its right operand, and it reads data from theistreamoperand and saves it into the right operand. Like output operators, input operators return their left operand as the result. Since the input operator returns its left operand, we can combine the sequence of input requests into a single statement. In other words, this input operation is equivalent to:
std::cin >> v1; std::cin >> v2;
The effect of the input operation is to read two values from standard input, placing the first in v1 and the second in v2 .
Completing the Program
complete the program
All that's left is to output the result: std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl; Although this statement is longer than the one that prints the prompt, there is no conceptual difference. It outputs each operand to standard output. Interestingly, the operands are not all values of the same type, some are string literals . E.g "The sum of " and others are various int values, such as v1, v2, and the result of evaluating the arithmetic expression: 其他是各种 int 值,如 v1、v2 以及对算术表达式 v1 + v2 The iostream library defines versions of the input and output operators that accept all of the built-in types. 求值的结果。iostream 库定义了接受全部内置类型的输入输出操作符版本。
|