So I have a coding assignment to complete, and I am having trouble finishing it. The exact instructions are as follows:
To make telephone numbers easier to remember, some companies use letters to show their telephone number.
For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed as CALL HOME, which uses eight letters.
Write a program that does the following:
- Prompts a user to enter Y or y to begin conversion, or any other input to quit.
- Prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits
- Processes only the first seven letters if the user enters more than seven letters.
- Outputs the – (hyphen) after the third digit.
- Allows the user to use both uppercase and lowercase letters as well as spaces between words.
- Process as many telephone numbers as the user wants while allowing them to quit after each conversion.
I have currently completed all of the steps except for the last one (kind of).
The instructor is looking for the word "exit" to quit the program. Currently, I have it set up for "%" to end the program. Logically you would say to just change the "%" to "exit" and move on, but I get an error when I do this.
#include <iostream>
using namespace std;
int main()
{
int counter;
char phoneNumber;
char cont;
// Prompts a user to enter Y or y to begin conversion, or any other input to quit.
cout << "Please enter 'Y' or 'y' to continue, otherwise the program with quit.
Input: ";
cin >> cont;
if (cont == 'y' || cont == 'Y')
{
// statement(s) will execute if the boolean expression is true
}
else
{
return 0;
// statement(s) will execute if the boolean expression is false
}
cout << "To stop this program enter 'exit'." << endl;
cout << "Enter a phone number in letters only
NOTE: Must enter 7 or more letters: ";
cin >> phoneNumber;
cout << endl;
phoneNumber = static_cast<char>(toupper(phoneNumber));
while (phoneNumber != '%')
{
cout << "
To stop this program enter 'exit'." << endl;
cout << "Enter a phone number in letters only." << endl;
for (counter = 0; phoneNumber != '%' && counter < 7; counter++)
{
cin >> phoneNumber;
if (counter == 3)
cout << "-";
if ((phoneNumber >= 'A' && phoneNumber <= 'Z') ||
(phoneNumber >= 'a' && phoneNumber <= 'z'))
switch (phoneNumber)
{
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;
}
}
while (cin.get() != '
')
;
}
return 0;
}
I think it has something to do with the char, and that's all I could find out. I have been searching the internet for an answer, but I came up empty. Any help you could provide would be greatly appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…