Homework summery below.
Iv been working on this for days and oddly cannot get this code to work correctly, taking from things i found online, in my book, in my lectures and so on.
The 1st part of code whether Y or N or other will continue anyway (but still technically passes the mindtap 1st part of coding but would still like it to work correctly for learning purposes)
The number works fine as well, but then i cant get the do.. while loop to work correctly prompting me to (CIN) yes or no to repeat a new number
Would like some simplified help im a c++ dummy but still learning, so if you can help me fix the code and explain why
Summary
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.
Instructions
Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits.
If the user enters more than seven letters, then process only the first seven letters.
Also output the - (hyphen) after the third digit.
Allow the user to use both uppercase and lowercase letters as well as spaces between words.
Moreover, your program should process as many telephone numbers as the user wants.
The program should accept input and produce output similar to the example program execution below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <iostream>
using namespace std;
int main() {
char tele = 'y';
do
{
cout << "Enter Y/y to convert a telephone number from letters to digits." << endl;
cout << "Enter any other letter to terminate the program." << endl;
cin >> tele;
} while (tele == 'n' || tele == 'N');
//----------------------------------------------------
cout << "Enter a telephone number using letters:" << endl;
//----------------------------------------------------
do
{
char letter;
int number = 0;
while (cin.get(letter) && number < 7 )
{
if (letter != ' ' && letter >= 'A' && letter <= 'z') {
number++;
if (letter > 'Z') {
letter = (int)letter-32;
}
if (number == 4) {
cout << "-";
}
switch (letter) {
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;
default:
break;
}
}
}
cout << endl;
cout << "Enter another number? Y or N?" << endl;
cin >> tele;
} while (tele == 'n' || tele == 'N');
return 0;
}
|