0

I'm trying to make a switch case calculator that will ask for an expression (e.g. 2+2) and print the answer, repeating the process until the user enters 'q'.

I can't figure out how to get the program to end when the user enters 'q'. the program below successfully asks for an expression, gives the answer, and asks for another. However when you enter an incorrect expression, it repeats the default case forever, including when you input 'q'.

I know the problem is to do with how I cin the variables, considering the operands are of type double and also something is wrong with the while loop, but I cant think of any alternatives, and can't seem to find a solution elsewhere.

int main() {

double operand1;
double operand2;
char operation;
double answer;

while (operation != 'q'){

cout << "Enter an expression:""\n";
cin >> operand1 >> operation >> operand2;


     switch(operation)
{
    case '+':
        answer = (operand1 + operand2);
        break;

    case '-':
        answer = (operand1 - operand2);
        break;

    case '*':
        answer = (operand1 * operand2);
        break;

    case '/':
        answer = (operand1 / operand2);
        break;


    default:
            cout << "Not an operation :/";
            return 0;

}

    cout <<operand1<<operation<<operand2<< "=" << answer<< endl;

}
 return 0;
}
Jamie S
  • 11
  • 3

2 Answers2

1

Since you are reading 3 variables at once, when you type q in console, it is assigned to operand1, but not the operation which is the terminator of the while loop - therefore, endless loop starts.

The basic problem is with the logic of the application. The quickest resolution of that code is below:

int main() {

    double operand1;
    double operand2;
    char operation;
    char more = 'y';
    double answer;

    while (more != 'n') {

        cout << "Enter an expression:""\n";
        cin >> operand1 >> operation >> operand2;

        switch (operation)
        {
        case '+':
            answer = (operand1 + operand2);
            break;

        case '-':
            answer = (operand1 - operand2);
            break;

        case '*':
            answer = (operand1 * operand2);
            break;

        case '/':
            answer = (operand1 / operand2);
            break;


        default:
            cout << "Not an operation :/";
            return 0;

        }

        cout << operand1 << operation << operand2 << "=" << answer << endl;
        cout << "Continue? (y/n) ";
        cin >> more;
    }
    return 0;
}
DevDio
  • 1,525
  • 1
  • 18
  • 26
0

A simple change will fix the prob:

int main()
{

    double operand1;
    double operand2;
    char operation = '8';
    double answer;


    while (operation != 'q'){

        cout << "Enter an expression:""\n";
        cin >> operand1 >> operation >> operand2;    
        switch(operation)
        {
            case '+':
                answer = (operand1 + operand2);
                break;

            case '-':
                answer = (operand1 - operand2);
                break;

            case '*':
                answer = (operand1 * operand2);
                break;

            case '/':
                answer = (operand1 / operand2);
                break;


            default:
                    cout << "Not an operation :/";
                    return 0;

        }

        cout <<operand1<<operation<<operand2<< "=" << answer<< endl;

        cout << "Wants to quit? Enter (q)"\n";
        cin >> operation;

    }
 return 0;
}
MKR
  • 19,739
  • 4
  • 23
  • 33