Create a Visual C++ program that reads in a list of unformatted phone number str
ID: 3663033 • Letter: C
Question
Create a Visual C++ program that reads in a list of unformatted phone number strings and converts each one into another string that has the form: (area) prefix-number.
Your program must loop and prompt the user to input a list of raw phone number strings. Each raw phone number will be specified on a separate input line starting in column 1. The end of the input data is detected when the user hits "Enter" and inputs a line with no characters on it (zero length).
A raw phone number string will be either 10 digits in length or 7 digits in length (e.g., with no area code specified). If no area code is specified in the raw string, then your program must convert the 7 digit phone number into a 10 digit new string using the default area code "206".
For example,
2062812945 must be converted into a new string with value: "(206) 281-2945".
4253372797 must be converted into a new string with value: "(425) 337-2797".
9974321 must be converted into a new string with value: "(206) 997-4321".
As your program reads through the input data, it must process each incoming raw phone number, creating the new string variable with the converted data and outputting a report showing both the raw phone number and the converted formatted phone number. Also, if the phone number prefix is either "281" or "286" then the output must indicate that the phone number is an "SPU" phone number. For example, the output for one of the raw input numbers might look like:
Enter an unformatted phone number > 2062812945
Unformatted phone number: 2062812945
Formatted phone number: (206) 281-2945
This is an SPU phone number.
Finally, for this lab exercise:
• You must use the "C-String" ASCIIZ array of characters datatype for all of the string values in the program, including storage for both the raw string value and storage for the converted value.
• You may not use the built-in "string" datatype for any variables in the program.
• You must create a single string that contains the full formatted phone number value.
• You can use other c-string variables for temporary manipulations if you need to.
• You may use the any of the c-string functions (e.g., strcpy_s, strcat_s, strcmp, etc.), as well as manipulating the string buffers as arrays of characters using the ASCIIZ conventions.
• You should use the cin.getline() operation to read each input line from the user and store it in a raw c-string array buffer variable.
• Your program should include the <cstring> header file, e.g.,
#include <iostream>
#include <cstring>
using namespace std;
Create a Visual C++ program that reads in a list of unformatted phone number strings and converts each one into another string that has the form: (area) prefix-number.
Your program must loop and prompt the user to input a list of raw phone number strings. Each raw phone number will be specified on a separate input line starting in column 1. The end of the input data is detected when the user hits "Enter" and inputs a line with no characters on it (zero length).
A raw phone number string will be either 10 digits in length or 7 digits in length (e.g., with no area code specified). If no area code is specified in the raw string, then your program must convert the 7 digit phone number into a 10 digit new string using the default area code "206".
For example,
2062812945 must be converted into a new string with value: "(206) 281-2945".
4253372797 must be converted into a new string with value: "(425) 337-2797".
9974321 must be converted into a new string with value: "(206) 997-4321".
As your program reads through the input data, it must process each incoming raw phone number, creating the new string variable with the converted data and outputting a report showing both the raw phone number and the converted formatted phone number. Also, if the phone number prefix is either "281" or "286" then the output must indicate that the phone number is an "SPU" phone number. For example, the output for one of the raw input numbers might look like:
Enter an unformatted phone number > 2062812945
Unformatted phone number: 2062812945
Formatted phone number: (206) 281-2945
This is an SPU phone number.
Finally, for this lab exercise:
• You must use the "C-String" ASCIIZ array of characters datatype for all of the string values in the program, including storage for both the raw string value and storage for the converted value.
• You may not use the built-in "string" datatype for any variables in the program.
• You must create a single string that contains the full formatted phone number value.
• You can use other c-string variables for temporary manipulations if you need to.
• You may use the any of the c-string functions (e.g., strcpy_s, strcat_s, strcmp, etc.), as well as manipulating the string buffers as arrays of characters using the ASCIIZ conventions.
• You should use the cin.getline() operation to read each input line from the user and store it in a raw c-string array buffer variable.
• Your program should include the <cstring> header file, e.g.,
#include <iostream>
#include <cstring>
using namespace std;
Explanation / Answer
This below C++ code will written as per the given problem statement.
Steps:
1. Read the number from console using using cin.getline() method.
2. used standard c++ string functions.
3. after formatting completed print the result of phone number.
See the below C++ code:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char number[10], quit = '1';
char sdu1[] = "281", std2[] = "286", temp2[] = "206";
int count1 = 0, count2 = 0, length = 0;
while(quit != 'Q' && quit != 'q')
{
cout<<"Enter an unformatted phone number >"<<endl;
//cin.getline(number,10);
cin>>number;
for(int i = 0; number[i] != ''; i++)
{
length++;
}
cout<<"Unformatted phone number: ";
for(int i = 0; i < length; i++)
{
cout<<number[i];
}
cout<<endl;
if(length == 7)
{
cout<<"Formatted phone number:";
cout<<"(";
for(int i = 0; i < 3 ; i++)
cout<<temp2[i];
cout << ")";
for(int i= 0; i < 3 ; i++)
{
cout << number[i];
}
cout << "-";
for(int i = 3 ; i < 7 ; i++)
{
cout << number[i];
}
cout<<endl;
}
else if(length == 10)
{
for(int i =0; i<3;i++)
{
if(number[i] == sdu1[i])
count1++;
}
for(int i =0; i<3;i++)
{
if(number[i] == std2[i])
count2++;
}
if(count1 == 3 or count2 == 3)
{
cout<<"Formatted phone number:";
cout<<"(";
for(int i = 0; i < 3; i++)
{
cout << number[i];
}
cout << ")";
for(int i = 3; i < 6; i++)
{
cout << number[i];
}
cout << "-";
for(int i = 6; i < 10; i++)
{
cout << number[i];
}
cout<<endl;
cout<<"This is an SPU phone number."<<endl;
}
else
{
cout<<"Formatted phone number:";
cout<<"(";
for(int i = 0; i < 3; i++)
{
cout << number[i];
}
cout << ")";
for(int i = 3; i < 6; i++)
{
cout << number[i];
}
cout << "-";
for(int i = 6; i < 10; i++)
{
cout << number[i];
}
cout<<endl;
}
}
length = 0;
count1 = 0;
count2 = 0;
cout<<"Enter Q for Quit the Loop or Continue With The Loop Enter Other then Q"<<endl;
cin>>quit;
}
return 1;
}
Output: