views:

157

answers:

4

I have been staring at this all day and I am having a difficult time with this code. I need this program to convert a string of letters, such as Call Cash, into a 7 digit telephone number. I get either one of two outcomes. Either the program does not output any numbers at all or it gets stuck in an infinite loop. Any assistance would be greatly appreciated.

// Program to convert letters to numbers in a phone number.

//Header file
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    //Declare variables
char input;
char letters;
int number;
int counter;
counter = 0;


    //Program
cout << "Enter Y/y to convert a telephone number form letters to digits." << endl;
cout << "Enter any other letter to terminate the program." << endl;
cin >> input;
while (input == 'Y' || input == 'y')
{
    cout << "Enter telephone number using letters: ";
    cin >> letters;
    cout << endl;

    cout << "The corresponding phone number is: ";

    while (counter <= 7);
    {
        cout << number;
        if ((letters >= 'A' && letters <= 'Z') || (letters >= 'a' && letters <= 'z'))
        switch (letters)
    {
    case 'A':
    case 'a':
    case 'B':
    case 'b':
    case 'C':
    case 'c':
    cout << "2";
    break;

    case 'D':
    case 'd':
    case 'E':
    case 'e':
    case 'F':
    case 'f':
    cout << "3";
    break;

    case 'G':
    case 'g':
    case 'H':
    case 'h':
    case 'I':
    case 'i':
    cout << "4";
    break;

    case 'J':
    case 'j':
    case 'K':
    case 'k':
    case 'L':
    case 'l':
    cout << "5";
    break;

    case 'M':
    case 'm':
    case 'N':
    case 'n':
    case 'O':
    case 'o':
    cout << "6";
    break;

    case 'P':
    case 'p':
    case 'Q':
    case 'q':
    case 'R':
    case 'r':
    case 'S':
    case 's':
    cout << "7";
    break;

    case 'T':
    case 't':
    case 'U':
    case 'u':
    case 'V':
    case 'v':
    cout << "8";
    break;

    case 'W':
    case 'w':
    case 'X':
    case 'x':
    case 'Y':
    case 'y':
    case 'Z':
    case 'z':
    cout << "9";
    break;

    if (counter == 3)
        cout << "-";
}
    }
}


return 0;
}

edit: This is the new code I have. The problem I am hitting now is that it is only returning a value for the first letter and it is returning each value on a separate line. I do appreciate the assistance:

    //Declare variables
char input;
char letters;
int number;
int i;


    //Program
cout << "Enter Y/y to convert a telephone number form letters to digits." << endl;
cout << "Enter any other letter to terminate the program." << endl;
cin >> input;
while (input == 'Y' || input == 'y')
{
    cout << "Enter telephone number using letters: ";
    cin >> letters;
    cout << endl;

    cout << "The corresponding phone number is: ";

    for(int i = 0; i < 7; i++)
    {
        if ((letters >= 'A' && letters <= 'Z') || (letters >= 'a' && letters <= 'z'))
        switch (letters)
    {
    case 'A':
    case 'a':
    case 'B':
    case 'b':
    case 'C':
    case 'c':
    cout << "2";
    break;

    case 'D':
    case 'd':
    case 'E':
    case 'e':
    case 'F':
    case 'f':
    cout << "3";
    break;

    case 'G':
    case 'g':
    case 'H':
    case 'h':
    case 'I':
    case 'i':
    cout << "4";
    break;

    case 'J':
    case 'j':
    case 'K':
    case 'k':
    case 'L':
    case 'l':
    cout << "5";
    break;

    case 'M':
    case 'm':
    case 'N':
    case 'n':
    case 'O':
    case 'o':
    cout << "6";
    break;

    case 'P':
    case 'p':
    case 'Q':
    case 'q':
    case 'R':
    case 'r':
    case 'S':
    case 's':
    cout << "7";
    break;

    case 'T':
    case 't':
    case 'U':
    case 'u':
    case 'V':
    case 'v':
    cout << "8";
    break;

    case 'W':
    case 'w':
    case 'X':
    case 'x':
    case 'Y':
    case 'y':
    case 'Z':
    case 'z':
    cout << "9";
    break;
    cout << number;
    }

    if (i == 2)
        cout << "-";

    }
    cout << "To process another telephone number, enter Y/y." << endl;
    cout << "Enter any other letter to terminate the program." << endl;
    cin >> input;
}


return 0;

}

+2  A: 

You have a semicolon after your while (counter <= 7). You have misplaced your closing brace for your switch (letters), which should come before the if (counter == 3). You have a while loop on input, but you never change it while the program is running.

Did I get them all?

Borealid
+1  A: 

The first thing I notice is that during your inner while loop you cout the variable 'number' every loop, yet you never assign a value to it. Next, you have a semicolon at the beginning of the inner loop. Your also never increment 'counter' and store the input number in a single char instead of a string.

As for the observed behavior the infinite loop would come from never incrementing counter, resulting in a loop that never stops. Printing nothing would come from never assigning a value to 'number'.

I think what you meant to do was to have the switch conditionals assign a value to 'number' instead of just printing it. Thus the cout should be after the switch to avoid uninitialized value nastiness.

EDIT: Oh, be sure to use your loop to iterate down the string.

Here's the general idea of what you need to do:

string input;
cin >> input;

int length = strlen(input)
for(int i = 0; i < length; i++)
{
    //what happens if this condition fails?
    if ((letters >= 'A' && letters <= 'Z') || (letters >= 'a' && letters <= 'z'))
    {
        switch (letters[i])
        {
            case 'A':
            case 'a':
            case 'B':
            case 'b':
            case 'C':
            case 'c':
            number = 2;
            break;

            <snip>
         }

        cout << number;
    }
    if(i == 2)
    {
        cout << '-';
    }
}
Mandelbrot
I have updated the code with your suggestions and am now running into a slightly different problem. You are being incredibly more helpful than the prof.
H Bomb1013
I would advice you to convert the 'letters' using toupper (or tolower)... that way, you reduce the switch cases by half...makes it much more readable.
st0le
My previous comment was directed @HBomb1013
st0le
That's an excellent point st0le
Mandelbrot
A: 

declare 'letters' as a string instead of as a single char then go through the string character by character and convert them

Anders K.
+2  A: 

Where you have...

(letters >= 'A' && letters <= 'Z') || (letters >= 'a' && letters <= 'z')

prefer

isalpha (letters)
Brian Hooper