Any help regarding functions?

I have an assignment that I have been working for a week and a half already and I can't seem to get any closer to completing it and it is really messing with me and now I am here asking for help. So now my issue is that I am having trouble with calling functions. I got lucky with the first function (being able to input my a phone number) but I am not as lucky with the other calls. I also am getting build errors when I build my code.

These are the errors:

I get this error six times ► \lab04source.cpp(49) : see declaration of 'ToDigit'

and I get this error six times as well along with the error above (error happens on multiple lines between lines 49 and 61) ► lab04source.cpp(61): error C2374: 'ToDigit' : redefinition; multiple initialization

This is my code:

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//-----------------------------------------------
// Programming Assignment:	Phone dialing program
// Developer:			bulletfingrz
// Date Written:	        01/29/2014
// Purpose:		        Phone-Dialing Program
// ----------------------------------------------

#include <iostream>
using namespace std;



int ReadDials(char &num1, char &num2, char &num3, char &num4, char &num5, char &num6, char &num7, char &num8)
{
	
	
	
	//Call Input
	cout << "Enter a phone number: ";
	cin >> num1,num2,num3,num4,num5,num6,num7,num8;
	

	//IF's & Returns
	if (num1 == 'Q')
	{
		return -5;
	}
	else if (num4 != '-')
	{
		return -4;
	}
	else if (num1 == 0)
	{
		return -2;
	}
	else if (num1 == 5 && num2 == 5 && num3 == 5)
	{
		return -3;
	}
	else
	{
		return 0;
	}

	
		//calling ToDigit 7x
	
		char ToDigit(num1);

		char ToDigit(num2);

		char ToDigit(num3);

		char ToDigit(num5);

		char ToDigit(num6);

		char ToDigit(num7);

		char ToDigit(num8);

	


}



int ToDigit(char &numDialed)
{
	//lowertoupper
	if (islower(numDialed))
	{
		toupper(numDialed);
	}
	

	switch(numDialed)
	{
	case 'A': 2;
		return 0;
	case 'B': 2;
		return 0;
	case 'C': 2;
		return 0;
	case 'D': 3;
		return 0;
	case 'E': 3;
		return 0;
	case 'F': 3;
		return 0;
	case 'G': 4;
		return 0;
	case 'H': 4;
		return 0;
	case 'I': 4;
		return 0;
	case 'J': 5;
		return 0;
	case 'K': 5;
		return 0;
	case 'L': 5;
		return 0;
	case 'M': 6;
		return 0;
	case 'N': 6;
		return 0;
	case 'O': 6;
		return 0;
	case 'P': 7;
		return 0;
	case 'Q': 7;
		return 0;
	case 'R': 7;
		return 0;
	case 'S': 7;
		return 0;
	case 'T': 8;
		return 0;
	case 'U': 8;
		return 0;
	case 'V': 8;
		return 0;
	case 'W': 9;
		return 0;
	case 'X': 9;
		return 0;
	case 'Y': 9;
		return 0;
	case 'Z': 9;
		return 0;
	default:
		return -1;
	}


	
}

void AcknowledgeCall(char num1, char num2, char num3, char num4, char num5, char num6, char num7, char num8)
{
	cout << "number dialed: " << AcknowledgeCall << endl;
}


int main()
{
	//Variables
	int errorCode;
	char num1, num2, num3, num4, num5, num6, num7, num8;
	char newCall;
	

	//newCall LOOP
	do
	{

		//Calling ReadDials
		errorCode = ReadDials(num1, num2, num3, num4, num5, num6, num7, num8);


		//ErrorCode exit
		if (errorCode == -5)
		{
			break;
		}

		//ErrorCode If's
		if (errorCode == -1)
		{
			cout << "ERROR - Invalid character was entered. " << endl;
		}
		else if (errorCode == -2)
		{
			cout << "ERROR - Phone number cannot begin with 0. " << endl;
		}
		else if (errorCode == -3)
		{
			cout << "ERROR - Phone number cannot begin with 555. " << endl;
		}
		else if (errorCode == -4)
		{
			cout << "ERROR - Hyphen is not in the correct position. " << endl;
		}
		else
			{
				//calling acknowledgeCall
				void AcknowledgeCall();
		}
		
		

		//NewCall
		cout << "Would you like to make a new call? ";
		cin >> newCall;
		

		
	}
	while (newCall == 'Y' || newCall == 'y');

	
}


I'll be waiting patiently for a response! :)
Last edited on
The error your getting 6 times is because you have char in front of your function ToDigit(xxx).
Lines 48-60
As mentioned, your problems are due to the fact that you've written the char type keyword in-front of your function calls for no particular reason.
In addition, you're missing a function prototype (forward declaration).
I got rid of the char in front of my ToDigits now I am getting 7 errors from lines 48 - 60 all the errors say: error c3861 'ToDigit' : Identifier not found

//Call Input
cout << "Enter a phone number: ";
cin >> num1,num2,num3,num4,num5,num6,num7,num8;

You also need to move your 'call input' inside your main function. It needs to go in the do loop before your read dials because without it you have nothing to pass into your readdial function.
Alight so I my moved //Call input into the do while loop and I also made sure to correct the forward declaration issue and the char type issue as well now. I went ahead and compiled and I keep getting my error "ERROR - Hyphen is not in the correct position. " when the hyphen was in fact in the correct position.

This is the code so far now:

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <iostream>
using namespace std;
int ToDigit(char &numDialed);



int ReadDials(char &num1, char &num2, char &num3, char &num4, char &num5, char &num6, char &num7, char &num8)
{
	
	
	

	//IF's & Returns
	if (num1 == 'Q')
	{
		return -5;
	}
	else if (num4 != '-')
	{
		return -4;
	}
	else if (num1 == 0)
	{
		return -2;
	}
	else if (num1 == 5 && num2 == 5 && num3 == 5)
	{
		return -3;
	}
	else
	{
		return 0;
	}

	
		//calling ToDigit 7x
	
		 ToDigit(num1);

		 ToDigit(num2);

		 ToDigit(num3);

		 ToDigit(num5);

		 ToDigit(num6);

		 ToDigit(num7);

		 ToDigit(num8);

	


}



int ToDigit(char &numDialed)
{
	//lowertoupper
	if (islower(numDialed))
	{
		toupper(numDialed);
	}
	

	switch(numDialed)
	{
	case 'A': 2;
		return 0;
	case 'B': 2;
		return 0;
	case 'C': 2;
		return 0;
	case 'D': 3;
		return 0;
	case 'E': 3;
		return 0;
	case 'F': 3;
		return 0;
	case 'G': 4;
		return 0;
	case 'H': 4;
		return 0;
	case 'I': 4;
		return 0;
	case 'J': 5;
		return 0;
	case 'K': 5;
		return 0;
	case 'L': 5;
		return 0;
	case 'M': 6;
		return 0;
	case 'N': 6;
		return 0;
	case 'O': 6;
		return 0;
	case 'P': 7;
		return 0;
	case 'Q': 7;
		return 0;
	case 'R': 7;
		return 0;
	case 'S': 7;
		return 0;
	case 'T': 8;
		return 0;
	case 'U': 8;
		return 0;
	case 'V': 8;
		return 0;
	case 'W': 9;
		return 0;
	case 'X': 9;
		return 0;
	case 'Y': 9;
		return 0;
	case 'Z': 9;
		return 0;
	default:
		return -1;
	}


	
}

void AcknowledgeCall(char num1, char num2, char num3, char num4, char num5, char num6, char num7, char num8)
{
	cout << "number dialed: " << AcknowledgeCall << endl;
}


int main()
{
	//Variables
	int errorCode;
	char num1, num2, num3, num4, num5, num6, num7, num8;
	char newCall;
	
	

	//newCall LOOP
	do
	{
		//Call Input
		cout << "Enter a phone number: ";
		cin >> num1,num2,num3,num4,num5,num6,num7,num8;

		//Calling ReadDials
		errorCode = ReadDials(num1, num2, num3, num4, num5, num6, num7, num8);


		//ErrorCode exit
		if (errorCode == -5)
		{
			break;
		}

		//ErrorCode If's
		if (errorCode == -1)
		{
			cout << "ERROR - Invalid character was entered. " << endl;
		}
		else if (errorCode == -2)
		{
			cout << "ERROR - Phone number cannot begin with 0. " << endl;
		}
		else if (errorCode == -3)
		{
			cout << "ERROR - Phone number cannot begin with 555. " << endl;
		}
		else if (errorCode == -4)
		{
			cout << "ERROR - Hyphen is not in the correct position. " << endl;
		}
		else
			{
				//calling acknowledgeCall
				void AcknowledgeCall();
		}
		
		

		//NewCall
		cout << "Would you like to make a new call? ";
		cin >> newCall;
		

		
	}
	while (newCall == 'Y' || newCall == 'y');

	
}
give me a little time to look over the rest and see whats going on...in the mean time I would recommend stepping through your code one line at a time..See whats actually in your variables and see if they are changing when you expect them to. I don't know which IDE your using but most have a feauture that allows you to watch variables.
I went ahead and compiled and I keep getting my error "ERROR - Hyphen is not in the correct position. " when the hyphen was in fact in the correct position.

Including the full text of the error message (which included a line number) rather than paraphrasing the message would be useful.

cin >> num1,num2,num3,num4,num5,num6,num7,num8;
doesnt' do what you think it does.

That snippet is equivalent to:
1
2
3
4
5
6
7
8
    cin >> num1 ;
    num2;
    num3;
    num4;
    num5;
    num6;
    num7;
    num8;

where all of the statements except the first are essentially no-ops.

You probably wanted:
cin >> num1 >> num2 >> num3 ...

On line 7, ReadDials promises to return a value. It does... sometimes. And sometimes it doesn't.

Lines 38 to 50 serve no purpose since you do nothing with the return value.

Line 64 in your code returns the upper case version of numDialed, but does not change numDialed. Since you don't do anything with the return value, that line may as well not be there.

The statement after the labels on the even numbered lines between 70 and 120 do nothing.
You probably meant something like:
1
2
3
4
5
6
7
    switch(numDialed)
    {
    case 'A': return 2;
    case 'B': return 2;
    case 'C': return 2;
    case 'D': return 3;
    ...


which might be more readable as:
1
2
3
4
5
6
7
8
9
10
    switch(numDialed)
    {
    case 'A':     // fall through to the next case..
    case 'B': 
    case 'C':    return 2 ;

    case 'D': 
    case 'E':
    case 'F':    return 3 ;
    ... 


Line 132 in your code prints the address of a function and ignores all of the parameters it receives.

Line 182 in your code is a function prototype.

[Edit: typo]
Last edited on
@llb02 I am using Microsoft Visual Studio and Thanks again for the help!

@cire I went ahead and looked at what you pointed out and made corrections to it. It worked and it accepted my numbers and I also tested my error returns and I got those to work as well now there are only two problems that are holding me back. The first problem is that when I enter "Q" the program will not stop, it will accept "Q" but when I hit enter it won't exit it expects more number to be entered so that it can quit. The second issue is that when I enter a number with letters in it it will not convert those letter to numbers (asf-7895 to 273-7895) the output gives me asf-7895 instead of 273-7895.

this is what I have now:

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <iostream>
using namespace std;
char ToDigit(char &numDialed);

char num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0, num7 = 0, num8 = 0;

int ReadDials(char &num1, char &num2, char &num3, char &num4, char &num5, char &num6, char &num7, char &num8)
{
	
	//incase something goes wrong: int ReadDials(char num1, char num2, char num3, char &num4, char &num5, char &num6, char &num7, char &num8)

	//Call Input
		//cout << "Enter a phone number: ";
		//cin >> num1;
		//cin >> num2;
		//cin >> num3;
		//cin >> num4;
		//cin >> num5;
		//cin >> num6;
		//cin >> num7;
		//cin >> num8;
	

	//IF's & Returns
	if (num1 == 'Q')
	{
		return -5;
	}
	else if (num4 != '-')
	{
		return -4;
	}
	else if (num1 == '0')
	{
		return -2;
	}
	else if (num1 == '5' && num2 == '5' && num3 == '5')
	{
		return -3;
	}
	else
	{
		return -6;
	}

	
		//calling ToDigit 7x
	
		 ToDigit(num1);

		 ToDigit(num2);

		 ToDigit(num3);

		 ToDigit(num5);

		 ToDigit(num6);

		 ToDigit(num7);
		 
		 ToDigit(num8);

	


}


char ToDigit(char &numDialed)
{
	//lowertoupper
	if (islower(numDialed))
	{
		toupper(numDialed);
	}
	

	//switch(numDialed)
	//{
	//case 'A': return 2;
	//case 'B': 2;
	//	return 0;
	//case 'C': 2;
	//	return 0;
	//case 'D': 3;
	//	return 0;
	//case 'E': 3;
	//	return 0;
	//case 'F': 3;
	//	return 0;
	//case 'G': 4;
	//	return 0;
	//case 'H': 4;
	//	return 0;
	//case 'I': 4;
	//	return 0;
	//case 'J': 5;
	//	return 0;
	//case 'K': 5;
	//	return 0;
	//case 'L': 5;
	//	return 0;
	//case 'M': 6;
	//	return 0;
	//case 'N': 6;
	//	return 0;
	//case 'O': 6;
	//	return 0;
	//case 'P': 7;
	//	return 0;
	//case 'Q': 7;
	//	return 0;
	//case 'R': 7;
	//	return 0;
	//case 'S': 7;
	//	return 0;
	//case 'T': 8;
	//	return 0;
	//case 'U': 8;
	//	return 0;
	//case 'V': 8;
	//	return 0;
	//case 'W': 9;
	//	return 0;
	//case 'X': 9;
	//	return 0;
	//case 'Y': 9;
	//	return 0;
	//case 'Z': 9;
	//	return 0;
	//default:
	//	return -1;
	//}

	//alternative switch
	switch(numDialed)
	{
	case 'A':
	case 'B':
	case 'C':	return 2 ;

	case 'D':
	case 'E':
	case 'F':	return 3 ;

	case 'G':
	case 'H':
	case 'I':	return 4 ;

	case 'J':
	case 'K':
	case 'L':	return 5 ;

	case 'M':
	case 'N':
	case 'O':	return 6 ;

	case 'P':
	case 'Q':
	case 'R':
	case 'S':	return 7 ;

	case 'T':
	case 'U':
	case 'V':	return 8 ;

	case 'W':
	case 'X':
	case 'Y':
	case 'Z':	return 9 ;

	default:	return -1 ;
	}


	
}

void AcknowledgeCall(char num1,char num2,char num3,char num4,char num5,char num6,char num7,char num8)//char num1, char num2, char num3, char num4, char num5, char num6, char num7, char num8)
{
	cout << "Number dialed: " << num1; 
		cout << num2;
		cout << num3;
		cout << num4 <<num5 << num6 << num7 << num8 << endl << endl;
}



int main()
{
	//Variables
	int errorCode;
	
	char newCall;
	bool state = false;
	

	//newCall LOOP
	do
	{

		//Call Input
		cout << "Enter a phone number: ";
		cin >> num1;
		cin >> num2;
		cin >> num3;
		cin >> num4;
		cin >> num5;
		cin >> num6;
		cin >> num7;
		cin >> num8;

		

		//Calling ReadDials
		errorCode = ReadDials(num1, num2, num3, num4, num5, num6, num7, num8);


		//ErrorCode exit
		if (errorCode == -5)
		{
			break;
		}

		//ErrorCode If's
		if (errorCode == -1)
		{
			cout << "ERROR - Invalid character was entered. " << endl;
		}
		else if (errorCode == -2)
		{
			cout << "ERROR - Phone number cannot begin with 0. " << endl;
		}
		else if (errorCode == -3)
		{
			cout << "ERROR - Phone number cannot begin with 555. " << endl;
		}
		else if (errorCode == -4)
		{
			cout << "ERROR - Hyphen is not in the correct position. " << endl;
		}
		else if (errorCode == -6)
			{
				//calling acknowledgeCall
				 AcknowledgeCall(num1, num2, num3, num4, num5, num6, num7, num8);
		}
		
		

		//NewCall
		cout << "Would you like to make a new call? \n(Enter 'Y' for yes or 'N' for no)" << endl;
		cin >> newCall;
		
		if (newCall == 'Y' || newCall == 'y')
			state =false;
		else
			state =true;
			
		
	}
	while (!state);

	
}


All of the comments I made concerning ReadDial have yet to be addressed.

Also you changed the signature for ToDigit which makes me think you should be returning char representations rather than ints (ie. return '2'; rather than return 2;).

You might also want to consider whether ToDigit should return a value, since it takes a reference to char as a parameter. Modifying that value may be what you intended to do in the first place. I prefer the return value, however. And if you're returning the value, there is no need to pass the argument to the function by reference.
Last edited on
Topic archived. No new replies allowed.