Jump to content

Help with switch in C++

Go to solution Solved by Chasem121,

Never mind, I realized that I had letter as a integer rather than a character.

For some reason, whenever I input a letter it always goes with the default choice. A, S, M, or D do not seem to want to work. Does anyone know what is happening?

#include <iostream>using namespace std;int main(){    int letter;    double num1, num2, answer;        cout << "Please input 2 numbers to begin.\n";    cin >> num1;    cin >> num2;    cout << "Please input a letter, A for addition, S for subtraction, M for multiplication, and D for division.\n";    cin >> letter;        switch (letter)    {        case 'A': { answer = num1 + num2;                    cout << "The sum of " << num1 << "and " << num2 << "is " << answer << ".\n";                    break;        }        case 'S': { answer = num1 - num2;                    cout << "The difference of " << num1 << "and " << num2 << "is " << answer << ".\n";                    break;        }        case 'M': { answer = num1 * num2;                    cout << "The product of " << num1 << "and " << num2 << "is " << answer << ".\n";                    break;        }        case 'D': {                    if (num2 != 0)                        cout << "The quotient of " << num1 << "and " << num2 << "is " << answer << ".\n";                    else                        cout << "Error: You can not divide by zero\n";                    break;        }        default: cout << "Error: Please input A, S, M, or D.\n";    }    return 0;}
Link to comment
https://linustechtips.com/topic/480198-help-with-switch-in-c/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×