Feiyu BLOG 2023.3.31
Information Technology Teaching
Python-Based Teaching
Research Topics
Academic Level Examination
Python Program Design
C++ Programming Olympiad
……


C++ programs process input data and then produce output. Common input methods include the cin>> statement and the formatted input function scanf().
cin Formatted Input
The cin input statement is used to input data from the keyboard. After execution, the program stores the input data in variables, and cin>> must be used in conjunction with variables.
cin>> Statement Format
The cin statement combines cin and >> together, formatted as follows:
Format 1: cin>>variable;
Function: To obtain a single data point and assign it to a variable, for example, cin>>b;
Format 2: cin>>variable1>>variable2>>variable3;
Function: To input multiple data points from the keyboard and assign them to corresponding variables, such as cin>>a>>b; which assigns input data to variables a and b.
When writing programs, when inputting multiple data points consecutively, the data can be separated by spaces. For example, the statement cin>a>>b; assigns 12 to variable a and 35 to variable b. You cannot input 1235; you must input 12 35.
Reference Program:
#include <iostream>
using namespace std;
int main(){
int t1,t2,t3,t4,t5;
cin >> t1 >> t2 >> t3 >> t4 >> t5;
t1=t1/3;
t2=t2+t1;
t5=t5+t1;
t2=t2/3;
t1=t1+t2;
t3=t3+t2;
t3=t3/3;
t2=t2+t3;
t4=t4+t3;
t4=t4/3;
t3=t3+t4;
t5=t5+t4;
t5=t5/3;
t4=t4+t5;
t1=t1+t5;
cout << t1 << ' ' << t2 << ' ' << t3 << ' ' << t4 << ' ' << t5;
return 0;
}
Execution Result:

Characteristics of cin Statements:
In cin statements, space characters and newline characters are delimiters and do not get input into variables.
Ensure input consistency. The number, order, and type of data must match the variables. If too much data is input, the excess will be ignored.
The cin statement can be written on one line or spread across multiple lines.
Advanced Improvement
1. Read the following program segment, think about the program execution process, and fill in the final execution result on the line below.
#include <iostream>
using namespace std;
int main(){
int s,a,b;
cin >> s >> a >> b;
s-=a;
s-=b;
cout << "s=" << s << endl;
}
Execution Result: _________________
2. A rabbit fell into a dry well. The well is too deep, and it is difficult for it to jump out, but it believes that it can eventually climb out through persistence. If the well is 2 meters deep, and the rabbit can jump a maximum height of x meters each time, try programming:
Input the value of x and output how far it is from the well’s mouth.
For example, input: 0.5 then output: 1.5; input 0.7 then output 1.3.
scanf Formatted Input
In C++, when inputting large data with specific format requirements, using scanf for input is more efficient and faster than using cin.
Format of scanf() Input Function
Format: scanf(“format specifier”, address list);
Function: To input values according to the specified format.
For example: scanf(“%d”, &a); inputs an integer from the keyboard and assigns it to variable a. The & is the address operator, indicating the address of the variable. Input data is like delivering a package to a variable; knowing the variable’s address is essential for successful delivery.
Example Problem: Jiji wants to measure the thickness of a textbook with a ruler, but a single sheet of paper is too thin to measure accurately. Following the teacher’s suggestion, Jiji adopts the method of measuring the thickness of multiple sheets at once and then calculating the average to determine the thickness of a single sheet (accurate to 0.01 mm).
Test Program:
#include <iostream>
#include "cstdio"
using namespace std;
int main(){
int x,y,n;
float h1,h2,h3,h;
scanf("%d%d", &x, &y);
scanf("%f%f%f", &h1, &h2, &h3);
n=(y-x+1)/2;
h=(h1+h2+h3)/3;
printf("%.2f\n",h/n);
return 0;
}
Execution Result:

Format Specifiers for scanf()
scanf() is a standard library function, and you need to include it in the header section with #include <cstdio>. The format specifiers are used to specify the input format, starting with % followed by a format character.
%d, used for inputting integers
%ld, used for inputting long integers
%f, used for inputting floating-point numbers
%c, used for inputting a single character
%s, used for inputting string type
Characteristics of scanf() Function
scanf(“format specifier”, address list) functions to store variable values at the memory addresses of the variables.
Note:
(1) The address list in the scanf function should be the addresses of the variables, not the variable names. For example, scanf(“%d”, &a); should not be written as scanf(“%d”, a); & is the address operator and cannot be omitted!
(2) If there are other characters in the “format specifier” string besides the format specifiers, then during data input, the corresponding characters must match those characters. For example: scanf(“h=%f,i=%f,j=%f”, &h, &i, &j); requires inputting data as: h=9.5,i=6.9,j=8.6. You cannot input: 9.5 6.9 8.6.
Advanced Improvement
1. Read the following program segment, think about the program execution process, and fill in the final execution result on the line below.
#include <iostream>
#include "cstdio"
using namespace std;
int main(){
int ch1,ch2;
scanf("%d%d", &ch1, &ch2);
printf("%c %c", ch1, ch2);
return 0;
}
Input: 65 66 Execution Result: ____________
2. Programming Problem
During lunch, the school cafeteria delivers boxed meals and soup to the classroom. It is known that there are L liters of soup, and the soup containers used by students are 160mm long, 105mm wide, and 50mm deep. Assume each student only serves soup once and cannot fill it too full, only serving soup to a depth of 45mm. Try programming to calculate how many students can drink from L liters of soup (Hint: cleverly use “%.0f” to round the result).
For example: input 40, output 53.
1.IC011 – C++ Output Statements2.IC007 – C++ Data Types (Integer)3.IC008 – C++ Data Types (Floating Point)4.IC009 – C++ Data Types (Character) / Type Casting5.IC010 – C++ Assignment Operations
Improve 1% Every Day, and after a year you can achieve37times growth; if you regress by1%every day, you will weaken and even regress to zero; even a small change or a good habit can produce an amazing compounding effect!