Question

I don't know how to terminate the code in the second time that whether or not...

I don't know how to terminate the code in the second time that whether or not the users want to continue to get a new number. PLEASE HELP.

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 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 below is my code

#include

#include

#include

using namespace std;

int main() {

char starter;

string inputLetters;

int count = 0;

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 >> starter;

while(starter == 'y' || starter == 'Y')

{

cout << "Enter a telephone number using letters: " << endl;

getline(cin,inputLetters);

cout << endl;

int i = 0;

while (i

{

bool notSpace = true;

switch (inputLetters[i])

{

case 'A':

case 'a':

case 'B':

case 'b':

case 'C':

case 'c':

cout << "2";

count++;

break;

case 'D':

case 'd':

case 'E':

case 'e':

case 'F':

case 'f':

cout << "3";

count++;

break;

case 'G':

case 'g':

case 'H':

case 'h':

case 'I':

case 'i':

cout << "4";

count++;

break;

case 'J':

case 'j':

case 'K':

case 'k':

case 'L':

case 'l':

cout << "5";

count++;

break;

case 'M':

case 'm':

case 'N':

case 'n':

case 'O':

case 'o':

cout << "6";

count++;

break;

case 'P':

case 'p':

case 'Q':

case 'q':

case 'R':

case 'r':

case 'S':

case 's':

cout << "7";

count++;

break;

case 'T':

case 't':

case 'U':

case 'u':

case 'V':

case 'v':

cout << "8";

count++;

break;

case 'W':

case 'w':

case 'X':

case 'x':

case 'Y':

case 'y':

case 'Z':

case 'z':

cout << "9";

count++;

break;

case ' ':

notSpace = false;

break;

default:

cout << "Invalid input.";

}

if (count == 3 && notSpace)

cout << "-";

if (count == 7)

break;

i++;

cout << endl;

}

cout << "To process another telephone number, enter Y/y";

cout << "Enter any other letter to terminate the program.";

cin >> starter;

cout << endl;

count = 0;

}

//system("pause");

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The modified lines are explained with comments

Modified program

#include<iostream>
#include <cstring>
#include<limits>
using namespace std;

int main() {

   char starter;

   string inputLetters;

   int count = 0;

   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 >> starter;

   while(starter == 'y' || starter == 'Y')

   {

       cout << "Enter a telephone number using letters: "<<endl;
      
       // to avoid taking empty string from get line because of new line
       cin.ignore( numeric_limits<streamsize>::max(), '\n' );
      
       getline(cin,inputLetters);
      

       int i = 0;

      
       while (i<8)

       {

           bool notSpace = true;

           switch (inputLetters[i])

           {

               case 'A':

               case 'a':

               case 'B':

               case 'b':

               case 'C':

               case 'c':

                   cout << "2";

                   count++;

                   break;

               case 'D':

               case 'd':

               case 'E':

               case 'e':

               case 'F':

               case 'f':

                   cout << "3";

                   count++;

                   break;

               case 'G':

               case 'g':

               case 'H':

               case 'h':

               case 'I':

               case 'i':

                   cout << "4";

                   count++;

                   break;

               case 'J':

               case 'j':

               case 'K':

               case 'k':

               case 'L':

               case 'l':

                   cout << "5";

                   count++;

                   break;

               case 'M':

               case 'm':

               case 'N':

               case 'n':

               case 'O':

               case 'o':

                   cout << "6";

                   count++;

                   break;

               case 'P':

               case 'p':

               case 'Q':

               case 'q':

               case 'R':

               case 'r':

               case 'S':

               case 's':

                   cout << "7";

                   count++;

                   break;

               case 'T':

               case 't':

               case 'U':

               case 'u':

               case 'V':

               case 'v':

                   cout << "8";

                   count++;

                   break;

               case 'W':

               case 'w':

               case 'X':

               case 'x':

               case 'Y':

               case 'y':

               case 'Z':

               case 'z':

                   cout << "9";

                   count++;

                   break;

               case ' ':

                   notSpace = false;

                   break;

               default:

                   cout << "Invalid input.";

           }

           if (count == 3 && notSpace)

               cout << "-";

           if (count == 7)

               break;

           i++;

           // print empty string to print the digits continuously not line by line
           cout << "";

       }
      
       // print new line
       cout<<"\n";

       cout << "To process another telephone number, enter Y/y\n";

       cout << "Enter any other letter to terminate the program.\n";

       cin >> starter;

       cout << endl;

       count = 0;

   }

//system("pause");

   return 0;

}

Sample Input/Output

rgukt-rkv@rguktrkv-TravelMate-P243-M: -/Documents/Untitled Folder/April/April 12 raukt-rkvarauktrky-TravelMate-P243-M:-/Docum

Add a comment
Know the answer?
Add Answer to:
I don't know how to terminate the code in the second time that whether or not...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Concepts CS 225 for C++ To make telephone numbers easier to remember, some companies use...

    Programming Concepts CS 225 for C++ To make telephone numbers easier to remember, some companies use digits and letters (or only letters) to show their telephone number. In some cases, to make a telephone number meaningful, companies might use more than seven digits and letters. Here are some examples: Phone Number in Display Note Actual Phone Number GET LOAN - 438-5626 CALL HOME More than seven digits/letters used for ease of remembrance. 225-5466 111 GOLD - 111-4653 Glass4u - 452-7748...

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols !.@,#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

  • 4B : C PROGRAMMING To make telephone numbers easier to remember, some companies use letters to...

    4B : C PROGRAMMING To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters & spaces, 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 prompts the user to enter a telephone number expressed in letters/spaces and...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • 1. in this programe i want add some products to be shown after choosing choise (...

    1. in this programe i want add some products to be shown after choosing choise ( show all products ) before i add any products. let say 10 products have added to the program then when the user choose (show all products ) all the 10 products appear and the user going to choose one and that one must has its own name, price,and quantity after that the quantity will be reduce. 2. allow the user to buy products. ===========================================================...

  • Regular C Programming It is just a random generator of telephone numbers DESCRIPTION To make telephone...

    Regular C Programming It is just a random generator of telephone numbers DESCRIPTION To make telephone numbers easier to remember, some companies use letters to show their telephone number, but mapping the letters to the numbers on a standard telephone keypad: 1 2 ABC 3 DE 4G 5 11 6 MNO 7 PONS 8 Tu 9.xyz # For example, using letters & spaces, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • What did I do wrong with this C++ code? Assume that we don't need to ask...

    What did I do wrong with this C++ code? Assume that we don't need to ask users for the number of students. #include <iostream> #include <iomanip> using namespace std; int main() { float *grades; // a pointer used to point to an array int size; int count = 0; // track the number of grade/student float grade; // the grade of a student float average, total; // average grade and total grades //**************start your code here************************ cout<<"How many student grades...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT