Search code examples
c++stringgetline

Splitting a getline() string into multiple int and char variables?


I'm having some trouble understanding how to properly manipulate strings. The program below is a simple calculator.

When I put the input directly into variables through multiple cin statements, everything worked fine. Now, I want to take the input as a string with getline() and store the numbers/operators in existing variables from the getline().

My main issue is that I want the program to recognize both 2+2 and 2 + 2.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
    int Num1, Num2, Num3 = 0, result;
    char Operator1, Operator2 = 0;
    string input, in1;

    cout << "Enter your equation on one line.\n";
    getline(cin, input);


    //this is where getline needs to be manipulated into Num1/2/3 and Operator1/2

    cout << input;


    if (Operator2 != 0)
    {
        if (Operator1 == '+')
        {
            if (Operator2 == '+')
            {
                result = Num1 + Num2 + Num3;
                cout << "I made it to A!";
            }
            else if (Operator2 == '-')
            {
                result = Num1 + Num2 - Num3;
            }
            else if (Operator2 == '/')
            {
                result = Num1 + Num2 / Num3;
            }
            else if (Operator2 == '*')
            {
                result = Num1 + Num2 * Num3;
            }
        }
        else if (Operator1 == '-')
        {
            if (Operator2 == '+')
            {
                result = Num1 - Num2 + Num3;
            }
            else if (Operator2 == '-')
            {
                result = Num1 - Num2 - Num3;
            }
            else if (Operator2 == '/')
            {
                result = Num1 - Num2 / Num3;
            }
            else if (Operator2 == '*')
            {
                result = Num1 - Num2 * Num3;
            }
        }
        else if (Operator1 == '/')
        {
            if (Operator2 == '+')
            {
                result = Num1 / Num2 + Num3;
            }
            else if (Operator2 == '-')
            {
                result = Num1 / Num2 - Num3;
            }
            else if (Operator2 == '/')
            {
                result = Num1 / Num2 / Num3;
            }
            else if (Operator2 == '*')
            {
                result = Num1 / Num2 * Num3;
            }
        }
        else if (Operator1 == '*')
        {
            if (Operator2 == '+')
            {
                result = Num1 * Num2 + Num3;
            }
            else if (Operator2 == '-')
            {
                result = Num1 * Num2 - Num3;
            }
            else if (Operator2 == '/')
            {
                result = Num1 * Num2 / Num3;
            }
            else if (Operator2 == '*')
            {
                result = Num1 * Num2 * Num3;
            }
        }
        else
        {
            cout << "I don't recognize that operator. Did you type in one of these?: + - * /";
        }
    }
    else if (Operator2 == 0)
    {
        if (Operator1 == '+')
    {
            result = Num1 + Num2;
    }
        else if (Operator1 == '-')
    {
            result = Num1 - Num2;
    }
        else if (Operator1 == '*')
    {
            result = Num1 * Num2;
    }
        else if (Operator1 == '/')
    {
            result = Num1 / Num2;
    }
        else
    {
        cout << "I don't recognize that operator. Did you type in one of these?: + - * /";
    }

        result = Num1 + Num2;
        cout << "I made it to B!";
    }


    cout << "Your result is: " << result << endl << endl;

    return 0;
}

Any help would be appreciated, but I'd much prefer an explanation to working code.

I'm not interested in the math logic or using namespace std aspects of the program.


Solution

  • First of all, you should ask yourself whether it is necessary to store the entire string, instead of just using cin on the variables directly, as you said you did previously.

    If you really do want to store the entire string (as you do now for echoing it), you could consider using a stringstream (you're already including sstream in your file, for some reason):

    getline(cin, input);
    
    std::istringstream iss(input);
    // you can now use iss just as cin.
    // I'm not sure exactly what you want to do, but it would look something like this:
    iss >> num1;
    iss >> Operator1;
    iss >> num2;
    
    cout << input;