1, Program:#include <iostream>
int main ()
{
std::cout<< "Enter numbers:" <<std::endl;
int v1,v2;
std::cin>>v1>>v2;
std::cout<< "The sum of" <<v1<< "and" <<v2
<< "is" <<v1+v2<<std::endl;
return 0;
}
The program first outputs
Enter the numbers:
The program then waits for the user to enter. If input 3 7 follows a newline character, the program produces the following output:
The sum of 3 and 7 is 10
2, Analysis:
#include <iostream>
is a preprocessing instruction that tells the compiler to use the iostream library.
In the main function
std::cout<< "Enter numbers:" <<std::endl;
<< is the output operator, and when the operator is the output operator, the result is the left operand.
Equivalent to
(std::cout<< "Enter numbers:") <<std::endl;
Or
std::cout<< "Enter The Numbers:";
std::cout<<std::endl;
Endl is a special value, called an operator (manipulator), that has a newline when it is written to the output stream, and refreshes the buffer associated with the device. By flushing the buffer, the user can immediately see the output written to the stream. Note that when the user forgets to flush the output stream it may cause the output to remain in the buffer, and once the program crashes, it will cause error inference to the location of the program crash.
prefix std:: indicates that cout and Endl are defined in the namespace STD, and the program avoids conflicts by inadvertently using names that are the same as those defined in the library.
std::cin>>v1>>v2;
>> is the input operator, similar to the output operator, and the result is the left operand.
Equivalent to
std::cin>>v1;
std::cin>>v2;
Read two values from the standard input, the first one in V1, and the second in V2.
C + + Learning lesson I-input/output