I'm working on this as an assignment for Computer Science 121. I thought I had the code working, but I couldn't figure out one crucial part. The assignment is to convert phrases (such as 'GET LOAN' or 'CALL HOME') into telephone numbers (438-5626 and 225-5466 respectively). The program has to translate whatever the user types in, including spaces, uppercase, lowercase, and phrases longer than 7 letters. If it's longer than seven, only the first seven are processed. Also, a hyphen needs to be inserted after the 3rd number. Mine does everything but evaluates only the first seven. Mine will produce a number for every letter inputted. How can I get it to work properly? Here's what I have so far:

#include <iostream>
#include <cmath>
#include <cctype>

using namespace std;

int main()
{
    char word;
	int count = 0;


    cout << "To stop this program enter %." << endl;

    cout << "Enter a telephone phrase followed by the '$' sign: ";                 
    cin >> word;                      
    cout << endl;
	word = static_cast<char>(toupper(word));


    while (word != '%')
    {               
		cout << "The corresponding telephone number is: ";

		while (word != '$')
		{
			for (count = 1; count <= 7; count++)
			{
            switch (word)                     
            {
            case 'A':
            case 'B':
            case 'C':
                cout << "2";             
                break;                            
            case 'D':
            case 'E':
            case 'F':
                cout << "3";          
                break;                      
            case 'G':
            case 'H':
            case 'I':
                cout << "4";         
                break;                          
            case 'J':
            case 'K':
            case 'L':
                cout << "5";         
                break;                    
            case 'M':
            case 'N':
            case 'O':
                cout << "6";            
                break;                      
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
                cout << "7";            
                break;                        
            case 'T':
            case 'U':
            case 'V':
                cout << "8";             
                break;                           
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
                cout << "9";
				break;
			case ' ':
				cout << "";
				break;
            default:
                    cout << "Invalid input." << endl;
            }
			if (count == 3)
				cout << "-";
			cin >> word;
			word = static_cast<char>(toupper(word));
			}
		}
		cout << endl;

        cout << "Enter a telephone phrase followed by the '$' sign: ";
        cin >> word;
		word = static_cast<char>(toupper(word));
        cout << endl;
    }

    return 0;
}

Thanks for the help :)

You run the counting loop for every iteration of the loop that searches for '$'. Try merging the two loops into one:

while (word != '%')
{
  //...

  for (count = 1; word != '$' && count <= 7; count++)
  {
    //...
  }

  //...
}

Thanks, that helped a lot :)

It'll end a telephone number at the seventh digit now, but it'll continue on a new line. Here's the execution:

To stop this program enter %.
Enter a telephone phrase followed by the '$' sign: big deal$

The corresponding telephone number is: 244-3325
Enter a telephone phrase followed by the '$' sign: big deals$

The corresponding telephone number is: 244-3325
Enter a telephone phrase followed by the '$' sign:
The corresponding telephone number is:
Enter a telephone phrase followed by the '$' sign: hello world$

The corresponding telephone number is: 435-5696
Enter a telephone phrase followed by the '$' sign:
The corresponding telephone number is: 53
Enter a telephone phrase followed by the '$' sign: call home$

The corresponding telephone number is: 225-5466
Enter a telephone phrase followed by the '$' sign:
The corresponding telephone number is:
Enter a telephone phrase followed by the '$' sign: mississippi$

The corresponding telephone number is: 647-7477
Enter a telephone phrase followed by the '$' sign:
The corresponding telephone number is: 774-
Enter a telephone phrase followed by the '$' sign: call home$

The corresponding telephone number is: 225-5466
Enter a telephone phrase followed by the '$' sign:
The corresponding telephone number is:
Enter a telephone phrase followed by the '$' sign: hello World$

The corresponding telephone number is: 435-5696
Enter a telephone phrase followed by the '$' sign:
The corresponding telephone number is: 53
Enter a telephone phrase followed by the '$' sign:

Well, when you type characters, they don't just vanish magically when your code is done reading. They stay in the stream waiting for the next input call. This loop will clear out the input stream for you, try to find a suitable place in your code to put it:

while ( cin.get() != '\n' )
  ;

Thanks for the help! The program's running properly now :)

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.