Thread: why cin.good does not work in this code?

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    13

    why cin.good does not work in this code?

    Code:
    1#include <iostream>
      2 #include <string>
      3 
      4 using namespace std;
      5 
      6 class Matherror
      7 {
      8 private:
      9         string error_mes;
     10 public:
     11         Matherror (const string error)
     12         {
     13                 error_mes=error;
     14         }
     15 
     16         string &  what ()
     17         {
     18                 return error_mes;
     19         }
     20 
     21 };
     22 
     23 int main ()
     24 {
     25         int x,y;
     26 
     27         try
     28         {
     29                 cout << "please input two intergrator : x= y= " << endl;
     30 
     31                 cin >> x >> y;
     32 
     33 
     34                 if (y==0)
     35                         throw Matherror ("derive by zero!");
     36 
     37                 else if (!cin.good ())
     38                         throw string ("one of the input is not integer");
     39 
     40                 throw (1);
     41 
     42         }
     43 
     44         catch (Matherror s) {cerr << s.what () << endl;}
     45         catch (string str) {cerr << str << endl;}
     46         catch (...) {cout << x%y << endl;}
     47         return 0;
    48 }
    Hi everyone
    The string "one of the input is not integer" should be outputted when characters are entered.Howerver, the "derive by zero!" is outputted when characters are entered, so it seems !cin.good () does not work. what is wrong?

    Fan Li

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Check your if() statement. Remember when the stream fails the variable is set to zero and the failbit is set.
    See this link.
    If extraction fails, zero is written to value and failbit is set. If extraction results in the value too large or too small to fit in value, std::numeric_limits<T>::max() or std::numeric_limits<T>::min() is written and failbit flag is set.
    So because y was set to zero the Matherror() is thrown.

    Also why are you throwing 1 if everything passes?

    Jim

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    13
    Hi Jim
    I understand. I am a beginner and I took this code from my textbook, but it seems too old, so it did not mention what you said.

    As for the reason why 1 is threw, there is no explanation, so I don't know. It seems there is a better code if everything passes from your answer, would you tell me what is it?

    Fan Li

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    would you tell me what is it?
    If you must use exceptions then you should first test the stream state, then test for zero.

    Instead of throwing when everything is good just let the code continue.

    Code:
    #include <iostream>
    #include <string>
    #include <exception>
    
    using namespace std;
    
    class Matherror
    {
        private:
            string error_mes;
        public:
            Matherror(const string error)
            {
                error_mes = error;
            }
    
            string &  what()
            {
                return error_mes;
            }
    
    };
    
    int main()
    {
        int x, y;
    
        try
        {
            cout << "please input two intergrator : x= y= " << endl;
    
            cin >> x >> y;
    
            if(!cin)
                throw string("one of the input is not integer");
            else if(y == 0)
                throw Matherror("derive by zero!");
    
        }
        catch(Matherror s) {
            cerr << s.what() << endl;
        }
        catch(string str) {
            cerr << str << endl;
        }
    
        // No exceptions thown so continue with program.
        cout << "No exceptions: ";
        cout << x % y << endl;
    
        return 0;
    }

    Jim

  5. #5
    Registered User
    Join Date
    Jul 2016
    Posts
    13
    Thanks for your code and explanation.

    Fan Li

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does this example code work good?
    By Todd_65536 in forum C Programming
    Replies: 2
    Last Post: 11-25-2013, 03:11 PM
  2. '\b' '\n' '\r' etc - good code?
    By CoffeCat in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2012, 11:53 PM
  3. is this code is good?
    By ExDHaos in forum C++ Programming
    Replies: 3
    Last Post: 05-23-2009, 09:56 AM
  4. does anyone have good code????
    By K&J in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2001, 10:32 AM
  5. I know OpenGL...now i want a good project 2 work on...
    By Zeeshan Zia in forum Game Programming
    Replies: 2
    Last Post: 10-20-2001, 12:18 PM

Tags for this Thread