Detailed explanation of the usage of assignment and input and output statements in C + + _c language

Source: Internet
Author: User
Tags control characters

C + + Assignment statement explanation
C + + assignment statements have the ability to assign statements to other high-level languages. But the difference is that the assignment number "=" in C + + is an operator that can be written as

  A=b=c=d;

In most other languages the assignment number is not an operator, and the above is not legal.

About the concept of an assignment expression and an assignment statement. In C + +, an assignment expression can be included in other expressions, such as:

  if ((a=b) >0) cout<< "A>0" <<endl;

A condition is specified in the () following syntax. Now replace an assignment expression "a=b" in the position of x by assigning an assignment (assigning a value of B to a) and then determining whether a is greater than 0, such as greater than 0, and executing cout<< "a>0" <<endl;. The "A=b" in an If statement is not an assignment statement but an assignment expression, which is valid to write. Can't write

  if ((a=b;) >0) cout<< "A>0" <<endl;

Because an assignment statement cannot be included in the condition of If. C + + distinguishes the assignment statement from the assignment expression, increases the type of expression, and implements the functions that are difficult to implement in other languages.

C + + input cout and output cin
input and output are not formal components of the C + + language. Neither C nor C + + itself provides a specialized statement structure for input and output. The input output is not defined by C + + itself, but is defined in the I/O library provided by the compilation system.

The output and input of C + + is implemented in the form of "stream".

Information about the definition of CIN, cout, and stream operators for streaming objects is stored in the input and output stream libraries of C + +, so if you use the CIN, cout, and flow operators in your program, you must use the preprocessing command to include the head file stream in this file:

  #include <iostream>

Although Cin and cout are not statements provided by C + + itself, in the unlikely case of confusion, statements that are entered by the CIN and stream extraction operator ">>" are often referred to as input statements or CIN statements, and the cout and stream insertion operator "<<" is used to describe the convenience. The statement that implements the output is called an output statement or a cout statement. According to the syntax of C + +, all the actions that can be implemented and end with a semicolon are statements.
Basic operation of input stream and output stream

The general format for cout statements is:

  cout<< expression 1<< expression 2<<......<< expression n;

The general format for a CIN statement is:

  cin>> variable 1>> variable 2>>......>> variable n;

When defining a Stream object, the system creates a buffer in memory for staging data for the input and output stream. When the cout statement is executed, the inserted data order is stored in the output buffer until the output buffer is full or the Endl (or ' \ n ', Ends,flush) in the cout statement is reached and the existing data in the buffer is exported together, and the buffer is emptied. The data in the output stream is output from the system's default device (typically the monitor).

A cout statement can be written in several lines. Such as

 cout<< "This is a simple C + + program." <<endl;

can be written

  cout<< "This is"//note no semicolon at the end of the line
  << "A C + +"
  << "program."
  <<endl; Statement has a semicolon last

You can also write multiple cout statements, i.e.

  cout<< "This is"; The end of the statement has a semicolon
  cout << "A C + +";
  cout << "program."
  cout<<endl;

The output of the above 3 types of cases are

This is a simple C + + program.

Note that you cannot insert multiple output items with an insert operator "<<", such as:

  cout<<a,b,c; Error, cannot insert multiple items at once
  cout<<a+b+c;//Correct, this is an expression, as a

When using cout output, the user does not have to inform the computer according to what type of output, the system will automatically determine the type of output data, so that the output of the data according to the corresponding type output. If a is defined as int, B is float, and C is a char, the

  cout<<a<< ' <<b<< ' <<c<<endl;

will be output in the following form:

  4 345.789 A

Like cout, a CIN statement can be written in several lines. Such as

  cin>>a>>b>>c>>d;

can be written

  Cin>>a//Note the end of the line without a semicolon >>b//
  This may look clearer
  >>c
  >>d;

can also be written

  cin>>a;
  cin>>b;
  cin>>c;
  cin>>d;

The above 3 types of cases can be entered from the keyboard:

1 2 3 4↙

You can also enter data in multiple lines:

1 ↙
2 3↙
4↙

When you enter with CIN, the system also extracts the corresponding length of bytes from the input stream according to the type of the variable. If any

  Char C1, C2;
  int A;
  float B;
  cin>>c1>>c2>>a>>b;

If you enter

1234 56.78↙

Note: 34 should be followed by a space to be separated from 56.78 points. You can also enter the following format:

1 2 34 56.78↙ (with spaces between 1 and 2)

You cannot use a CIN statement to enter a space character and a carriage return newline character as characters to a character variable, and they are skipped. You can use the GetChar function if you want to enter a character variable with a space character or carriage return line break (or any other character on any other keyboard).

When you organize input stream data, you should carefully analyze the type of the variable in the CIN statement and enter it in the appropriate format, otherwise error prone.
using a control character in an input stream and an output stream

The default format for using cout and CIN is described above. But sometimes people in the input and output have some special requirements, such as the output of real numbers, specify the width of the field, only two decimal digits, data left or right alignment, and so on. C + + provides the control characters that are used in the input and output streams (some of which are called operators), as shown in table 3.1.

Note that if you use a control character, in addition to the beginning of the program unit to add iostream header file, but also add Iomanip header file.

For example, the output double precision number:

  Double a=123.456789012345; Assigning an initial value to a

1) cout<<a; Output: 123.456
2) cout<<setprecision (9) <<a; Output: 123.456789
3) cout<<setprecision (6); Restore the default format (precision 6)
4) cout<< setiosflags (ios∷fixed); Output: 123.456789
5) Cout<<setiosflags (ios∷fixed) <<setprecision (8) <<a; Output: 123.45678901
6) Cout<<setiosflags (ios∷scientific) <<a; Output: 1.234568e+02
7) cout<<setiosflags (ios∷scientific) <<setprecision (4) <<a; Output: 1.2346E02

The following is an example of an integer output:

  int b=123456; Assign an initial value to B

1) cout<<b; Output: 123456
2) cout<3) cout<<setiosflags (ios∷uppercase) <<b; Output: 1E240
4) cout<<setw <<b<< ', ' <<b; Output: 123456,123456
5) Cout<<setfill (' * ') <<SETW <<b; Output: * * * * 123456
6) Cout<<setiosflags (Ios∷showpos) <<b; Output: +123456

If you use the same SETW (n) in multiple cout statements, and you use Setiosflags (ios::right), you can align the row data to the right, and if you specify the same precision, you can align up and down decimal points.

"Example" aligns the decimal points of the line.

#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
 double a=123.456,b=3.14159,c=-3214.67;
 Cout<<setiosflags (ios::fixed) <<setiosflags (ios::right) <<setprecision (2);
 COUT<<SETW (<<a<<endl;
 ) COUT<<SETW (<<b<<endl;
 ) COUT<<SETW (<<c<<endl;
 ) return 0;
}

The output is as follows:

123.46 (field width is 10, right is aligned, take two decimal places)
3.14
-3214.67

Set the fixed-point output first, take two decimal digits, and align right. These settings are valid for subsequent output (unless reset), and SETW is only valid for the subsequent output item, so the SETW (10) must be written before the output a,b,c.

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.