Thread: Simple Calculator

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    Smile Simple Calculator

    So this is something I made out of everything I've learned sofar.
    I post it here so I can get pointers on what to improve on.

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    void add(float x){
        cout << "Result: " << x << endl;
    };
    void sub(float x){
        cout << "Result: " << x << endl;
    };
    void mul(float x){
        cout << "Result: " << x << endl;
    };
    void div(float x){
        cout << "Result: " << x << endl;
    };
    
    
    int main()
    {
        float a;
        float b;
        int c;
    
    
        cout << "Loke302 Simple Calculator!\n" << endl;
        cout << "Enter a number: ";
        cin >> a;
        cout << "Enter a number: ";
        cin >> b;
        cout << "[1] To Add\n[2] To Subtract\n[3] To Multiply\n[4] To Divide" << endl;
        cin >> c;
    
    
        if (c == 1) {add(a + b);
        return 0;
        }
        else if (c == 2) {sub(a - b);
        return 0;
        }
        else if (c == 3) {mul(a * b);
        return 0;
        }
        else if (c == 4) {div(a / b);
        return 0;
        }
        else if (c >= 5) {
            cout << "Option doesn't excist!" << endl;
            return 1;
        }
        else if (c <= 0) {
            cout << "Option doesn't excist!" << endl;
            return 1;
        }
    }
    Last edited by Loke302; 12-01-2011 at 05:54 PM.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Loke302 View Post
    So this is something I made out of everything I've learned sofar.
    I post it here so I can get pointers on what to improve on.

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    void add(float x){
        cout << "Result: " << x << endl;
    };
    void sub(float x){
        cout << "Result: " << x << endl;
    };
    void mul(float x){
        cout << "Result: " << x << endl;
    };
    void div(float x){
        cout << "Result: " << x << endl;
    };
    
    
    int main()
    {
        float a;
        float b;
        int c;
    
    
        cout << "Loke302 Simple Calculator!\n" << endl;
        cout << "Enter a number: ";
        cin >> a;
        cout << "Enter a number: ";
        cin >> b;
        cout << "[1] To Add\n[2] To Subtract\n[3] To Multiply\n[4] To Divide" << endl;
        cin >> c;
    
    
        if (c == 1) {add(a + b);}
        else if (c == 2) {sub(a - b);}
        else if (c == 3) {mul(a * b);}
        else if (c == 4) {div(a / b);}
        else if (c >= 5) {
            cout << "Option doesn't excist!";
        }
        else if (c <= 0) {
            cout << "Option doesn't excist!";
        }
        return 0;
    }
    You misunderstood what functions should be used for and how.
    I'm showing you an example of how your add should ideally be.
    Code:
    double add(double x, double y)
    {
        return (x+y);
    }
    You could show the result by:
    Code:
    cout<<"Result: "<<add(a,b);

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    Quote Originally Posted by manasij7479 View Post
    You misunderstood what functions should be used for and how.
    I'm showing you an example of how your add should ideally be.
    Code:
    double add(double x, double y)
    {
        return (x+y);
    }
    You could show the result by:
    Code:
    cout<<"Result: "<<add(a,b);
    Ok thanks I'll do that then

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Last two if statements could be combined into one.
    Break out as much code from the if statements as you can. For example, store the result in a variable somewhere and print the result AFTER the if statement.
    Improving your indentation and code style would not be a bad thing either.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    BTW, I didn't try to compile but I think the
    Code:
    using  namespace std;
    is unnecessary in your current implementation.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by walla View Post
    BTW, I didn't try to compile but I think the
    Code:
    using  namespace std;
    is unnecessary in your current implementation.
    No it isn't .... unless you want to do the same thing by different 'using' statements...or prefixing std at dozens of places.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    Quote Originally Posted by manasij7479 View Post
    No it isn't .... unless you want to do the same thing by different 'using' statements...or prefixing std at dozens of places.
    He didn't use anything from std

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Incorrect. cout and cin lies in the std namespace. So does endl.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    Interesting..
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple calculator
    By jackalope in forum C Programming
    Replies: 4
    Last Post: 11-25-2010, 08:00 PM
  2. simple calculator
    By yacek in forum C Programming
    Replies: 10
    Last Post: 10-07-2010, 05:24 AM
  3. Simple calculator
    By princeyeni in forum C++ Programming
    Replies: 6
    Last Post: 05-05-2010, 03:54 AM
  4. very simple calculator
    By N_D_E in forum C Programming
    Replies: 4
    Last Post: 03-22-2010, 07:18 PM
  5. Need help with simple calculator
    By j_wolfe in forum C Programming
    Replies: 2
    Last Post: 09-15-2008, 09:00 AM