Question

In: Computer Science

Create a program that: 1. Has a main() 2. Includes the proper header and namespace declarations...

Create a program that:

1. Has a main()

2. Includes the proper header and namespace declarations

3. Create a program using the following requirements:

    a. Request a 7-character telephone number from the user using an informational prompt
        and translate the characters into a 7-digit telephone number.
      
        The user will not enter the dash, the program will format the telephone number properly.
        For example, aaadddd would translate to 222-3333.
      
        Do not code anything for 0 or 1. This is simply to exercise knowledge of the different while
        loops, switches and nested loops.
      
    b. Ensure the solution is not case sensitive, i.e., the user may enter AAAADDDD or
        aaadddd.
      
    c. Include an outer DO-WHILE loop that asks the user if they would like to continue
        entering telephone numbers. If the user enters y or Y, continue. Any other entry
        will exit the program.

    d. Include an inner/nested WHILE loop that processes the entered characters until the
        entered characters are translated into the proper telephone number.

    e. Use a SWITCH statement to translate characters to numbers. If the user enters something
        other than A-Z or a-z, display an error message and exit the WHILE loop, continuing
        with asking the user if they would like to continue.

    f. Output/display to the console the translated telephone number.

If you like to include the following line of code: system("pause");
You MAY need to: #include <cstdlib>

Solutions

Expert Solution

Please find your solution below and if any doubt or need change comment and do upvote.

CODE:

#include <iostream>
using namespace std;

int main()
{
    string s;
    char ch;
    do{
        //take user input
        cout<<"Enter a 7-character telephone number: ";
        cin>>s;
        string str="";
        int flag=0;
        
        int i=0;
        
        //use while loop and swith case to convert telephone number
        while(i<s.length()&&flag==0){
            
            switch(s[i]){
                
                //for the case of a,b,c or A,B,C number would be 2(as per keypad phone keys)
                case 'a':
                str=str+'2';
                break;
                case 'b':
                str=str+'2';
                break;
                case 'c':
                str=str+'2';
                break;
                case 'A':
                str=str+'2';
                break;
                case 'B':
                str=str+'2';
                break;
                case 'C':
                str=str+'2';
                break;
                
                //for the case of d,e,f or D,E,F number would be 3(as per keypad phone keys)
                case 'd':
                str=str+'3';
                break;
                case 'e':
                str=str+'3';
                break;
                case 'f':
                str=str+'3';
                break;
                case 'D':
                str=str+'3';
                break;
                case 'E':
                str=str+'3';
                break;
                case 'F':
                str=str+'3';
                break;
                
                //for the case of g,h,i or G,H,I number would be 4(as per keypad phone keys)
                case 'g':
                str=str+'4';
                break;
                case 'h':
                str=str+'4';
                break;
                case 'i':
                str=str+'4';
                break;
                case 'G':
                str=str+'4';
                break;
                case 'H':
                str=str+'4';
                break;
                case 'I':
                str=str+'4';
                break;
                
                //for the case of j,k,l or J,K,L number would be 5(as per keypad phone keys)
                case 'j':
                str=str+'5';
                break;
                case 'k':
                str=str+'5';
                break;
                case 'l':
                str=str+'5';
                break;
                case 'J':
                str=str+'5';
                break;
                case 'K':
                str=str+'5';
                break;
                case 'L':
                str=str+'5';
                break;
                
                //for the case of m,n,o or M,N,O number would be 6(as per keypad phone keys)
                case 'm':
                str=str+'6';
                break;
                case 'n':
                str=str+'6';
                break;
                case 'o':
                str=str+'6';
                break;
                case 'M':
                str=str+'6';
                break;
                case 'N':
                str=str+'6';
                break;
                case 'O':
                str=str+'6';
                break;
                
                //for the case of P,Q,R,S or p,q,r,s number would be 7(as per keypad phone keys)
                case 'p':
                str=str+'7';
                break;
                case 'q':
                str=str+'7';
                break;
                case 'r':
                str=str+'7';
                break;
                case 's':
                str=str+'7';
                break;
                case 'P':
                str=str+'7';
                break;
                case 'Q':
                str=str+'7';
                break;
                case 'R':
                str=str+'7';
                break;
                case 'S':
                str=str+'7';
                break;
                
                //for the case of t,u,v or T,U,V number would be 8(as per keypad phone keys)
                case 't':
                str=str+'8';
                break;
                case 'u':
                str=str+'8';
                break;
                case 'v':
                str=str+'8';
                break;
                case 'T':
                str=str+'8';
                break;
                case 'U':
                str=str+'8';
                break;
                case 'V':
                str=str+'8';
                break;
                
                //for the case of w,x,y,z or W,X,Y,Z number would be 9(as per keypad phone keys)
                case 'w':
                str=str+'9';
                break;
                case 'x':
                str=str+'9';
                break;
                case 'y':
                str=str+'9';
                break;
                case 'z':
                str=str+'9';
                break;
                case 'W':
                str=str+'9';
                break;
                case 'X':
                str=str+'9';
                break;
                case 'Y':
                str=str+'9';
                break;
                case 'Z':
                str=str+'9';
                break;
                
                //for 0 and 1
                case '0':
                str=str+'0';
                break;
                case '1':
                str=str+'1';
                break;
                
                //if any other characters than these the set flag 1 and break 
                default:
                flag=1;
                break;
            }
            i++;
        }
        //if entered string was valid
        if(flag==0){
            string telephoneNo="";
            //after three character add -
            for(int i=0;i<3;i++){
                telephoneNo=telephoneNo+str[i];
            }
            //add remaining characters
            telephoneNo=telephoneNo+'-';
            for(int i=3;i<7;i++){
                telephoneNo=telephoneNo+str[i];
            }
            //print convert telephoneNo
            cout<<telephoneNo<<endl;
        }
        else{
            //if flag==1 then invalid
            cout<<"Invalid telephone number"<<endl;
        }
        
        //ask user if they want to continue
        cout<<"Do you want to continue? (Y or y): ";
        cin>>ch;
    }while(ch=='y'||ch=='Y');
    
    

    return 0;
}

OUTPUT:


Related Solutions

Create a Namespaces.h header file containing a namespace declaration yourname. The declaration should include: a method...
Create a Namespaces.h header file containing a namespace declaration yourname. The declaration should include: a method with the signature void message (string, ostream &) that prints a string to the output stream. a method with the signature void message (double, ostream &) that prints a double to the output stream. Create a testNamespaces.cpp that uses the yourname namespace, and invokes the message() string method with a message of your choice and cout as input parameters, and invokes the message double...
The requirements for this program are as follows: Create a header file named “Employee.h”. Inside this...
The requirements for this program are as follows: Create a header file named “Employee.h”. Inside this header file, declare an Employee class with the following features: Private members a std::string for the employee’s first name a std::string for the employee’s last name an unsigned int for the employee’s identification number a std::string for the city in which the employee works Public members A constructor that takes no arguments A constructor that takes two arguments, representing: the employee’s first name the...
Write an entire program (with a main function and necessary header files included) that declares an...
Write an entire program (with a main function and necessary header files included) that declares an 80 byte character buffer, prompts a user to enter a single word (no spaces) from the default input device (e.g. the keyboard), which is stored in the buffer, and then reverses the string while overwriting the buffer. Print the sting before and after the reversal. The program transcript should resemble the following output: $ ./program.exe enter string: nvidia before: nvidia after: aidivn in C
Java program In the main(), create and load a 2-dimentional array to hold the following sales...
Java program In the main(), create and load a 2-dimentional array to hold the following sales data Number of Tacos Sold (hard code the data into the array when you create it) Truck3 Truck4 .    Truck5 Monday 250 334 229 Wednesday   390 145 298 Friday .    434 285 . 156 • Write a method to calculate the grand total of all Tacos sold Write a method to allow the user to enter a truck number and get the total...
Create your own Java program that has all three of these components in its design: Includes...
Create your own Java program that has all three of these components in its design: Includes file input OR file output (one of the two) Uses token-based processing of String, int, double, or boolean (your choice) Incorporates two additional methods in addition to the main() method: one that's a value-returning method, the other a void.
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha...
What would the following program output? #include <iostream> using namespace std; int main() { char alpha = 'A'; for(int i = 0; i < 13; i++){ for(int j = 0; j < 2; j++){ cout << alpha; alpha++; } } cout << endl; return 0; }
PYTHON - For loop - program must have proper docstring. 1/1 + 1/2 + 1/4 +1/4...
PYTHON - For loop - program must have proper docstring. 1/1 + 1/2 + 1/4 +1/4 + 1/8 + 1/8 + 1/8 + . . . + 1/n The program should ask the user to input a number for n. Program should perform range check on the input, for it to be from 1 to 100,000,000. I f user input in not in the range, prompt the user until correct number is given. The program should call the function with...
C (1) Create three files to submit: ItemToPurchase.h - Struct definition and related function declarations ItemToPurchase.c...
C (1) Create three files to submit: ItemToPurchase.h - Struct definition and related function declarations ItemToPurchase.c - Related function definitions main.c - main() function Build the ItemToPurchase struct with the following specifications: Data members (3 pts) char itemName [ ] int itemPrice int itemQuantity Related functions MakeItemBlank() (2 pts) Has a pointer to an ItemToPurchase parameter. Sets item's name = "none", item's price = 0, item's quantity = 0 PrintItemCost() Has an ItemToPurchase parameter. Ex. of PrintItemCost() output: Bottled Water...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game that involves betting on the sum of two dice. The player will start out with some initial total amount of money. During each round, the player can bet some money that the sum of the two dice will be equal to a certain number. If the player wins the bet, that player adds the amount of the bet to his or her current total....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT