2

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;

    }
}
mch
  • 9,424
  • 2
  • 28
  • 42
Tahir
  • 59
  • 5
  • 3
    Show example input, current output, and an indication of what other output you want and why. – underscore_d Sep 17 '20 at 13:39
  • 2
    Very quick debugging step that this situation begs for: right before your `switch`, put a line similar to `cout << "UserChoice is '" << UserChoice << "'\n";`. Only after you have confirmed that `UserChoice` has the value you think it does should you blame your `switch` statement. – JaMiT Sep 17 '20 at 13:44
  • 1
    To execute the code in your `case` labels, try typing `43`, `45` or `47` in the console window, and then take a look at the ASCII table to see how it relates to your `char` symbols. – Ron Sep 17 '20 at 13:47

3 Answers3

8

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.

scohe001
  • 15,110
  • 2
  • 31
  • 51
4

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.

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
3

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;

    }
}
Саша
  • 837
  • 4
  • 12
  • 3
    I presume you actually mean that if the user enters a symbol when streaming to an `int`, it results in an `int` of `0` (rather than the desired `char`) and hence hits the `default` case. Is that right? It could be explained more clearly I think. – underscore_d Sep 17 '20 at 13:41
  • 3
    "and it is not possible to convert int to char," thats wrong. Only the values after the conversion are not what OP expects – 463035818_is_not_an_ai Sep 17 '20 at 13:43
  • 2
    @underscore_d In his case it is not possible because he is taking user input in int format and trying to print the result by using char in his switch statement, which is 100% wrong. – Саша Sep 17 '20 at 13:43
  • 4
    @Саша no it is not 100% wrong as long as you take into account the conversion and that chars are printed differently than ints – 463035818_is_not_an_ai Sep 17 '20 at 13:44
  • 3
    printing a `char` with value `1` is not the same as printing a char with value `'1'` – 463035818_is_not_an_ai Sep 17 '20 at 13:45
  • 2
    foo.cc:58:1: warning: no return statement in function returning non-void [-Wreturn-type] – Goswin von Brederlow Sep 17 '20 at 13:46
  • 1
    @idclev463035818 How he can process an int input using '+' in his switch statement? Write an answer it is interesting to me to know and learn ) – Саша Sep 17 '20 at 13:51
  • i think we are actually talking about the same. It is just the wording that I find disturbing, because it is possible to convert int to char (if you just take into account that the value of a char is not the same as the character represented by that integer value) – 463035818_is_not_an_ai Sep 17 '20 at 13:59
  • @idclev463035818 Yes Yo are right, It is possible and I also don't know which version of GCC he is using ) – Саша Sep 17 '20 at 14:07
  • 1
    @Tahir Also you can declare value1 and value2 with the double type of variable instead of int, to print float values as well. – Саша Sep 17 '20 at 14:51