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 :)