I have written the code for the question below, but i am not satisfied with it . 1 , its just picking the first letter and cuts the rest and its also not me the question i put in the last an also not displaying the goodbye. here is the origina question
Question 2: NESTED LOOPS
To make phone numbers easier to remember, some companies use letters to show their phone
numbers. For example, using letters, the telephone number 438 5626 can be shown as GET
LOAN. In some cases, to make numbers more meaningful, companies may use more than
seven letters. For example, 225 5466 can be displayed as CALL HOME, which uses eight
letters. The program below prompts the user to enter a telephone number expressed in capital
letters and displays the corresponding telephone number in digits. If the user enters more than
seven letters, only the first seven letters are processed. A space is displayed between the third
and fourth digits.
The program has the following structure:
•
A do while loop to allow the user to convert as a telephone number expressed in letters
to its corresponding digital format.
•
A prompting message appears on the screen to ask the user to enter the phone number
in capital letters.
•
A for loop to input the seven letters.
•
Inside the for loop, an if statement to display the space between the third and fourth
digits in the telephone number.
•
This is followed by a switch statement to display the digit corresponding to the letter
read: A, B and C corresponds to 2, D, E and F to 3, G, H and I to 4, J, K and L to 5, M, N
and O to 6, P, Q R and S to 7, T, U and V to 8, and W, X, Y and Z to 9. If any other
character is encountered, a * is displayed.
•
After the for loop, a getline statement skips the rest of the characters on the line.
•
Then the user is prompted to determine whether another number should be converted.
•
The dowhile loop is exited when the user enters ‘n’.
•
When the dowhile loop is exited, a message to say goodbye is displayed.
In order to help you we give the following framework:
#include <iostream>
#include <cstdlib>
using namespace std;
const int NUMBERLETTERS = 7;
int main()
{
char letter, answer;
cout << "This program displays phone numbers expressed as"
cout << " letters in digital format";
// loop while user wants to convert phone numbers
do
{
// prompt for telephone number in capitals
// loop to input 7 letters for phone number
for ( ; ; )
{
// print space between third and fourth digit
if (i == 3)
COS1511/101
cout << " ";
// complete the loop
// display digit corresponding to letter
}
//skip letters more than 7
string skip;
getline(cin, skip, '\n');
cout << endl;
// prompt user to input another number to convert
}//stop when user no longer wants to convert phone numbers
while ( );
cout << "Goodbye!" << endl;
return 0;
}
Run your program with the input given below and submit printouts of the program and output.
(We write the data of each session in one line, but you will possibly enter the values on
separate lines.)
CALL HOME
GET LOAN
Hi THERE
BYE FOR NOW
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
|
#include <iostream>
#include <cstdlib>
using namespace std;
const int NUMBERLETTERS = 7;
int main()
{
char letter, answer;
cout << "This program displays phone numbers expressed as";
cout << " letters in digital format";
// loop while user wants to convert phone numbers
do
{
cout << "Please enter the phone number in CAPITAL LETTERS: ";
// prompt for telephone number in capitals
// loop to input 7 letters for phone number
for (int i = 1 ;i <= NUMBERLETTERS ; i++ )
{
cin >> letter;
// print space between third and fourth digit
if (i == 3)
cout << " ";
switch(letter)
{
case 'A':
case 'B':
case 'C':cout << "2" << endl;
break;
case 'D':
case 'E':
case 'F':cout << "3" << endl;
break;
case 'G':
case 'H':
case 'I':cout << "4" << endl;
break;
case 'J':
case 'K':
case 'L':cout << "5" << endl;
break;
case 'M':
case 'N':
case 'O':cout << "5" << endl;
break;
case 'P':
case 'Q':
case 'R':
case 'S':cout << "7" << endl;
break;
case 'T':
case 'U':
case 'V':cout << "8" << endl;
break;
case 'W':
case 'X':
case 'Y':
case 'Z':cout << "9" << endl;
break;
default:
cout << "*" << endl;
// complete the loop
// display digit corresponding to letter
}
//skip letters more than 7
string skip;
getline(cin, skip, '\n');
cout << endl;
}
// prompt user to input another number to convert
cout << "\nWould you like to convert another number? Indicate by Y - yes or N - no"<< endl;
cin >> answer;
}//stop when user no longer wants to convert phone numbers
while (toupper(answer) != 'N');
cout << "Goodbye!" << endl;
return 0;
}
|