Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Learning C++: 'Complex' Calculator

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Engineer
Expert Cheater
Reputation: 1

Joined: 25 Nov 2007
Posts: 170

PostPosted: Sat Aug 14, 2010 2:21 pm    Post subject: Learning C++: 'Complex' Calculator Reply with quote

Hey, I'm learning c++ at the moment. I made this code and It works perfectly, but I was wondering if there is any way to improve it/if there are any shortcuts I could have taken Wink
The code is 120 lines long because I wanted as much room for improvement as possible, so I made a separate function for each big action the program needs to take (Also, I can just copypaste the code into another .cpp file)

Code:

#include <iostream>
using namespace std;

class Maths
{
    private:
    int mFirstNumber;
    char mOperator;
    int mLastNumber;

    public:
    Maths(int firstnumber, char operator0, int lastnumber)
    {
        mFirstNumber = firstnumber;
        mOperator = operator0;
        mLastNumber = lastnumber;
    }

    void ResetValues(int firstnumber, char operator0, int lastnumber)
    {
        mFirstNumber = firstnumber;
        mOperator = operator0;
        mLastNumber = lastnumber;
    }

    int CalculateResults()
    {
        {
            switch(mOperator){
            case '+':
            return mFirstNumber + mLastNumber;
            break;
            case '-':
            return mFirstNumber - mLastNumber;
            break;
            case '*':
            return mFirstNumber * mLastNumber;
            break;
            case '/':
            return mFirstNumber / mLastNumber;
            break;
            default:
            cout << "switch(mOperator) failed to detect char";
            return 5;
            break;
            }
        }
    }
};

int dFirstNumber = 0;
char cOperator = '+';
int dLastNumber = 0;
char cverted = 'i';
Maths Calculation(dFirstNumber, cOperator, dLastNumber);

void AskForInput()
{
    cout << endl << "First number: ";
    cin >> dFirstNumber;
    cout << endl << "Operator (+, -, *, /): ";
    cin >> cOperator;
    cout << endl << "Last number: ";
    cin >> dLastNumber;
}

void OutsideResetValues()
{
    Calculation.ResetValues(dFirstNumber, cOperator, dLastNumber);
}

void PrintResults()
{
    cout << "The result is: " << Calculation.CalculateResults() << "." << endl;
}

int StringToChar(string input)
{
    if (input == "yes")
    {
        return 'y';
    }
    else
    {
        return 'n';
    }
}
void AskIfWantMore()
{
    string choice = "no";
    cout << endl << "Do you want to do more calculations? (yes/no): ";
    cin >> choice;
    cverted = StringToChar(choice);

    switch(cverted)
    {
        case 'y':
        AskForInput();
        OutsideResetValues();
        PrintResults();
        AskIfWantMore();
        break;

        case 'n':
        cout << "Bye.";
        break;

        default:
        cout << "Error!";
        break;
    }
}
int main()
{

    AskForInput();
    OutsideResetValues();
    PrintResults();
    AskIfWantMore();

    return 0;
}
Back to top
View user's profile Send private message
Anden100
Grandmaster Cheater
Reputation: 0

Joined: 20 Apr 2007
Posts: 668

PostPosted: Sat Aug 14, 2010 2:55 pm    Post subject: Reply with quote

avoid all global variables, and remove the line "using namespace std;"
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sat Aug 14, 2010 6:25 pm    Post subject: Reply with quote

creating functions like that in your class effectively flags them as inline.

also you can use a stringstream for your conversion needs.
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Sun Aug 15, 2010 12:41 am    Post subject: Reply with quote

You're treating classes as an excuse to use globals. That is incorrect. Think of a class as an object that would have its own internal state.

Furthermore: http://en.wikipedia.org/wiki/Shunting-yard_algorithm -> http://en.wikipedia.org/wiki/Reverse_Polish_notation
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites