is still input and output (character matrix, space, newline)

Source: Internet
Author: User

The problem with Pat's blessing is because of the input and output ... The first example is over, the underlying is the storage error?

Today, we mainly describe how to enter character matrix, string, and space wrapping.

SCANF is the formatted input, and printf is the formatted output. CIN is the input stream, cout is the output stream. The efficiency is slightly lower, but the writing is simple. CIN is less real-time because it uses buffers, which are normally full before being refreshed.

For character: The input of CIN ignores spaces and carriage returns. scanf ("%c", &i) is equivalent to i = GetChar (), and the newline character and carriage return are read in.

First, for the input string matrix, the general will use the double cycle, for the absorption of the carriage return, theoretically speaking GetChar () can be used, but do not know why, when the problem will be more absorbing characters, it is recommended to use another method:

    

The specific reason is not known, just know can use.
Cin simply absorbs a single character, does not absorb a carriage return or a space, but if each line of characters is continuous input, it is recommended to use Getline (CIN, array name) or Cin.getline (array name);
However, due to the flow input, the efficiency will be poor; If the buffer is full, the Cin.ignore () will be used.
Enter the characters with spaces:
scanf ("%c", &c); or cin>>c;.
Although CIN is convenient but inefficient, it is recommended to use scanf;

For general data input, there is not much problem, there is a problem to continue to add. Here are some other people's problems that I haven't met yet.

A written test often encounters a sudden string s;cin>>s;

Sometimes the compilation will be wrong, do not know why.

{

................................................................................................................................................

Today, when practicing enumeration types, you also encounter such a problem.

Enum Weekday{monday,tuesday} day;

cin>>day;//Error

scanf ("%d", &day);//Normal

Zhou Pure books are also used scanf, it seems scanf and printf can not casually discarded ah. After all, it's standard input and output stream, no buffers.

SCANF is the formatted input, and printf is the formatted output. CIN is the input stream, cout is the output stream. The efficiency is slightly lower, but the writing is simple.  formatted output is more efficient, but it is cumbersome to write code.  The flow output operation is slightly less efficient, but easy to write.  cout efficiency is low, as the first floor said, is to output something to buffer, and then output, resulting in reduced efficiency.  buffer comparison Abstract, for example:  once encountered such a situation (similar), int i; cout<< ' a '; cin>>i; cout<< ' b '; operation result nothing is seen in the output, Enter an integer type such as 3 and then press ENTER after the AB is also displayed. But this is not often the case, is in some of the more large projects occasionally appear, because the character a first to the buffer, but no output, and so on input i,b into the buffer and then output. Stream input is also the same.
..................................................................................................................................................................................................................................................................................................................
}

For specific uses of scanf:
The argument behind the scanf is the address of the variable to be written. So int A, b; It is necessary to call scanf in &a,&b form.
However, if it is a string, char s[10]; scanf ("%s", s); Parameter s do not &. Because S is the address form.
Http://bbs.bccn.net/thread-236258-1-1.html Here is a very detailed explanation.

To be Continued ...

is still input and output (character matrix, space, newline)

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.