jeudi 18 juillet 2019

switch paths are unreachable [duplicate]

This question already has an answer here:

    #include <iostream> // for input and output
    #include <cstdlib>  // for rand() and srand()
    #include <string>
        using namespace std;

        int main()
    {
        cout << " hello user, pick choose a letter that corresponds with what u want to do " << endl;


        string letter;
        int x,y,z;
        int num;
       cout <<  "A)   Add two numbers\n"
        "B)   Subtract two numbers\n"
        "C)   Multiply two numbers\n"
        "D)  Divide two numbers\n"
        "X)   Exit program\n";
        cout << "what letter do u want?" << endl;
        cin >> letter;
        cout << "now pick the first number" << endl;
        cin >> x;
        cout << "now pick the second number" << endl;
        cin >> y;

        if(letter == "A" or "a")
        num = 0;
        if(letter == "B" or "b")
        num = 1;
        if(letter == "C" or "c")
        num = 2;
        if(letter == "D" or "d")
        num = 3;
        if(letter == "X" or "x")
        num = 4;
        else
            cout << " you chose the wrong character " << endl;
       switch(num){
            case 0:

                z = x + y;
                cout << "the answer is" << " " << z << endl;
                break;
            case 1:

                z = x - y;
                cout << "the answer is" << " " << z << endl;
                break;
            case 2:

                z = x * y;
                cout << "the answer is" << " " << z << endl;
                break;
            case 3:

                if(y < 0)
                {cout << "pick a number larger than that" << endl;
                    break;}
                z = x / y;
                cout << "the answer is" << " " << z << endl;
                break;
            case 4:
                cout << "you have chosen to exit this program, goodbye" << endl;
                break;

                    }
    return 0;
}

no matter what I input I am told that I have chosen to exit the program I expected to be able to reach cases 0-3 but am only able to reach 4.I tried to delete the last 3 if statements yet it still only responds to the last if statement. what should I do to be able choose the other case options

Aucun commentaire:

Enregistrer un commentaire