Why is not Processing char in switch statement? C++

Viewed 122

I am trying to take and process the user decision by using char +, -, /, *, Why is switch statement ignoring them thus I don't see any mistake in my Code.

#include <iostream>
using namespace std;

void optionMenu();
double UserOutput(int);

int main ()
{
    int UserChoice;

    optionMenu();
    cout << " Choice: ";
    cin >> UserChoice;
    UserOutput(UserChoice);


    return 0;
}
void optionMenu()
{
    cout << " Select your choice" << '\n';
    cout << " + for Addition" << '\n';
    cout << " - for Subtraction" << '\n';
    cout << " / for Division" << '\n';
    cout << " * for Multiplication" << '\n';
}
double UserOutput (int UserChoice)
{
    int value1, value2;
    switch (UserChoice)
    {
        case '+':
        cout << " Enter First Number: "; cin >> value1;
        cout << " Enter Second Number: "; cin >> value2;
        cout << " The result for the Entered numbers is equal to: [" << (value1 + value2) << "]" << '\n';
        break;
        case '-':
        cout << " Enter First Number: "; cin >> value1;
        cout << " Enter Second Number: "; cin >> value2;
        cout << " The result for the Entered numbers is equal to: [" << value1 - value2 << "]" << '\n';
        break;
        case '/':
        cout << " Enter First Number: "; cin >> value1;
        cout << " Enter Second Number: "; cin >> value2;
        if(value2)
        cout << " The result for the Entered numbers is equal to: [" << value1 / value2 << "]" << '\n';
        else
            cout << " Not Allowed or Infinity, Try again!" << '\n';
        break;
        case '*':
        cout << " Enter First Number: "; cin >> value1;
        cout << " Enter Second Number: "; cin >> value2;
        cout << " The result for the Entered numbers is equal to: [" << value1 * value2 << "]" << '\n';
        break;
        default:
            cout << " Invalid Input Try again" << '\n';
            break;

    }
}
3 Answers

Your issue is right here:

int UserChoice;

optionMenu();
cout << " Choice: ";
cin >> UserChoice;

You're asking the user to enter +,-,/,* characters, but you're reading it into an int variable. This will cause std::cin to fail and 0 to be written into UserChoice (see here for more).

Instead, read that choice in as a char:

char UserChoice;
//^^

optionMenu();
cout << " Choice: ";
cin >> UserChoice;

Note that you also have the following warning:

main.cpp:60:1: warning: no return statement in function returning non-void [-Wreturn-type]

This is because you've specified UserOutput as a function returning a double, but you never return anything. You may want to change this to a void function to avoid mistakes/bugs in the future.

The problem is how you read the users choice:

int UserChoice;
cin >> UserChoice;

This tries to convert an integer in text from from the stream into an int. The stream contains for example "+". The parser sees '+' as next character, which is not a digit, stops and returns 0. The users input is actually never consumed.

To read the users choice you have use

char UserChoice;

PS: double UserOutput (int UserChoice) should be void.

Because you are sending user input in int format to switch statement and it is not possible to convert int to char directly in your case, so the int is ignored and returned the default stream of switch statement. To solve it just send and take the user input in char and it will work with char.

#include <iostream>
using namespace std;

void optionMenu();
double UserOutput(char);

int main ()
{
    char UserChoice;

    optionMenu();
    cout << " Choice: ";
    cin >> UserChoice;
    UserOutput(UserChoice);
    return 0;
}
void optionMenu()
{
    cout << " Select your choice" << '\n';
    cout << " + for Addition" << '\n';
    cout << " - for Subtraction" << '\n';
    cout << " / for Division" << '\n';
    cout << " * for Multiplication" << '\n';
}
double UserOutput (char UserChoice)
{
    int value1, value2;
    switch (UserChoice)
    {
        case '+':
        cout << " Enter First Number: "; cin >> value1;
        cout << " Enter Second Number: "; cin >> value2;
        cout << " The result for the Entered numbers is equal to: [" << (value1 + value2) << "]" << '\n';
        break;
        case '-':
        cout << " Enter First Number: "; cin >> value1;
        cout << " Enter Second Number: "; cin >> value2;
        cout << " The result for the Entered numbers is equal to: [" << value1 - value2 << "]" << '\n';
        break;
        case '/':
        cout << " Enter First Number: "; cin >> value1;
        cout << " Enter Second Number: "; cin >> value2;
        if(value2)
        cout << " The result for the Entered numbers is equal to: [" << value1 / value2 << "]" << '\n';
        else
        cout << " Not Allowed or Infinity, Try again!" << '\n';
        break;
        case '*':
        cout << " Enter First Number: "; cin >> value1;
        cout << " Enter Second Number: "; cin >> value2;
        cout << " The result for the Entered numbers is equal to: [" << value1 * value2 << "]" << '\n';
        break;
        default:
        cout << " Invalid Input Try again" << '\n';
        break;

    }
}
Related