C + + Learning lesson I-input/output

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.