After learning about if…else… statements and while loops, we will further improve the program here. If the number of input errors (non-compliant inputs) exceeds the set limit of 3, the program will exit. This is a common operation, primarily for safety reasons.
Related Articles:
Elementary School Students’ Introduction to C++ Programming, a detailed guide on how to learn C++, beginner-friendly!Elementary School Students’ Introduction to C++ Lesson TwoElementary School Students’ Introduction to C++ Lesson ThreeElementary School Students’ Introduction to C++ Lesson Four (Conditional Statements)Elementary School Students’ Introduction to C++: Infinite Loops with While (5)For example, when entering a password for a bank card, if you input the wrong password three times, the card will be locked, or you may be unable to log in for the day. This is also to prevent loss of the bank card and to avoid repeated password attempts by others, which could lead to losses. Especially in app logins, if we do not implement protective measures, it would be possible to brute-force the bank card password.
#include <iostream>using namespace std;
int main(){ int n; int num = 0; // Count the number of errors
while (true) { cout << "Please enter the number of hours later:"; cin >> n;
if (n <= 100) { break; // Input is valid, exit the loop }
num = num + 1; // Accumulate errors cout << "Please enter a number less than 100" << endl;
// Exceeding three errors if (num >= 3) { cout << "Input error exceeded three times, program exiting!" << endl; return 0; // End the program directly } }
// Normal calculation if ((n + 3) % 12 == 0) { cout << 12 << " o'clock" << endl; } else { cout << (n + 3) % 12 << " o'clock" << endl; }
return 0;}
As we can see, we have added a variable num to count the number of input errors.
int num = 0;
In the while(true) infinite loop, as long as we do not exit the loop, meaning the input is incorrect, we will increment this variable by 1 and assign it to the variable num:
num = num + 1;
When the value of num is greater than or equal to 3, we will prompt the user and end the program.
Related Vocabulary
function Functionreturn Returnwhile Whileif Ifelse Elsebuild Buildrun Runtrue Truefalse Falsebefore Beforetoken Tokenerror Errordeclared Declaredbreak Break
int n; // Declare variable n as an integer
Variable: Similar to a container, like a truck that can carry different things; today it can carry fruits, tomorrow it can carry vegetables, and the contents can change.
In contrast, a constant is something that does not change, such as the number 1, which will always be 1 and cannot change. The constant π is also fixed and unchanging at 3.1415926… Although it is infinite and non-repeating, this value does not change to something like 4.5.
Declaration: Declaring a variable as an integer type, string type, etc., can also be called defining. Here, I use the analogy of a rice bucket and a trash can at home. For example, if we declare this clean large bucket as a rice bucket, it can only be used to hold rice, not trash; and this one we call a trash can, which is specifically for holding trash, not rice.
Additionally, the rice bucket has specifications, such as 30 kg, 50 kg, etc. If we pour more than 30 kg into a 30 kg rice bucket, it is clear that the rice will overflow, similar to how exceeding the range of an integer type will result in an error.
This intuitive and concrete explanation makes it easy for children to understand the meaning and function of variable declaration.It is still recommended to slow down the pace for beginners, and not to view these seemingly simple concepts with the eyes of someone who already knows them. These concepts require time to digest for those who are just starting to learn.I still highly recommend this C++ programming series, which is carefully organized from beginner to competition level. I personally really like this series, and most of the examples come from it.If you are in a hurry or want to master it quickly, you can choose corresponding books based on your learning level: