This is very similar to how firewalls protect computer networks from attackers; it won't prevent all attacks, but it does make a program much more resistant.
This process is called checking, validating, or filtering your data.
When the data first enters the program, or later by a lower-level routine that actually uses the data?
Although these flags live in ios_base, because ios is derived from ios_base and ios takes less typing than ios_base, they are generally accessed through ios (eg. ios also provides a number of member functions in order to conveniently access these states: Note that this program is expecting the user to enter an integer.
However, if the user enters non-numeric data, such as “Alex”, cin will be unable to extract anything to n Age, and the failbit will be set.
In nearly all secure programs, your first line of defense is to check every piece of data you receive.
If you can keep malicious data from entering your program, or at least keep it from being processed, your program becomes much harder to attack.