C++ Series Five: Input/Output

1. Input

Input refers to the process of obtaining data from external sources. In C++, input data can be obtained through external sources such as keyboards or files.

(1)cin

cinIs an input stream object in the C++ standard library, used to read data from the standard input device (usually the keyboard). cinis an operator for reading data of different types.

eg read integers from user input:

#include <iostream>
using namespace std;

int main() {
    
    
    int num;
    cout << "Enter an integer: ";
    cin >> num;
    cout << "You entered: " << num << endl;
    return 0;
}

Running the above code, the output is:

Enter an integer: 10
You entered: 10

As can be seen from the above code, you first need to include iostreamthe header file, and then use using namespace std;to declare the std namespace. Then an integer variable num is defined, and the user is prompted to enter an integer. Finally, use cin>>numthe statement to store the input value in the num variable. It should be noted that >>it is an operator used to extract data from the input stream.

(2)getline

getlineis a function used to read a line of text. It can read a line of characters from a file or standard input and save the line to a string variable.

eg read a line of text

#include <iostream>
#include <string>
using namespace std;

int main() {
    
    
    string str;
    cout << "Enter your name: ";
    getline(cin, str);
    cout << "Hello, " << str << endl;
    return 0;
}

In the above code, a string variable str is defined, and then the user is prompted to enter a name. Next, use getline(cin, str)the function to read the entire line of data entered by the user and store it in the str variable. Finally, output the welcome message, which includes the name entered by the user.

(3) File input

In addition to taking input from the keyboard, C++ also allows reading input from files. To do file input, you need to open a file and then use cina redirected input stream.

eg read input from a file

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    
    
    ifstream infile("input.txt");
    int num;
    while (infile >> num) {
    
    
        cout << num << endl;
    }
    infile.close();
    return 0;
}

In the above code, first use to ifstreamopen the file named "input.txt", and then use whilea loop to read each integer in the file and output it to the screen. Finally, infile.close()close the input file with .

2. Output

Output refers to the process of sending data to an external source. In C++, output can be performed through console output or writing data to a file. The following are the most common exported functions in C++:

(1)cout

coutIs an output stream object in the C++ standard library, used to print data to the standard output device (usually the screen).

e.g

#include <iostream>
using namespace std;

int main() {
    
    
    cout << "Hello, world!" << endl;
    return 0;
}

In the above code, use coutto output the "Hello, world!" string, and use endlthe statement to add a new line.

(2) File output

In addition to outputting data to the console, C++ also allows data to be written to a file. For file output, open a file and use couta redirected output stream.

eg write data to file

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    
    
    ofstream outfile("output.txt");
    outfile << "Hello, world!" << endl;
    outfile.close();
    return 0;
}

ofstreamIn the above code, the output file named "output.txt" is first opened using the statement, and then outfile << “Hello, world!”the string is written to the file using the statement. Finally, outfile.close()close the output file with .

3. Formatted output

In some cases, it is necessary to output data in a specific format, such as floating point numbers with two decimal places. C++ provides some formatted output functions that can be used to control the format of the output data.

(1)setw

setw()function to set the output field width.

eg output an integer in a column with a field width of 10

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    
    
    int num = 12345;
    cout << setw(10) << num << endl;
    return 0;
}

Running the above code, the output is:

12345

In the above code, when outputting the num variable to the screen, setw(10)a function is used to specify the width of the output field.

(2)setprecision

setprecision()Function is used to set the precision of floating point numbers.

eg keep the floating point number with two decimal places

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    
    
    double num = 3.1415926535;
    cout << setprecision(2) << num << endl;
    return 0;
}

Running the above code, the output is:

3.14

The above code uses setprecision(2)functions to specify the output precision of floating point numbers.

(3) fixedandscientific

fixedand scientificare two functions that control the output format of floating-point numbers. fixedfunction to output floating point numbers with fixed decimal places, and scientificfunction to output floating point numbers in scientific notation.

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    
    
    double num = 1234567890.123456;
    cout << fixed << setprecision(2) << num << endl;
    cout << scientific << setprecision(2) << num << endl;
    return 0;
}

Running the above code, the output

1234567890.12
1.23e+009

In the above code, fixedthe function is used to output floating-point numbers in a fixed format, while scientificthe function is used to output floating-point numbers in scientific notation.

4. Summary

Naive record of learning C++ language 20 years ago

insert image description here

Guess you like

Origin blog.csdn.net/apr15/article/details/130538588