Strings - Need assitance creating function headers & prototypes to pass a string to an other function.

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;
}






Last edited on
Hi Zzuniga,

The reason your code will not compile is your string Modified_Number(string Phone_Number_2) function is not returning any string. Ether make it return a string, or set it as a void return type.

Let us know if you have any questions.

Best.
Bdanielz,

Thank you for the reply! Can you send me links of articles on the topic of returning strings? Or better yet, could you show me how to return a string with a separate example code?

Thanks
Line 56: Modified_Number is not a string. It's the address of your function.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Hi Zzuniga,

Here are few examples using your problem as an example.

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>

using namespace std;
//Function Prototypes
void Phone_Number(string);
string Modified_Number(string);
string Modified_Number_alt(string input);

int main() {
    string aPhoneNumber = "5552221111";


    // assigning a value by returning a string
    string aModifiedNumber = Modified_Number (aPhoneNumber);

    // printing the variable.
    cout << aModifiedNumber << endl;

    // just returning a value and printing, but not assigning a variable.


    cout << Modified_Number_alt ("5551111234") << endl;
    return 0;
}

// formatting the sting simluar to how you were printing it. 
string Modified_Number(string input) {
    const int phoneLen = 10;
    string formattedNumber = "(   )   -     ";  // making a blank template.

    // this will cover too short and too big.
    if (input.length () != phoneLen)
        cout << "The phone number must be exactly 10 digits.\n";
    else {
        int pos1 = 1;
        int pos2 = 0;
        // skip '(' and add 3 from input to formattedNumber
        int areaCodeLen = 3;
        for (int i = 0; i < areaCodeLen; ++i) {
            formattedNumber[pos1++] = input[pos2++];
        }
        // skip )
        pos1++;
        int prefixLen = 3;
        for (int i = 0; i < prefixLen; ++i) {
            formattedNumber[pos1++] = input[pos2++];
        }
        //skip -
        pos1++;
        int suffixLen = 4;
        for (int i = 0; i < suffixLen; ++i) {
            formattedNumber[pos1++] = input[pos2++];
        }

    }
    return formattedNumber;
}

// another way to format the string
string Modified_Number_alt(string input) {
    const int phoneLen = 10;

    // this will cover too short and too big.
    if (input.length () != phoneLen)
        cout << "The phone number must be exactly 10 digits.\n";
    else {
        input.insert (0, "(");
        input.insert (4, ")");
        input.insert (8, "-");

    }
    return input;
}

Read more about returning data using functions.
(int, string ect..)
http://www.cplusplus.com/doc/tutorial/functions/

Let us know if you have any questions!
Topic archived. No new replies allowed.