Search code examples
c++mathrational-number

How to allow user input of numbers and operators?


how would I add a function that allows the user to input something like 2 + 2 or 10 / 5 then simply run the objects to calculate this as they would when they enter it manually using my "Enter first input" statements. So as part of the assignment I need to allow the user to input something like 10 / 5 + 1 / 2 in the console. I also need to be able to allow for operator overloading and I'm not sure if my program is currently allowing this. Any help would be appreciated. Thanks!

#include <iostream>
#include <conio.h>

using namespace std;

class Rational
{
    private:
        float numInput;

    public:
        Rational(): numInput(0)
        {}

        void getValues()
        {           
            cout << "Enter number: ";
            cin >> numInput;
        }

        void showValues()
        {
            cout << numInput << endl;
        }

        Rational operator + (Rational) const;
        Rational operator - (Rational) const;
        Rational operator * (Rational) const;
        Rational operator / (Rational) const;
};

Rational Rational::operator + (Rational arg2) const
{
    Rational temp;
    temp.numInput = numInput + arg2.numInput;
    return temp;
}

Rational Rational::operator - (Rational arg2) const
{
    Rational temp;
    temp.numInput = numInput - arg2.numInput;
    return temp;
}

Rational Rational::operator * (Rational arg2) const
{
    Rational temp;
    temp.numInput = numInput * arg2.numInput;
    return temp;
}

Rational Rational::operator / (Rational arg2) const
{
    Rational temp;
    temp.numInput = numInput / arg2.numInput;
    return temp;
}

int main()
{
    Rational mathOb1, mathOb2, outputOb;
    int choice;
    mathOb1.getValues();
    cout << "First number entered: ";
    mathOb1.showValues();
    cout << endl;
    cout << "Enter operator: + = 1, - = 2, * = 3, / = 4  ";
    cin >> choice;
    cout << endl;
    mathOb2.getValues();
    cout << "Second number entered: ";
    mathOb2.showValues();    cout << endl;

    switch (choice)
    {
        case 1:
            outputOb = mathOb1 + mathOb2;
            break;
        case 2:
            outputOb = mathOb1 - mathOb2;
            break;
        case 3:
            outputOb = mathOb1 * mathOb2;
            break;
        case 4:
            outputOb = mathOb1 / mathOb2;
            break;
        default:
            cout << "Invalid choice! " << endl;
    }
    cout << "Answer: ";
    outputOb.showValues();
    cout << endl;

    return 0;
}

Solution

  • You can't use cin >> {int}, that will just fail if you provide a char and you'll be stuck from there on.

    Just use std::getline and parse out the tokens from there:

    std::string expression;
    std::getline(std::cin, expression);
    

    You can then use any of the many, many ways to split a string into tokens as expressed in this question, and loop through the tokens and interpret them based on whether they are operators or numbers.