简体   繁体   中英

Understanding input validation in C++

So I have been looking up how to check that users enter the correct format when console application asks the user for data. For instance when the program asks the user for an amount of money it will correct the user if a letter or other character is entered. I found some code and understand how to "use" the code. "Use" meaning that I know where to put the pieces of code so that it runs properly but I don't really understand what the functions do and why some other codes are even need. I want to see if someone could help me understand what the functions do so when my codes begin to get more complex I can avoid careless mistakes. Below I listed a code with a few question about the code necessary for input validation that works find but as the question stated I don't really understand how the functions work. please help me out!!

#include <iostream>

using namespace std;

int main()
{
   int number;

   cout << "enter number: ";
   cin >> number;

   while (!cin) //I've seen this used and not used in different variation of input validation. is this effective? and what exactly does 'not cin'
             //mean to the computer?
   {
       cout << "not correct variable type: ";
       cin.clear(); //why do you need to clear cin? is it not cleared already when you are  
                 //inputing information? what information is there when entering information?

       while (cin.get() != '\n'); //What exactly does '.get()' do? Why are you looking for an end line character? Unless my understanding
                               //is not correct (probably isn't correct) the computer automatically goes 
                               //to the next line when you hit the 'enter' key.
       cin >> number;
   }

   cout << "great you entered a number and it is: " << number;
   cout << endl;
   system("pause");
   return 0;
}

Responses to your comments:

while (!cin) //I've seen this used and not used in different variation of input validation. is this effective? and what exactly does 'not cin'
             //mean to the computer?
{

If there was an error in reading the input, an error flag is set on cin . !cin evaluates to true if an error flag is set on the object. More details can be found at istream::operator!()

   cout << "not correct variable type: ";
   cin.clear(); //why do you need to clear cin? is it not cleared already when you are  
                //inputing information? what information is there when entering information?

The call to clear() clears the object of the error flags. If this is not done, any attempt to read using the object will fail. More details can be found at istream::clear() .

   while (cin.get() != '\n'); //What exactly does '.get()' do? Why are you looking for an end line character? Unless my understanding
                              //is not correct (probably isn't correct) the computer automatically goes 
                              //to the next line when you hit the 'enter' key.

This reads and discards all input until and including the newline character. That gives the user the chance to provide valid input in the next line.

   cin >> number;

This attempts to read input from the next line.

}
#include <iostream>
#include <cstdlib>

using namespace std;

   int main()
   {
     int number;

     cout << "enter number: "; 
     cin >> number;

   while (!cin) //while(!cin) means if there is no stream, then run the  block of code that follows
  {
   cout << "not correct variable type: ";
   cin.clear(); //You need to clear the stream in case there are any newline characters or special characters contained in the stream

   while (cin.get() != '\n'); //cin.get() will extract the stream until it reaches the first whitespace
   cin >> number;              //while(cin.get() != '\n\) is to prevent the stream cin from saving the newline character
                                //the user enters. since cin is a stream, it will accept basically everything including
                                //newline characters and special charaters like '\t' etc
 }
cout << "great you entered a number and it is: " << number;
cout << endl;
system("pause");
return 0;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM