Question

In: Computer Science

C++: (I need this program to output in a certain format I will post the "needed...

C++: (I need this program to output in a certain format I will post the "needed results" below)

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 letter. 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, than process only the first seven letters. Also output the - (hyphen after the third digit. Allow the user to use both upper case and lowercase letters as well as spaces between words. moreover, your program should process as many telephone numbers as the user wants.

Desired Outputs:

Enter Y/y to convert a telephone number from letters to digits. Enter any other letter to terminate the program.

y

Enter a telephone number using 7 or more letters for prefix and number, onnly the first 7 letters are used and spaces are not counted.

-->: To Be or not

The corresponding telephone number is:

862-3676

To process another telephone number, enter Y/y Enter any other letter to terminate the program.

y

Enter a telephone number using 7 or more letters for Prefix and number, only the first 7 letters are used and spaces are not counted

--> YRU Here?

The corresponding telephone number is:

978-4373

To process another telephone number, enter Y/y Enter any other letter to terminate the program.

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include<iostream>

#include<cctype>

#include<string>

using namespace std;

int main(){

    string telephone_str, choice;

    string telephone_num = "";

    cout<<"Enter Y/y to convert a telephone number from letters to digits. Enter any other letter to terminate the program."<<endl;

    cin>>choice;

    while(choice=="Y" || choice=="y"){

        telephone_num = "";

        cout<<"Enter a telephone number using 7 or more letters for prefix and number, only the first 7 letters are used and spaces are not counted."<<endl;

        cout<<"-->: ";

        cin.clear();

        cin.ignore(100, '\n');

        getline(cin, telephone_str);

        int i=0, k=0;

        while(i<telephone_str.size() && k<7){

            switch(toupper(telephone_str[i])){

                case 'A':

                case 'B':

                case 'C':

                    telephone_num += '2';

                    break;

                case 'D':

                case 'E':

                case 'F':

                    telephone_num += '3';

                    break;

                case 'G':

                case 'H':

                case 'I':

                    telephone_num += '4';

                    break;

                case 'J':

                case 'K':

                case 'L':

                    telephone_num += '5';

                    break;

                case 'M':

                case 'N':

                case 'O':

                    telephone_num += '6';

                    break;

                case 'P':

                case 'Q':

                case 'R':

                case 'S':

                    telephone_num += '7';

                    break;

                case 'T':

                case 'U':

                case 'V':

                    telephone_num += '8';

                    break;

                case 'W':

                case 'X':

                case 'Y':

                case 'Z':

                    telephone_num += '9';

                    break;

            }

            if(telephone_str[i]!=' '){

                if(k==2)

                    telephone_num+='-';

                k++;

            }

            i++;

        }

        cout<<"The corresponding telephone number is: "<<telephone_num<<endl;

        cout<<"To process another telephone number, enter Y/y Enter any other letter to terminate the program."<<endl;

        cin>>choice;

    }

    return 0;

}


Related Solutions

c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have...
Read a text file into arrays and output - Java Program ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
I have this matlab program, and need to turn it into a C++ program. Can anyone...
I have this matlab program, and need to turn it into a C++ program. Can anyone help me with this? % Prompt the user for the values x and y x = input ('Enter the x coefficient: '); y = input ('Enter the y coefficient: '); % Calculate the function f(x,y) based upon % the signs of x and y. if x >= 0    if y >= 0        fun = x + y;    else        fun = x + y^2;    end...
I need assistance on this problem in Pseudocode and in C++ Program Program 3: Give a...
I need assistance on this problem in Pseudocode and in C++ Program Program 3: Give a baby $5,000! Did you know that, over the last century, the stock market has returned an average of 10%? You may not care, but you’d better pay attention to this one. If you were to give a newborn baby $5000, put that money in the stock market and NOT add any additional money per year, that money would grow to over $2.9 million by...
I need C++ programming with output. I have tried other programming and it does not work....
I need C++ programming with output. I have tried other programming and it does not work. So please give me the one that actually works. Assignment 1 Design your own linked list class that works as a template class. It should provide member functions for appending, inserting and deleting nodes. The destructor should destroy the list. The class should also provide a member function that will display the contents of the list to the screen. The class should also provide...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
Hello, I need some assistance on completing this program in Pseudocode and in C++ Program 2:...
Hello, I need some assistance on completing this program in Pseudocode and in C++ Program 2: Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random...
i need C++ program with possible comments what is going on,(a) Write a program that reads...
i need C++ program with possible comments what is going on,(a) Write a program that reads a char as input, and determines if it is a lowercase letter, uppercase letter, a digit or something else (call the last one a special character).
I need this written in C # ASAP Write a C# console program that continually asks...
I need this written in C # ASAP Write a C# console program that continually asks the user "Do you want to enter a name (Y/N)? ". Use a "while" loop to accomplish this. As long as the user enters either an upper or lowercase 'Y', then prompt to the screen "Enter First and Last Name: " and then get keyboard input of the name. After entering the name, display the name to the screen.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT