Hi Guys!

Was wondering what I was doing wrong with this program. This a program that when a person enters a number, 2-9, it gives them the letters that correspond just like on your phone, e.g., 2 is a,b,c. They also have to chose if they want a switch statement or a nested by selecting 1 or 2 to give them their answer. I am getting some errors with the else statement that connects the switch and the nested. Can anybody show me what I am doing wrong. It must be able to run on either the switch or nested depending on user selection. Thanks again from a new programmer! :)

int main()
{
    int digit;
    int character;
    int select = -1;

    
   {
        cout << "What function do you want to use : " << endl;
        cout << "1) Switch Statement " << endl;
        cout << "2) Nested Statement" << endl << endl;
        cout << "Enter: ";
        cin >> select;
        if (select == 1)
        {
            cout << " Enter the letter you want to see as a number: ";
            cin >> character;
            switch (digit)
             {
                   case '2':
                        cout << " A, B, C";
                   case '3':
                        cout << " C, D, E";
                   case '4':
                        cout << " G, H, I";
                   case '5':
                        cout << " J, K ,L";
                   case '6':
                        cout << " M, N, O";
                   case '7':
                        cout << " P, Q, R, S";
                   case '8':
                        cout << " T, U, V";
                   case '9':
                        cout << " W, X, Y, Z";
                   default:
                        cout << digit<< "Error! Is not a number between 2-9 ."
                        << endl;                                     
            return 0;
        }
        else 
         if (select == 2)
        {
            cout <<" Enter the number you want see its corresponding letters: ";
            cin >> digit;
            if ( digit == 2)
            cout << digit << " A, B, C" << endl;
            else
                if ( digit == 3)
            cout << digit << " D, E, F" << endl;
            else
                if ( digit == 4)
            cout << digit << " G, H, I" << endl;
            else
                if ( digit == 5)
            cout << digit << " J, K, L" << endl;
            else
                if ( digit == 6)
            cout << digit << " M, N, 0" << endl;
            else
                if ( digit == 7)
            cout << digit << " P, Q, R, S" << endl;
            else
                if ( digit == 8)
            cout << digit << " T, U, V" << endl;
            else
                if ( digit == 9)
            cout << digit << " P, Q, R, S" << endl;
            else
            cout << digit << "Error! Is not a number between 2-9 ." << endl;
            system ("pause");
            return 0;
        }
      
        else
            cout << "Valid options are only 1 or 2." << endl;
      system ("pause");
    
        }
      
    return 0;
}

Recommended Answers

All 4 Replies

digit isn't given a value if select == 1. change the variable used by the switch statement to character instead of digit. Place break statements after the output statement in each case. Otherwise all digit values after the entered value of character will be displayed.

Just to clarify what Lerner posted, you need to have if ( digit ==[B]'2'[/B]) instead of if ( digit == [B]2[/B] ) on lines 46,49,52 etc.
(I hope you know why? If not, ask us back)
Also, you must add a break; after line 21 23 25 etc.
Otherwise, all the following statement will be executed. To say, if the user choose to use the select-case(by entering 1 in the main menu), and Enters 2 as the number, your program will print

A, B, C
D, E, F
G, H, I
and so on

Hey guys,

I made the changes that you said but it is giving me a syntax error on the line the else that connects the switch and nested as well as the else that connects the nested with the error output. Let me include the revised code and see if you can give me some insight. I hope I am doing the code wrapping right for the post!

#include <iostream>
using namespace std;

int main()
{
    int digit;
    int character;
    int select = -1;

    
   {
        cout << "What function do you want to use : " << endl;
        cout << "1) Switch Statement " << endl;
        cout << "2) Nested Statement" << endl << endl;
        cout << "Enter: ";
        cin >> select;
        if (select == 1)
        {
            cout << " Enter the letter you want to see as a number: ";
            cin >> character;
            switch (digit)
             {
                   case '2':
                        cout << " A, B, C";
                        break;
                   case '3':
                        cout << " C, D, E";
                        break;
                   case '4':
                        cout << " G, H, I";
                        break;
                   case '5':
                        cout << " J, K ,L";
                        break;
                   case '6':
                        cout << " M, N, O";
                        break;
                   case '7':
                        cout << " P, Q, R, S";
                        break;
                   case '8':
                        cout << " T, U, V";
                        break;
                   case '9':
                        cout << " W, X, Y, Z";
                        break;
                   default:
                        cout << digit<< "Error! Is not a number between 2-9 ."
                        << endl;                                     
            return 0;
        }
        
        else 
        if (select == 2)
        
        {
            cout <<" Enter the number you want see its corresponding letters: ";
            cin >> digit;
            if ( digit == '2')
            cout << digit << " A, B, C" << endl;
            else
                if ( digit == '3')
            cout << digit << " D, E, F" << endl;
            else
                if ( digit == '4')
            cout << digit << " G, H, I" << endl;
            else
                if ( digit == '5')
            cout << digit << " J, K, L" << endl;
            else
                if ( digit == '6')
            cout << digit << " M, N, 0" << endl;
            else
                if ( digit == '7')
            cout << digit << " P, Q, R, S" << endl;
            else
                if ( digit == '8')
            cout << digit << " T, U, V" << endl;
            else
                if ( digit == '9')
            cout << digit << " P, Q, R, S" << endl;
            else
            cout << digit << "Error! Is not a number between 2-9 ." << endl;
            system ("pause");
            return 0;
        }
      
        else
            cout << "Valid options are only 1 or 2." << endl;
      system ("pause");
    
        }
      
    return 0;}

You now, don't have a logical error but just a human error. So I think there is no point of explanation. Hence, I am posting the correct code with very minnor changes. Your code was fine, but just have very minute glitches. Not to worry, it happens to everyone:

#include <iostream>
using namespace std;

int main()
{
    char digit;
    int character;
    int select = -1;


    {
        cout << "What function do you want to use : " << endl;
        cout << "1) Switch Statement " << endl;
        cout << "2) Nested Statement" << endl << endl;
        cout << "Enter: ";
        cin >> select;
        if (select == 1)
        {
            cout << " Enter the letter you want to see as a number: ";
            cin >> digit;
            switch (digit)
            {
            case '2':
                cout << " A, B, C";
                break;
            case '3':
                cout << " C, D, E";
                break;
            case '4':
                cout << " G, H, I";
                break;
            case '5':
                cout << " J, K ,L";
                break;
            case '6':
                cout << " M, N, O";
                break;
            case '7':
                cout << " P, Q, R, S";
                break;
            case '8':
                cout << " T, U, V";
                break;
            case '9':
                cout << " W, X, Y, Z";
                break;
            default:
                cout << digit<< "Error! Is not a number between 2-9 ."
                << endl;
                return 0;
            }
        }

        else if (select == 2)

        {
            cout <<" Enter the number you want see its corresponding letters: ";
            cin >> digit;
            if ( digit == '2')
                cout << digit << " A, B, C" << endl;
            else
                if ( digit == '3')
                    cout << digit << " D, E, F" << endl;
            else
                if ( digit == '4')
                    cout << digit << " G, H, I" << endl;
            else
                if ( digit == '5')
                    cout << digit << " J, K, L" << endl;
            else
                if ( digit == '6')
                    cout << digit << " M, N, 0" << endl;
            else
                if ( digit == '7')
                    cout << digit << " P, Q, R, S" << endl;
            else
                if ( digit == '8')
                    cout << digit << " T, U, V" << endl;
            else
                if ( digit == '9')
                    cout << digit << " P, Q, R, S" << endl;
            else
                    cout << digit << "Error! Is not a number between 2-9 ." << endl;
            system ("pause");
            return 0;
        }

        else
            cout << "Valid options are only 1 or 2." << endl;


    }

    return 0;
}

You should probably look changes in the Line 6( i made the digit as a char) and few nesting.
You should perhaps tally your code with mine and compare. I hope you will get where you went wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.