Thread: Examine my code please

  1. #1
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57

    Examine my code please

    I finished the new calculator

    I figured out how to make it so the user could imput the signs instead of having to put 2 numbers seperated by a space.

    Can you tell me waht you guys think?

    Code:
    #include <iostream>
    using namespace std;
    
    double add (double a, double b)//Addition Function
    {
        return (a + b);
    }
    double subtract (double c, double d)//Subtraction Function
    {
        return (c - d);
    }
    double multiply (double e, double f)//Multiplication Function
    {
        return (e * f);
    }
    double divide (double g, double h)//Division Function
    {
        return (g / h);
    }
    
    int main ()
    {
    double pick1, pick2;
    char choose, method;  
        cout << "*********************" << endl;
        cout << "***The Calculator****" << endl;
        cout << "**By : Ryan Nielson**" << endl;
        cout << "*********************\n\n" << endl; 
        do 
        {
            cout << "\n\nPlease type your question below. Use + to add, - to subtract," << endl; 
            cout << "* to multiply or / to divide.\n " << endl;
            cin >> pick1 >> method >> pick2;
            
            if (method == '+')//addition
                    cout << "\n" <<pick1 << method << pick2 << "=" << add (pick1, pick2) << endl;
            else if (method == '-')//substraction
                    cout << "\n" <<pick1 << method << pick2 << "=" << subtract (pick1, pick2) << endl;
            else if (method == '*')//multiply
                    cout << "\n" <<pick1 << method << pick2 << "=" << multiply (pick1, pick2) << endl;
            else if (method == '/')//divide
                    cout << "\n" <<pick1 << method << pick2 << "=" << divide (pick1, pick2) << endl;
            else
                    cout << "\nIncorrect input!!!!!" << endl;
            
            cout << "\nWould you like to [c]ontinue or [e]xit: ";
            cin >> choose; 
        }while ((choose != 'e') && (choose != 'E'));
    return 0;
    }
    Last edited by TWIXMIX; 03-20-2004 at 11:18 PM.

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    formatting is a wonderful thing.

  3. #3
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    You mean its good or do you mean its bad :-/
    Learning C++
    Programmer in training

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Well with your current method of distinction between which operation they want to put on the two numbers...umm....


    You don't even need all of those functions......

    Why don't you go.....

    Code:
    if (operator == "*")
    {
      cout << num1 * num2;
    }
    ?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  5. #5
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    I know I dont need all of them

    I forgot to say im using this to learn functions
    Learning C++
    Programmer in training

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Oh, ok then In that case yes your code is quite adequite. But what I would do(personally), is put those if statements in there own function, to make main() smaller and your code easier to decipher and catch errors.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  7. #7
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    how would I do that?
    Learning C++
    Programmer in training

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    >how would I do that?
    Code:
    void process()
    {
    double pick1, pick2;
    char choose, method;  
    
    do 
        {
            cout << "\n\nPlease type your question below. Use + to add, - to subtract," << endl; 
            cout << "* to multiply or / to divide.\n " << endl;
            cin >> pick1 >> method >> pick2;
            
            if (method == '+')//addition
                    cout << "\n" <<pick1 << method << pick2 << "=" << add (pick1, pick2) << endl;
            else if (method == '-')//substraction
                    cout << "\n" <<pick1 << method << pick2 << "=" << subtract (pick1, pick2) << endl;
            else if (method == '*')//multiply
                    cout << "\n" <<pick1 << method << pick2 << "=" << multiply (pick1, pick2) << endl;
            else if (method == '/')//divide
                    cout << "\n" <<pick1 << method << pick2 << "=" << divide (pick1, pick2) << endl;
            else
                    cout << "\nIncorrect input!!!!!" << endl;
            
            cout << "\nWould you like to [c]ontinue or [e]xit: ";
            cin >> choose; 
        }while ((choose != 'e') && (choose != 'E'));
    }
    Code:
    int main ()
    {
        cout << "*********************" << endl;
        cout << "***The Calculator****" << endl;
        cout << "**By : Ryan Nielson**" << endl;
        cout << "*********************\n\n" << endl; 
        process();
    return 0;
    }
    Last edited by Tronic; 03-21-2004 at 12:34 AM.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  9. #9
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    after i posted that i just thought of that.
    I also have to declar the pick and method variables in the processing function not in main() in order for it to work
    Learning C++
    Programmer in training

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    >I also have to declar the pick and method variables in the processing function not in main() in order for it to work

    Yes you would unless you declared an inline function.

    *goes to edit his code* I had an inkling(is that the right word? lol) that there would be scope issues.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  11. #11
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    what i meant is that your code seems very "busy" or "cluttered". I know its not exactly what you were asking us to examine, but its the first thing i notice.

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    I would also switch those if statements to switch statements.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  13. #13
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    yah it was RoD
    Thanks for the comment.
    So I should use switch statements.
    I usually dont use them unless wheres alot of choices
    Learning C++
    Programmer in training

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    You also might consider making the code in the if's easier to read. Modify each from:
    Code:
    if (method == '+')//addition
                    cout << "\n" <<pick1 << method << pick2 << "=" << add (pick1, pick2) << endl;
            else ...
    to
    Code:
    if (method == '+')//addition
        ans = add (pick1, pick2);
    else if (method == '-')//substraction
        ans = subtract (pick1, pick2);
    else ...
    
    cout << "\n" <<pick1 << method << pick2 << "=" << ans << endl;
            
    cout << "\nWould you like to [c]ontinue or [e]xit: ";
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM