Question

In: Computer Science

Week 4 Assignment: What’s in a Number? Directions: You are to write a C++ program that...

Week 4 Assignment: What’s in a Number?

Directions: You are to write a C++ program that meets the instruction requirements below.

Deliverables:

  • Your C++ source code file. (The file with the .CPP extension).No other files will be accepted.
  • A screenshot of your program running.

Program Instructions:

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.

Solutions

Expert Solution

Code:

#include<iostream>
using namespace std;
// function for converting character to number
int getDigit(char c){
   // switch case for converting the charcater to corrsponding number
   switch(c){
       // for a,b,c case sensitive give 2
       case 'A':  
       case 'B':
       case 'C':
       case 'a':
       case 'b':
       case 'c':
       return 2;
      
       // for d,e,f case sensitive give 3
       case 'D':
       case 'E':
       case 'F':
       case 'd':
       case 'e':
       case 'f':
       return 3;
      
       // for g,h,i case sensitive give 4
       case 'G':
       case 'H':
       case 'I':
       case 'g':
       case 'h':
       case 'i':
       return 4;
      
       // for j,k,l case sensitive give 5
       case 'J':
       case 'K':
       case 'L':
       case 'j':
       case 'k':
       case 'l':
       return 5;
      
       // for m,n,o case sensitive give 6
       case 'M':
       case 'N':
       case 'O':
       case 'm':
       case 'n':
       case 'o':
       return 6;
      
       // for p,q,r,s case sensitive give 7
       case 'P':
       case 'Q':
       case 'R':
       case 'S':
       case 'p':
       case 'q':
       case 'r':
       case 's':
       return 7;
      
       // for t,u,v case sensetive give 8
       case 'T':
       case 'U':
       case 'V':
       case 't':
       case 'u':
       case 'v':
       return 8;
      
       // for w,x,y,z case sensitive give 9
       case 'W':
       case 'X':
       case 'Y':
       case 'Z':
       case 'w':
       case 'x':
       case 'y':
       case 'z':
       return 9;
   }
}

int main()
{
   // infinite loop for asking words from the user
   while(true){
       // asking string from the user
       string my_word;
       cout<<"Enter words to get Tel_Number/('stop' to exit program): ";
       getline(cin, my_word);
       // if given word is = exit then exits the loop
       if(my_word=="stop"){
           break;
       }
       // else
       int total_words_count;
       total_words_count = 0;
       // iterating through the given word
       // and converting word to the Tel_Number
       for(int i=0; i < my_word.length(); i++){
           // converts all the words to digits to except " "
           if(my_word[i]!=' '){
               // printing the number corresponfing to the word
               // by calling function
               cout << getDigit(my_word[i]);
               total_words_count++;
               // after completion of 3 words put " - "
               if(total_words_count==3)
                   cout<<"-";
               // if words grater than 7 then convert upto 7 and
               // leave reamaining characters
               else if(total_words_count==7)
               break;
           }
       }
       cout<<endl;
   }
   return 0;
}

Attachments:

Output:

Any queries comment, please

Thank you:)


Related Solutions

In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: ·...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: · Your C++ source code file. (The file with the .CPP extension).No other files will be accepted. A screenshot of your program running. Program Instructions: Consider the following incomplete C++ program: #include int main() { … } 1. Write a statement that includes the header files fstream, string, and iomanip in this program. 2. Write statements that declare inFile to be an ifstream variable and...
Directions: Using a vector of integers that you define. Write a C++ program to run a...
Directions: Using a vector of integers that you define. Write a C++ program to run a menu driven program with the following choices: 1) Display the ages 2) Add an age 3) Display the average age 4) Display the youngest age 5) Display the number of students who can vote 6) Remove all students less than a given age 7) Quit Make sure your program conforms to the following requirements: 1. Write a function called getValidAge that allows a user...
Module/Week 3 ASSIGNMENT (CONTROL STRUCTURES . IF ..ELSE) Write a C++ program that computes a student’s...
Module/Week 3 ASSIGNMENT (CONTROL STRUCTURES . IF ..ELSE) Write a C++ program that computes a student’s grade for an assignment as a percentage given the student’s score and total points. The final score must be rounded up to the nearest whole value using the ceil function in the <cmath> header file. You must also display the floating-point result up to 5 decimal places. The input to the program must come from a file containing a single line with the score...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
C++ For this assignment, you will write a C++ program to either add (A), subtract (S),...
C++ For this assignment, you will write a C++ program to either add (A), subtract (S), multiply (M), and (N), or (O) two matrices if possible. You will read in the dimensions (rows, then columns) of the first matrix, then read the first matrix, then the dimensions (rows then columns) of the second matrix, then the second matrix, then a character (A, S, M, N, O) to determine which operation they want to do. The program will then perform the...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: ·Your...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: ·Your C++ source code file. (The file with the .CPP extension).No other files will be accepted. A screenshot of your program running. Program Instructions: Consider the following incomplete C++ program: #include <iostream> int main() { … } 1.    Write a statement that includes the header files fstream, string, and iomanip in this program. 2.    Write statements that declare inFile to be an ifstream variable and outFile to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT