Thread: c ++ calculator division program with error handling

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    2

    c ++ calculator division program with error handling

    Hello guys, pls im supposed to create a c++ program to calculate two input numbers. I'm also expected to add error handling but it keeps giving me errors. i have commented on it so you can have an idea what i'm trying to do. If i run the program straight it works, but if i'm to use an alphabet for either numerator or denominator it fails. thanks //division calculator with error handling #include using namespace std; int main(int argc, char* argv[]) { int returnvalue; float numerator = 0; float denominator = 0; float divresult = 0; cout numerator; if(cin.fail()) //if the value put in is not a number (0-9), it fails { cerr

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Use code tags !

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by manasij7479 View Post
    Use code tags !
    And line-breaks!

    o.O
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  4. #4
    Registered User R41D3N's Avatar
    Join Date
    Mar 2012
    Location
    ...Beyond Infinity, Behind a KeyBoard...
    Posts
    29
    Code:
    #include "<iostream>"
    
    
    using namespace std;
    
    
    
    
    int main(int argc, char* argv[]) 
    { 
        int returnvalue; 
        float numerator = 0; 
        float denominator = 0; 
        float divresult = 0; 
        cout "<<" numerator; 
        if(cin.fail()) //if the value put in is not a number (0-9), it fails
    As per your current code, the changes are in "". Though it seems kinda incomplete?
    Last edited by R41D3N; 06-12-2012 at 08:46 PM. Reason: :/

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    2
    Quote Originally Posted by manasij7479 View Post
    Use code tags !
    Sorry



    pls you should first understand that i started learning programming about 2 months ago so i'm very much a newbie lol, also, i don't get what you're saying(R41D3N) - but it is insisted i follow this format. i have refined it a bit but i'm having problem if i use an alphabet as numerator, then it'll be blank but the compiler is waiting for the denominator, how do i get that info to show? also if at the first instance i use an alphabet in the denominator, even if it does the division, the answer will be zero(that means it has reset my numerator to 0) thanks


    Code:
    //division calculator with error handling
    #include <iostream>
    
    using namespace std;
    int main(int argc, char* argv[])
    {
          int returnvalue;
          float numerator = 0;
          float denominator = 0;
          float divresult = 0;
          
          
          cout <<"Please enter the numerator:"; //the numerator is put in
          cin >>numerator; 
          if(cin.fail()) //if the value put in is not a number (0-9), it fails
          
           {
                       cerr<<"Invalid Input\nUse only Numbers"<<endl;//information regarding the error and advising for input change
                       cin.clear();
                       char discardit[3];
                       cin>>discardit;
                       char exitchar;
                       cout <<"Please enter the numerator:";
                    cin >>numerator;                          
            }
                            
          else// but if the numerator is correct (0-9), it moves here
          
                   cout<<"Please enter the denominator:";
                   cin>>denominator;
                
               if(cin.fail()) //if the denominator is not a number(0-9), it fails       
                             {
                              cerr<<"Invalid Input\nUse only Numbers"<<endl;//information regarding the error and advising for input change
                              cin.clear();
                            char discardit[3];
                              cin>>discardit;
                            char exitchar;
                              cout<<"Please enter the denominator:";
                           cin>>denominator;
                    
                              }
                         // but if the denominator is correct then...
                    
                    else if(denominator==0)//...it tests to make sure the input is not zero
                        {
                        cout<<"Cannot divide by zero"<<endl;
                                  cout<<"please enter the denominator:";
                               cin>>denominator;
                             }
                           else // but if it isn't zero, it moves here and does the division
                        divresult = (numerator/denominator);
                                cout<<divresult<<endl;
                                    
                returnvalue = 0;
                               return returnvalue;// returns the result.               
                            
    }//end of program

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    i have refined it a bit but i'm having problem if i use an alphabet as numerator, then it'll be blank but the compiler is waiting for the denominator, how do i get that info to show? also if at the first instance i use an alphabet in the denominator, even if it does the division, the answer will be zero(that means it has reset my numerator to 0) thanks
    I expect that you would have problems if the alphabet were used anywhere in simple divisions, like C++ can do. Since all the input is float type, then you can only proceed if the input is numerical. An if will perform a test and then the program will proceed based on the results. If the user is persistent in his errors, it will only cause bugged behavior in the program. So, the first step is to ditch the if-else logic in the current program and use a loop.

    For, While, and Do While Loops in C++ - Cprogramming.com
    I recommend the while loop for input-related tasks.

    After you get a good float numerator and denominator you can then check for zeros and do division, but only then does it work.

    It may look like the division result is being calculated all the time but that is not actually true.
    Code:
    else // but if it isn't zero, it moves here and does the division
        divresult = (numerator/denominator);
    cout<<divresult<<endl;
    Everything under the else is indicated in blue. You are just printing divresult all of the time, which is probably zero, unless you take a strange code path (And I already told you your logic is not reliable.) so brace together all of your else statements, like you know how to do.

    Programming is not a sloppy discipline, okay?

    edit: OK, so I'm about ten minutes late and it appears I helped a slacker. Great.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    The code you have pasted is sequentially and logically incorrect. It is very important to understand what the code is going to do. Just read it from top:
    You are asking user to input numerator, If it fails then you are asking again to input numerator. And if it passed or failed again then!!! Did you observer where your code flow is going?
    The 'else' where you are saying "but if the numerator is correct (0-9), it moves here", the control of else is up-till next statement only. So in any case "cin>>denominator" will execute.
    I will suggest write down your logic (flow) on a piece of paper without considering the syntax of C++. It will easy your life forever.
    S_ccess is waiting for u. Go Ahead, put u there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in calculator program
    By aaronson2012 in forum C++ Programming
    Replies: 6
    Last Post: 04-07-2010, 08:48 AM
  2. Replies: 5
    Last Post: 10-14-2009, 05:47 AM
  3. Floating point error when performing division
    By kezman in forum C Programming
    Replies: 3
    Last Post: 06-22-2009, 11:08 AM
  4. calculator program error
    By herWter in forum C++ Programming
    Replies: 7
    Last Post: 09-19-2008, 04:57 PM
  5. double division error (I get -0.0000)
    By dorakura in forum C Programming
    Replies: 6
    Last Post: 03-07-2008, 07:33 PM