Please help. I really think I'm close to getting this. No I'm just banging my head against a wall. I have to replace a nested if statement with a switch statement. The loop should continue until Z or z is entered. I can't figure out what I'm doing wrong.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>


using namespace std;

char letter;

int main()
{

  do {

        cout << "Enter a single letter, and I will tell you what that corresponding digit" << endl;
        cout << "is on the telephone keypad." << endl;
        cin >> letter;
        switch (letter)
        {
        case 'A':;
        case 'a':;
        case 'B':;
        case 'b':;
        case 'C':;
        case 'c':;
            cout << "2" << endl;
            break;
        case 'D':;
        case 'd':;
        case 'E':;
        case 'e':;
        case 'F':;
        case 'f':;
            cout << "3" << endl;
            break;
        case 'G':;
        case 'g':;
        case 'H':;
        case 'h':;
        case 'I':;
        case 'i':;
            cout << "4" << endl;
            break;
        case 'J':;
        case 'j':;
        case 'K':;
        case 'k':;
        case 'L':;
        case 'l':;
            cout << "5" << endl;
            break;
        case 'M':;
        case 'm':;
        case 'N':;
        case 'n':;
        case 'O':;
        case 'o':;
            cout << "6" << endl;
            break;
        case 'P':;
        case 'p':;
        case 'Q':;
        case 'q':;
        case 'R':;
        case 'r':;
        case 'S':;
        case 's':;
            cout << "7" << endl;
            break;
        case 'T':;
        case 't':;
        case 'U':;
        case 'u':;
        case 'V':;
        case 'v':;
            cout << "8" << endl;
            break;
        case 'W':;
        case 'w':;
        case 'X':;
        case 'x':;
        case 'Y':;
        case 'y':;
            cout << "9" << endl;
            break;
        case '&':;
            cout << "There is no digit on the telephone keypad that corresponds to &." << endl;
            break;
        }

     } while (letter != 'Z' || letter != 'z');
                cout << "Enter a single letter, and I will tell you what that corresponding digit" << endl;
                cout << "is on the telephone keypad." << endl;
                cin >> letter;
                break;

     {    







}

First, the break statement on line 97 has no business being there, and the { on line 99 has no business being there.

Secondly, your loop condition is fundamentally broken.

while (letter != 'Z' || letter != 'z');

The loop will continue as long as letter != 'Z' || letter != 'z' is true.

Let's imagine the letter is 'a'.
letter != 'Z' : this comes out as TRUE, because a is not the same as Z.
letter != 'z' : this comes out as TRUE, because a is not the same as z.
So letter != 'Z' || letter != 'z' becomes TRUE || TRUE, which is TRUE.

Now let's imagine the letter is 'z'.
letter != 'Z' : this comes out as TRUE, because z is not the same as Z.
letter != 'z' : this comes out as FALSE.
So letter != 'Z' || letter != 'z' becomes TRUE || FALSE, which is TRUE.

Now let's imagine the letter is 'Z'.

letter != 'Z' : this comes out as FALSE.
letter != 'z' : this comes out as TRUE.
So letter != 'Z' || letter != 'z' becomes FALSE || TRUE, which is TRUE.

The only way letter != 'Z' || letter != 'z' comes out as FALSE is if it's the same as FALSE || FALSE. Can you think of any letter that makes both sides FALSE?

Basically, your condition makes no sense and you didn't mean ||

Edited 1 Year Ago by Moschops

This article has been dead for over six months. Start a new discussion instead.