954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Text messaging program

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;
}
songweaver
Junior Poster in Training
80 posts since Mar 2009
Reputation Points: 17
Solved Threads: 0
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Just to clarify what Lerner posted, you need to have
if ( digit ==<strong>'2'</strong>) instead of if ( digit == <strong>2</strong> ) 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 entering1 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
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

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;}
songweaver
Junior Poster in Training
80 posts since Mar 2009
Reputation Points: 17
Solved Threads: 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.

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You