Comparison of cin and cout and scanf and printf in c++

  • 2020-05-24 05:51:23
  • OfStack

cin cout,

Basic instructions:

cin stands for standard input device, using the extract operator "" > > "Get the data from the device keyboard, send it to the input stream object cin, and send it to memory.

cin is the input stream, cout is the output stream, overloaded." > > "," < < "Operator, contained in the header file < iostream > In the.

First put the output into the buffer, and then output, resulting in a loss of efficiency, cin automatically determines your variable type, such as 1 char data can only be retrieved with the default char method.

scanf, printf

Basic instructions:

scanf is the formatted input and printf is the formatted output, contained in the header file < stdio.h > In the.

Because scanf operates with Pointers, there is no type safety mechanism. For example, if you have 1 char type data, you can get the input with %f without error, but there will be an exception at run time.

The scanf() function will stop when it encounters carriage return, space, and TAB. For example 1, the first scanf() will fetch "Hello", while "world!" It is still in the buffer so that the second scanf will fetch the data directly without waiting for input from the terminal.

Case 1:


 #include <stdio.h>
 int main()
 {
  char str1[20], str2[20];
  scanf("%s",str1); 
  printf("%s\n",str1);  
  scanf("%s",str2); 
  printf("%s\n",str2); 
  return 0;
 }

Test 1 input:
Hello world!
Output:
Hello
world!

The first scanf() pulls out "Hello" and "world!" It is still in the buffer so that the second scanf will fetch the data directly without waiting for input from the terminal.

In order to avoid the above problems, it is necessary to clear the residual data of the buffer, which can be solved by the following methods:
Method 1: the C language provides a function to empty the buffer, as long as you empty the buffer before reading the data!
This function is fflush(stdin).
Method 2: extract the remaining data from the buffer by yourself.
To tell you the truth, I did not understand this sentence, hehe! Why format control is like this! Hope the master gives directions 1!
scanf("%[^\n]",string);

gets()

Basic instructions:

The gets() function is used to read the string from the standard input device (keyboard) until the end of the newline, but the newline is discarded and the '\0' character is added at the end. Included in the header file < stdio.h > In the.

gets(s) function and scanf("%s", & s) similar, but not identical, using scanf("%s", & One problem with the s () function is that if a space is entered, the string is considered to be finished, and the character after the space is treated as the next input item, but the gets() function will receive the entire input string until a new line is encountered.

Prototype:

char * gets (char * buffer);

Example 2:


#include <stdio.h>
int main()
{
  char str1[20], str2[20];
  gets(str1); 
  printf("%s\n",str1);  
  gets(str2); 
  printf("%s\n",str2); 
  return 0;
}

Testing:
Hello world! [enter]
Hello world! [output]
12345 [input]
12345 [output]

In order to avoid the above problems, it is necessary to clear the residual data of the buffer, which can be solved by the following methods:
Method 1: the C language provides a function to empty the buffer, as long as you empty the buffer before reading the data!
This function is fflush(stdin).
Method 2: extract the remaining data from the buffer by yourself.
To tell you the truth, I did not understand this sentence, hehe! Why format control is like this! Hope the master gives directions 1!
scanf("%[^\n]",string);

The principle of input operation

Like the scanf function 1 mentioned in the previous section, the input of the program is built with a buffer, that is, the input buffer. The first input process is such that when the first keyboard input is finished, the input data will be put into the input buffer, and the cin function will fetch the data directly from the input buffer. Because the cin function fetches data directly from the buffer, sometimes when there is residual data in the buffer, the cin function fetches the residual data directly without asking for keyboard input. This is why the input statement is invalid in the example!

Some input functions and operators of cin

cin is a extern istream object. A number of member functions and overloaded operators are available, such as cin < < , cin.get(), cin.getline(), etc. Let's take a look at these functions:

1. cin < <

This operator reads the data based on the type of the subsequent variable.
Enter end condition: Enter, Space, Tab keys are encountered.
Handling of end characters: discard end characters in the buffer that make the input end (Enter, Space, Tab)

2. cin. get ()

The function has three formats: no arguments, 1 argument, 2 argument

cin.get (), cin.get (char ch), cin.get (array_name, Arsize)

Reading of characters:

Enter the end condition: Enter key
For terminal handling: do not discard Enter in the buffer

cin.get () and cin.get (char ch) are used to read characters, their use is similar,
ch= cin.get () is equivalent to cin.get (ch).

Reading a string:

cin.get (array_name, Arsize) is used to read strings and can accept space characters. When Enter is finished, read characters by length (Arsize) and discard the last Enter character.

The program 6:


#include <iostream>
using namespace std;
int main ()
{
char a[20];
cin.get(a, 10);
cout<<a<<endl;
return 0;
}

Test 1 input:

abc def[Enter]

Output:

abc def

This function accepts Spaces when entering a string.

Test 2 input:

1234567890[Enter]

Output:

123456789

[analysis] if the input is too long, the data will be taken according to the required length.

Program 7:


#include <iostream>
using namespace std;
int main ()
{
char ch, a[20];
cin.get(a, 5);
cin>>ch;
cout<<a<<endl;
cout<<(int)ch<<endl;
return 0;
}

Test 1 input:

12345[Enter]

Output:

1234

53

[analysis] the first input is very long, the string is taken "1234" according to the length, and the '5' still remains in the buffer, so the second input character is not read from the keyboard, but directly taken '5', so the printed ASCII value is 53(ASCII value of '5').

Test 2 input:

1234[Enter]

a[Enter]

Output:

1234

97

[analysis] the second input is valid, indicating that the function has discarded the Enter after the first input!

3. cin. getline ()

cin.getline () reads in much the same way as cin.get (array_name, Arsize), ending with Enter and accepting space characters. Reading characters by length (Arsize) discards the last Enter character.

But the two functions are different:

cin.get (array_name, Arsize) does not cause errors in the cin function when the input string is too long, and the cin operation continues, simply fetching the data directly from the buffer. However, cin.getline () will cause an error in the cin function when the input is too long, and the subsequent cin operation will no longer be performed. (the reasons are explained in detail in the next section, "error handling for cin")

Program 8:


#include <iostream>
using namespace std;
int main ()
{
char ch, a[20];
cin.getline(a, 5);
cin>>ch;
cout<<a<<endl;
cout<<(int)ch<<endl;
return 0;
}

Test input:

12345[Enter]

Output:

1234

-52

[analysis] compared to the routine of cin.get (array_name, Arsize), it turns out that ch here does not read the 5 in the buffer, but returns -52, which is actually cin > > The ch statement did not execute because cin failed!


Related articles: