Hello everyone, I am new to this forum and am looking for some assistance, any help would be greatly appreciated! I am stuck on this assignment for my programming fundamentals 1 class and am desperate at this point as I have been stuck for hours on end. Thank you.
Here is the prompt:
Many websites ask for phone numbers. The problem is that there are so many
different ways to represent a phone number. Examples include 817-555-1234,
817 555 1234 (c), and (817) 555-1234 x23. Write a C++ program which inputs
a string containing a phone number in any format and outputs it in the standard
format. For this assignment, the standard format is (817)555-1234.
Your c++ 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 exactly 10
digits
4. Output the phone number in standard format
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;
const int NUM_LENGTH = 10;
string ReadAndValidateUserNumber(string userNumber);
int main()
{
string userNumber;
ReadAndValidateUserNumber(userNumber);
system("PAUSE");
return 0;
}
string ReadAndValidateUserNumber(string userNumber)
{
bool check = false;
while (!check)
{
check = true;
cout << "Please enter a Number: ";
cin >> userNumber;
if (userNumber.length() != NUM_LENGTH)
cout << "The phone number may contain 10 digits only. \n";
else
{
userNumber.insert(0, "(");
userNumber.insert(4, ")");
userNumber.insert(8, "-");
for (int i = 0; i < userNumber.length(); i++)
{
if (isdigit(userNumber[i]))
{
userNumber = NUM_LENGTH;
}
}
}
if (!check)
{
cout << "Invalid Entry! Please try again." << endl;
}
}
return userNumber;
}
|