Calculator help

This is a discussion on Calculator help within the C++ Programming forums, part of the General Programming Boards category; I just started to read up on C++ programing, the closest ive been to programing is a few bash and ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    10

    Calculator help

    I just started to read up on C++ programing,
    the closest ive been to programing is a few bash and sh linux scripts..

    Anyway i have a problem with my "calculator" i can only get it to print out
    results with 4 decimals, i want it to be a little more accurate..

    Here is the code for my calc:
    Code:
    #include <iostream>
             int main ()
                 {
                      // Setting Variables needed for the calc
                      long double value1 = 0;
                      long double value2 = 0;
                      char calc;
    
                     // Getting the information needed to start calculating
                     cout << "What do you want to calculate? ( Example: 225*5 ) : ";
                       cin >> value1 >> calc >> value2;
    
                             // Starting to calculate...
    
                              if ( calc == '*' ) {
    
                                 long double result = value1 * value2;
                                     cout << value1 << calc << value2;
                                     cout << " = " << result;
                                                                      }
    
                              if ( calc == '/' ) {
    
                                 long double result = value1 / value2;
                                     cout << value1 << calc << value2;
                                     cout << " = " << result;
                                                                      }
    
                              if ( calc == '+' ) {
    
                                 long double result = value1 + value2;
                                     cout << value1 << calc << value2;
                                     cout << " = " << result;
                                                                      }
    
                              if ( calc == '-' ) {
    
                                 long double result = value1 - value2;
                                     cout << value1 << calc << value2;
                                     cout << " = " << result;
                                                                      }
    
        return 0;
        }
    If i do like 100/7 the result becomes: 100/7 = 14.2857

    Is there a way i havn't thought of that can make it more accurate?
    you dont have to give me code for it.. just hint me in the right direction.. i want to learn as much as possible for myself.

    And please don't laugh at the code, this is my first actual program (im not counting the "hello world" thing)


    edit: changed code according to bumfluff's recomendations
    Last edited by andreasleon; 01-06-2007 at 05:07 PM.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Well one thing, char isn't an argument of main.

    Also try sorting out your code layout, your if statements shouldn't really be to the left of your declaration of main.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    thank you, i have changed it so all if are to the right of main and removed char from it..
    code still compiles great.. i thought the (char) argument was needed to use it in the program, was compleatly my bad..

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can see more digits with float output using setprecision(int). (Include <iomanip>). Just remember that floats and doubles have a limited precision and you can't have too many significant digits.

    (With dev-cpp I'd Select All and unindent it a couple of times )

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    thanks alot.. that was exactly what i needed!
    and not to much information either..
    so i had to read read up on it

    thank you for a great awnser!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21