C ++ Primer Study Notes-Chapter 1

Source: Internet
Author: User

[Cpp]
# Include <iostream>
 
Int main ()
{
Std: cout <"Enter two numbers:" <std: endl;
Int v1, v2;
Std: cin> v1> v2;
Std: cout <"The sum of" <v1 <"and" <
V2 <"is" <v1 + v2 <std: endl;
System ("pause ");
Return 0;
}


Main function. The returned value must be of the int type.

This value can be regarded as a status indicator. If the return value is 0, the execution is successful. If the return value is not 0, a specific error occurs.


IO standard library, iostream library, defines four IO objects: cin, cout, cerr, clog.

Std: endl, with the output line break effect, and will refresh the buffer, so that you can immediately see the output written to the stream.

Std: endl, which contains two colons. This is a scope operator that specifies the namespace to which the service belongs.

 


While statement:

[Cpp]
# Include <iostream>
 
Int main ()
{
Int sum = 0, val = 1;
While (val <= 10 ){
Sum + = val;
++ Val;
}
Std: cout <"Sum of 1 to 10 random sive is"
<Sum <std: endl;
System ("pause ");
Return 0;
}


For statement:

[Cpp]
# Include <iostream>
 
Int main ()
{
Int sum = 0;
For (int val = 1; val <= 10; ++ val)
Sum + = val;
 
Std: cout <"Sum of 1 to 10 random sive is"
<Sum <std: endl;
System ("pause ");
Return 0;
}


Read the unknown target input:

[Cpp]
# Include <iostream>
 
Int main ()
{
Int sum = 0, value;
Std: cout <"Please Enter:" <std: endl;

While (std: cin> value ){
Sum + = value;
}

Std: cout <"Sum is"
<Sum <std: endl;
System ("pause ");
Return 0;
}

When a non-integer is read, or the file terminator (Ctrl + Z in windows; Ctrl + D in Unix) is input, the while loop ends.

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.