C ++ input Type Mismatch Detection Method

Source: Internet
Author: User

Today, I read the book C ++ primer plus, and I saw a method for detecting mismatched input types in C ++. The input Type Mismatch means that the input data type does not match the expected type, such as int n; cin> n. When the input data is a string, in this case, the input type does not match. So when this happens, does the value of Variable n change? How should we detect this situation? First, the value of Variable n has not changed. Unmatched strings remain in the input buffer. An error mark in the cin class is set to the return value of the bool type converted by the cin method to false. The following sample program uses the good () method to check whether the input has an error, check the error types respectively. First, check whether the EOF is encountered and use the eof () method. Then, check whether the input type does not match. Use the fail () method. Pay attention to fail () the method returns true if the EOF or input type does not match. You can also use the bad () method to detect input errors caused by file corruption or hardware errors.

// sumafile.cpp -- functions with an array argument#include <iostream>#include <fstream> // file I/O support#include <cstdlib> // support for exit()const int SIZE = 60;int main(){using namespace std;char filename[SIZE];ifstream inFile; // object for handling file inputcout << “Enter name of data file: “;cin.getline(filename, SIZE);inFile.open(filename); // associate inFile with a fileif (!inFile.is_open()) // failed to open file{cout << “Could not open the file “ << filename << endl;cout << “Program terminating.\n”;exit(EXIT_FAILURE);}double value;double sum = 0.0;int count = 0; // number of items readinFile >> value; // get first valuewhile (inFile.good()) // while input good and not at EOF{++count; // one more item readsum += value; // calculate running totalinFile >> value; // get next value}if (inFile.eof())cout << “End of file reached.\n”;else if(inFile.fail())cout << “Input terminated by data mismatch.\n”;elsecout << “Input terminated for unknown reason.\n”;if (count == 0)cout << “No data processed.\n”;else{cout << “Items read: “ << count << endl;cout << “Sum: “ << sum << endl;cout << “Average: “ << sum / count << endl;}inFile.close(); // finished with the filereturn 0;}

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.