In: Computer Science
Week 4 Assignment: What’s in a Number?
Directions: You are to write a C++ program that meets the instruction requirements below.
Deliverables:
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.
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:)