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

Many websites ask for phone numbers. The problem is that there are many ways to

ID: 3919798 • Letter: M

Question

Many websites ask for phone numbers. The problem is that there are many ways to represent a phone number. Examples include 817-555-1234, 817 555 1234 (c), and (817) 555-1234 x23. Write a Raptor program which inputs a string containing a phone number in any format and outputs it in a standard format. For this assignment, the standard format is (817) 555.1234. Your Raptor program should: 1. Input a string including the number 2. Copy only the digits from the input string into another string 3. Issue an error message if the input string does not contain at least 10 digits 4. If the input string contains more than 10 digits, include only the first 10 digits in the formatted number 5. Output the phone number in standard format Notes: 1. In Raptor, a string is just an array of characters. 2, Lesson #9 has an example of working with strings in Raptor. 3. When only digits are input, Raptor assumes it is an integer instead of a string. Your flowchart does not need to properly handle an input like 8175551234, but your Ct+ version does need to handle that.

Explanation / Answer

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

// main function definition
int main()
{
// To store phone number entered by the user
string phoneNumber;
// To store formatted number initialized to "(" character
string formattedNumber = "(";
// To count number of digits
int digit = 0;

// Accepts a phone number
cout<<" Please enter a phone number: ";
getline(cin, phoneNumber);

// Loops till length of the inputted phone number
for(int c = 0; c < phoneNumber.length(); c++)
{
// Checks if the current character of the phone number is digit
if(isdigit(phoneNumber.at(c)))
{
// Concatenates the current digit of the phone number with formatted number
formattedNumber += phoneNumber.at(c);
// Increase the digit counter by one
digit++;
// Checks if number of digits is equals to 3 then concatenates ")" symbol
if(digit == 3)
formattedNumber += ") ";
// Checks if number of digits is equals to 6 then concatenates "." symbol
if(digit == 6)
formattedNumber += ".";
// Checks if number of digits is equals to 8 then come out of the loop
if(digit == 10)
break;
}// End of if condition
}// End of for loop

// Checks if number of digits is less than 10 then display error message
if(digit < 10)
cout<<" The phone number must have at least 10 digits.";
// Otherwise displays the formatted number
else
cout<<" The properly formatted number is: "<<formattedNumber;
return 0;
}// End of main function

Sample Output 1:

Please enter a phone number: 817-555-1234

The properly formatted number is: (817) 555.1234

Sample Output 2:

Please enter a phone number: (817)515 7259 x23

The properly formatted number is: (817) 515.7259

Sample Output 3:

Please enter a phone number: 214-555-999

The phone number must have at least 10 digits.

Sample Output 4:

Please enter a phone number: 800**4444xxx3333

The properly formatted number is: (800) 444.4333