Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the following questions in C++: Question 1 Prompt the user for a phone

ID: 3835512 • Letter: C

Question

Complete the following questions in C++:

Question 1

Prompt the user for a phone number that's in the following format: (AAA) BBB-CCCC

AAA is the area code, BBB is the exchange, CCCC is the subscriber number; all of which must be digits and must be the length show here.

If the phone number is in the correct format, display the phone number in a new format: AAA.BBB.CCCC

View required output

Test Case 1

Test Case 2

Test Case 3

Test Case 4

Test Case 5

Test Case 6

Test Case 7

Test Case 8

Test Case 9

Test Case 10

Test Case 11

Test Case 12

Test Case 13

Test Case 14

Test Case 15

Test Case 16

Test Case 17

Test Case 18

Test Case 19

Test Case 20

Test Case 21

Test Case 22

Test Case 23

--------------------------------------------------------------------------------------------------------------------------------------------

Question 2

Prompt the user for a list of phone numbers separated by commas. User the validation rules from the last question.

View required output

Test Case 1

Test Case 2

Test Case 3

Test Case 4

Test Case 5

Test Case 6

Test Case 7

Test Case 8

Standard Input                
  (8055) 922-666  

Explanation / Answer

Code:
#include <iostream>
#include <string>
#include <cstring>

using namespace std;
// function prototype
string validatePhone(string);

int main()
{
string pno;
string result;
  
cout << "Enter a phone number in the following format: (XXX) XXX-XXXX ";
getline(cin, pno);
result = validatePhone(pno);
cout << pno << " -> " << result;
  
return 0;
}

// function to validate phone number
string validatePhone(string phone)
{
string result, sub, exchange, area;
string str;
unsigned int i;
  
if(phone.length() != 14)
result = "Input must be exactly 14 characters";
else{
area = phone.substr(phone.find("(")+1, phone.find(")")-1);
sub = phone.substr(phone.find("-")+1, phone.find(" ")-phone.find("-")-1);
exchange = phone.substr(phone.find(" ")+1, phone.find("-")-phone.find(" ")-1);
//cout << "area=" << area<<endl;
//cout << "sub=" <<sub<<endl;
//cout << "exchange="<< exchange <<endl;
if(area.size() == 3) {
for(i = 0; i < area.size() && area[i]>='0' && area[i]<='9'; i++);
if(i != area.size())
result = "Area code must be only numbers: " + area;
else
str = area;
}
else {
result = "Area code must be 3 characters: " + area;
}
  
if(exchange.size() == 3) {
for(i = 0; i < exchange.size() && exchange[i]>='0' && exchange[i]<='9'; i++);
if (i != exchange.size())
result += " exchange code must be only numbers: " + exchange;
else
str = str + '.' + exchange;
}
else {
result += " exchange code must be 3 characters: " + exchange;
}
if(sub.size() == 4) {
for(i = 0; i < sub.size() && sub[i]>='0' && sub[i]<='9'; i++);
if (i != sub.size())
result += " subscription code must be only numbers: " + sub;
else
str = str + '.' + sub;
}   
else {
result += " subscription code must be 3 characters: " + sub;
}
}
  
if(str.size() == 12)
return str;
else
return result;
}

Execution and output:
Enter a phone number in the following format: (XXX) XXX-XXXX (123) abc-1234
(123) abc-1234 ->
exchange code must be only numbers: abc

Enter a phone number in the following format: (XXX) XXX-XXXX (123) 12-1234
(123) 12-1234 -> Input must be exactly 14 characters

Enter a phone number in the following format: (XXX) XXX-XXXX (12) 1234-1234
(12) 1234-1234 -> Area code must be 3 characters: 12
exchange code must be 3 characters: 1234

Enter a phone number in the following format: (XXX) XXX-XXXX (12) 1234-1234
(12) 1234-1234 -> Area code must be 3 characters: 12
exchange code must be 3 characters: 1234


NOTE:
I was not able to provide answer for Question.2 due to lack of time. But i feel question 2 would be simple because the function to validate given phone numbers is already available. All we need to do is take arguments from command line, split them and pass to the validatePhone() in the above code

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote