C + +: Why the problem cannot be re-used after cin input CTRL + Z

Source: Internet
Author: User

hahaha, here I come again.

A problem occurred today when I wrote a program that required two cycles of input. Paste the code First:

#include <iostream>#include<string>#include<vector>#include<stdexcept>usingstd::cout;usingstd::cin;usingStd::endl;usingStd::cerr;usingSTD::string;usingstd::vector;usingStd::out_of_range;intCalcint (vector<string>vecstr) {    inti,sum;  for(i =0, sum =0; I!=vecstr.size (); + +i) {sum+=Stoi (vecstr.at (i)); }    returnsum;}DoubleCalcdou (vector<string>vecstr) {    inti; Doublesum =0.0;  for(i =0; I! = Vecstr.size (); + +i) {sum+=Stod (vecstr.at (i)); }    returnsum;}intmain () {vector<string>Vecstr; stringstr;  while(cin>>str)    {vecstr.push_back (str); }    Try{cout<<calcint (VECSTR) <<Endl; }    Catch(Out_of_range err) {cout<<err.what () <<Endl; cout<<"Conversion integer failed! "<<Endl;        } vecstr.clear ();  while(cin>>str)    {vecstr.push_back (str); }    Try{cout<<calcdou (VECSTR) <<Endl; }    Catch(Out_of_range err) {cout<<err.what () <<Endl; cout<<"Conversion integer failed! "<<Endl; }        return 0;}

The code executes as follows:

After I hit Ctrl + Z and the next car, the direct appearance of 270 and 0. However, there is code to know that I do not mean this, the intention is to output once, after the output.

But as if the second input failed, and I have no input.

Think about it, whether the status of CIN is set to 0, resulting in CIN can not input, forcibly enter the strong captivity, resulting in input failure. Therefore, a cin.clear () is added to the back of the while input loop, and the CIN flag bit is reset.

The code is as follows:

#include <iostream>#include<string>#include<vector>#include<stdexcept>usingstd::cout;usingstd::cin;usingStd::endl;usingStd::cerr;usingSTD::string;usingstd::vector;usingStd::out_of_range;intCalcint (vector<string>vecstr) {    inti,sum;  for(i =0, sum =0; I!=vecstr.size (); + +i) {sum+=Stoi (vecstr.at (i)); }    returnsum;}DoubleCalcdou (vector<string>vecstr) {    inti; Doublesum =0.0;  for(i =0; I! = Vecstr.size (); + +i) {sum+=Stod (vecstr.at (i)); }    returnsum;}intmain () {vector<string>Vecstr; stringstr;  while(cin>>str)    {vecstr.push_back (str); }    Try{cout<<calcint (VECSTR) <<Endl; }    Catch(Out_of_range err) {cout<<err.what () <<Endl; cout<<"Conversion integer failed! "<<Endl;    } vecstr.clear ();    Cin.clear ();  while(cin>>str)    {vecstr.push_back (str); }    Try{cout<<calcdou (VECSTR) <<Endl; }    Catch(Out_of_range err) {cout<<err.what () <<Endl; cout<<"Conversion integer failed! "<<Endl; }        return 0;}

The results are as follows:

It worked!

In that case, explain the principle carefully:

In fact, in the "c++primer5e" 8.1.2 in the IO condition of the state have a careful explanation, but the book is much, and some things speak more scattered, so some things I summed up here:

1.IO operation an innate problem is that errors can occur. Some errors can be recovered, and other errors occur deep in the system beyond the scope that the application can remediate. Therefore, the IO class has defined functions and flags that can help us access and manipulate the condition state of the flow. Specific conditions can be viewed by Baidu.

The condition status in 2.cin indicates that there are four flag bits, badbit,failbit,eofbit,goodbit. Where Badbit is set in the case of a stream crash, Failbit is set in the case of an IO operation failure, Eofbit is placed when the stream reaches the end of the file, Goodbit 0 indicates that the stream is not in error state, when Goodbit is 0 stream for the active state, Any of the other three flag bits will cause a stream error. The clear () function resets the stream to a valid state.

3.ctrl+z represents a file terminator, and Eofbit and Failbit are set when CIN reads. The stream is now in an invalid state.

4. Once a stream has an error, subsequent IO operations on it will fail.

The above is an overview of the principle.

C + +: Why the problem cannot be re-used after cin input CTRL + Z

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.