Why will my program not compile correctly when I both functions together?
Here is the gist of what I am suppose to be doing.
The goal of this program is to be able to enter in a phone-number as a string, and format it into the standard format, (111) 111-1111.
With the first function we are suppose to enter in the phone-number, and then run it through a loop, and extract only the digits from that string and copy them to an other string.
Example: String_1 = (817)-733-5778
After Loop: New_String = 8177335778
Then in function two, we run the new string through a loop. If the phone number is exactly ten digits then it is put into standard format.
Example: New_String=8177335778
After Loop: String = (817) 733-5778
Here is what I have thus far
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
|
#include <iostream>
#include <string>
using namespace std;
//Function Prototypes
void Phone_Number(string);
string Modified_Number(string);
int main()
{
string Phone_Number_2;
Phone_Number(Phone_Number_2);
string New_Number = Modified_Number(Phone_Number_2);
cout << " The formatted number is " << New_Number << endl;
return 0;
}
void Phone_Number(string Phone_Number_2)
{
string Phone_Number;
getline(cin, Phone_Number);
for (int Index = 0; Index < Phone_Number.length(); Index++)
{
int x = 0;
string Phone_Number_2 = "" + Phone_Number[Index];
if (Phone_Number[Index] <= '9' && Phone_Number[Index] >= '0')
{
Phone_Number_2[x] = Phone_Number[Index];
cout << Phone_Number_2[x];
x++;
}
}
}
string Modified_Number(string Phone_Number_2)
{
int digit_length = 11;
if (Phone_Number_2.length() >= digit_length)
cout << "The phone number must be exactly 10 digits.\n";
else
cout << "The properly formatted number is "
<< "(" << Phone_Number_2[0] << Phone_Number_2[1] << Phone_Number_2[2] << ")"
<< " " << Phone_Number_2[3] << Phone_Number_2[4] << Phone_Number_2[5]
<< "-" << Phone_Number_2[6] << Phone_Number_2[7] << Phone_Number_2[8] << Phone_Number_2[9]
<< endl;
return Modified_Number;
}
|