In: Computer Science
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.
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;
}