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