Function Calls

So, I'm working on functions and I'm trying to get everything together but when I try to build the program I receive an error stating:

"error c3861: 'toDigits': identifier not found"
The line it mentions has the error is 60

Any insight or suggestions would be amazing!

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
//--------------------------------------------------------------------------------
//Programming Assignment:  LAB4
//Developer:               Sierra McGivney
//Date Written:            03/26/2014
//Purpose:                 Simulates dialing of a phone number.
//--------------------------------------------------------------------------------

#include <iostream>
#include <cctype>
#include <iomanip>
#include <string>
using namespace std;

//Main Program
int main()
{
	//Declarations
	int readDials(char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8); //Function variables
	char toDigits(char &d); //Function variables
	void ackCall(char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8); //Function variables
	char d = ' ';
	int rtrnValue = 0;

	//Loop Begins
	while (rtrnValue != -5)
	{
		char d1 = ' ', d2 = ' ', d3 = ' ', d4 = ' ', d5 = ' ', d6 = ' ', d7 = ' ', d8 = ' ';
		readDials(d1, d2, d3, d4, d5, d6, d7, d8);
		
		//Switch scenarios: Return Values
		switch (rtrnValue)
		{
		case -1: cout << "Error: Invalid character entered." << endl; break;
		case -2: cout << "Error: The number cannot start with a 1 or 0." << endl; break;
		case -3: cout << "Error: The number cannot begin with 555." << endl; break;
		case -4: cout << "Error: The fourth digit MUST be a hyphen (-)." << endl; break;

		default: ackCall(d1, d2, d3, d4, d5, d6, d7, d8);
		}
		return -5;
	}



}

//Functions:
int readDials(char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8)
{
	cout << "Enter an eight digit phone number with the fourth digit being a hyphen (Press Q to quit): ";
	cin >> d1;
	int rtrnValue = 0;
	switch (d1)
	{
	case  'q': rtrnValue = -5; return rtrnValue;
	case 'Q': rtrnValue = -5; return rtrnValue;
	default: rtrnValue = 0;
	}
	cin >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8;
	toDigits(d2, d3, d4, d5, d6, d7, d8);
	switch (rtrnValue)
	{
	case -1: return rtrnValue;
	case -2: return rtrnValue;
	case -3: return rtrnValue;
	case -4: return rtrnValue;
	}
}

char toDigits(char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8)
{
	char d;
	toupper(d);
	switch (d)
	{
	case 'A': case 'B': case 'C': d = 2; break;
	case 'D': case 'E': case 'F': d = 3; break;
	case 'G': case 'H': case 'I': d = 4; break;
	case 'J': case 'K': case 'L': d = 5; break;
	case 'M': case 'N': case 'O': d = 6; break;
	case 'P': case 'Q': case 'R': case 'S':d = 7; break;
	case 'T': case 'U': case 'V': d = 8; break;
	case 'W': case 'X': case 'Y': case 'Z': d = 9; break;
	case '0': case '1':case '2': case '3': case '4': case '5': case '6': case '7': case '8': break;
	default: return -1;
	}

}

void ackCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8) //acknowledge call
{
	cout << "You have dialed: " << d1 << d2 << d3 << d4 << d5 << d6 << d7 << d8 << endl;
	return;
	}
Last edited on
The function readDials doesn't know what the toDigit function is. The function prototypes can be moved out of and before the main function.

1
2
3
4
5
int readDials(char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8); //Function variables
char toDigits(char &d); //Function variables
void ackCall(char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8); //Function variables

int main()


But then - the function prototype for toDigits doesn't match up with the function definition in terms of the parameters sent. And you have it set to return a char, but it only returns -1 in its default case.

function prototype
char toDigits(char &d);

function definition
char toDigits(char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8)

default: return -1;


And I'm not sure what's supposed to happen with d in the toDigit function. A local variable is declared but not initialized, then used to determine the switch case. Did you mean to use the parameters that were sent in by reference?
1
2
3
char d;
toupper(d);
switch (d)
Last edited on
refer to below comments!
Last edited on
Errors are gone, but the program does not run as intended.

The desired result should be as explained below:


Pseudocode
Main Function
    Declare the char variables for the 8 digits of the phone number
    while true
        Call the ReadDials function passing the 8 digits
        by reference.  ReadDials returns an error code by
        value.
        If the return value is -5, exit the do while loop
        If the error code is -1, display the
          error message "ERROR - An invalid character was entered".
        If the error code is -2, display the
          error message "ERROR - Phone number cannot begin with 0".
        If the error code is -3, display the
          error message "ERROR - Phone number cannot begin with 555".
        If the error code is -4, display the
          error message "ERROR - Hyphen is not in the correct position".
        Otherwise, call the AcknowledgeCall function 
    End-While
ReadDials function
    Input the first digit
    If a Q was entered, return -5.
    Input the rest of the phone number
    Call the ToDigit function for each of the 7 digits
      not for digit 4
    If ToDigit returns -1, return -1
    If digit 4 is not a hyphen, return -4.
    If digit 1 is 0, return -2.
    If digits 1 - 3 are 5, return -3
    Otherwise, return 0
ToDigit function
    Convert the digit to upper case
    Use a switch statement to determine if the digit is valid
      and convert the letters to digits
    If the digit is invalid, return -1.
    If the digit is valid, return 0.
AcknowledgeCall function
    Display the Phone Number.


Enter a phone number (Q to quit): 213-2121
Phone Number Dialed: 213-2121

Enter a phone number (Q to quit): asc-dfer
Phone Number Dialed: 272-3337

Enter a phone number (Q to quit): 555-resw
ERROR - Phone number cannot begin with 555

Enter a phone number (Q to quit): 098-8765
ERROR - Phone number cannot begin with 0

Enter a phone number (Q to quit): 12345678
ERROR - Hyphen is not in the correct position

Enter a phone number (Q to quit): @34-*uyt
ERROR - An invalid character was entered

Enter a phone number (Q to quit): Q
Press any key to continue . . .



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
//--------------------------------------------------------------------------------
//Programming Assignment:  LAB4
//Developer:               Sierra McGivney
//Date Written:            03/26/2014
//Purpose:                 Simulates dialing of a phone number.
//--------------------------------------------------------------------------------

#include <iostream>
#include <cctype>
#include <iomanip>
#include <string>
using namespace std;


//Declarations: Functions
int readDials(char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8); //Function variables
int toDigits(char &d2, char &d3, char &d5, char &d6, char &d7, char &d8); //Function variables
int ackCall(char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8); //Function variables
char d1, d2, d3, d4, d5, d6, d7, d8;
char d;


//Main Program
int main()
{
	//Declarations
	char d = ' ';
	int rtrnValue = 0;
	//Loop Begins
	while (rtrnValue != -5)
	{
		char d1 = ' ', d2 = ' ', d3 = ' ', d4 = ' ', d5 = ' ', d6 = ' ', d7 = ' ', d8 = ' ';
		readDials(d1, d2, d3, d4, d5, d6, d7, d8);
		
		//Switch scenarios: Return Values
		switch (rtrnValue)
		{
		case -1: cout << "Error: Invalid character entered." << endl; return -5;
		case -2: cout << "Error: The number cannot start with a 1 or 0." << endl; return -5;
		case -3: cout << "Error: The number cannot begin with 555." << endl; return -5;
		case -4: cout << "Error: The fourth digit MUST be a hyphen (-)." << endl; return -5;

		default: ackCall(d1, d2, d3, d4, d5, d6, d7, d8);
		}
	}



}

//Functions:
int readDials(char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8)
{
	cout << "Enter an eight digit phone number with the fourth digit being a hyphen (Press Q to quit): ";
	cin >> d1;
	int rtrnValue = 0;
	//Switch scenarios: Quit
	switch (d1)
	{
	case  'q': rtrnValue = -5; return rtrnValue;
	case 'Q': rtrnValue = -5; return rtrnValue;
	case '1': case '0': rtrnValue = -2;
	default: rtrnValue = 0;
	}
	cin >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8;
	toDigits(d2, d3, d5, d6, d7, d8); //call function: toDigits
	if (d4 == 0x2d)
	{
		rtrnValue = -1;
	}
	if ((d1 == 5) && (d2 == 5) && (d3 == 5))
	{
		rtrnValue = -3;
	}
	//Switch Scenarios: Errors
	 switch (rtrnValue)
	{
	case -1: return rtrnValue;
	case -2: return rtrnValue;
	case -3: return rtrnValue;
	case -4: return rtrnValue;
	}
}

int toDigits(char &d2, char &d3, char &d5, char &d6, char &d7, char &d8)
{
	toupper(d);
	switch (d)
	{
	case 'A': case 'B': case 'C': d = 2; break;
	case 'D': case 'E': case 'F': d = 3; break;
	case 'G': case 'H': case 'I': d = 4; break;
	case 'J': case 'K': case 'L': d = 5; break;
	case 'M': case 'N': case 'O': d = 6; break;
	case 'P': case 'Q': case 'R': case 'S':d = 7; break;
	case 'T': case 'U': case 'V': d = 8; break;
	case 'W': case 'X': case 'Y': case 'Z': d = 9; break;
	case '0': case '1':case '2': case '3': case '4': case '5': case '6': case '7': case '8': break;
	default: return -1;
	}

}

int ackCall(char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8) //acknowledge call
{
	cout << "You have dialed: " << d1 << d2 << d3 << d4 << d5 << d6 << d7 << d8 << endl;
	return -5;
	}

Last edited on
Generally it's not a good idea to global variables - it's OK to pull the function prototypes out of main, but not variable declarations.

What are the program and individual functions supposed to be doing? Is there a description of the problem that you can copy here?
Write a program that simulates the dialing of a phone number.

A user will input an 8-place number, for example: 359-3177 (note that the hyphen is considered a digit).
The rules for entering phone numbers follow.

• 8 places
• It may have numbers, letters, or both.
• The phone number cannot begin with 555.
• The phone number cannot begin with 0 or 1.
• The hyphen must be in the 4th position.
• No other characters (@#$%^&*()_+=\|/><etc.) are allowed.
• If a letter is entered, its output will be a number (check your phone pad).
• Enter Q to Quit.

If all of the rules are met, you will output a message to the console that reads like the following.
Phone Number Dialed: UN9-3177 *the number entered

If all of the rules are not met, then you output one of the following error messages to the console.

• ERROR - Phone number cannot begin with 555
• ERROR - Phone number cannot begin with 0
• ERROR - Hyphen is not in the correct position
• ERROR - An invalid character was entered

It will then prompt the user to try again.

---More---


Step 1: Requirements: Phone-Dialing Program
Write a program that simulates the dialing of a phone number.

A user will input an 8-place number, for example: 359-3177 (note that the hyphen is considered a digit).
The rules for entering phone numbers follow.

• 8 places
• It may have numbers, letters, or both.
• The phone number cannot begin with 555.
• The phone number cannot begin with 0 or 1.
• The hyphen must be in the 4th position.
• No other characters (@#$%^&*()_+=\|/><etc.) are allowed.
• If a letter is entered, its output will be a number (check your phone pad).
• Enter Q to Quit.

If all of the rules are met, you will output a message to the console that reads like the following.
Phone Number Dialed: UN9-3177 *the number entered

If all of the rules are not met, then you output one of the following error messages to the console.

• ERROR - Phone number cannot begin with 555
• ERROR - Phone number cannot begin with 0
• ERROR - Hyphen is not in the correct position
• ERROR - An invalid character was entered

It will then prompt the user to try again.


This should be a lot of fun!
Here are some great things to think about as you begin your program!

Define a function named ReadDials() that reads each digit and letter dialed into 8 separate char variables (DO NOT USE ARRAYS). All the digits are sent back through parameters by reference.

Then, for each digit, the program will use a function named ToDigit(), which receives a single char argument (pass by reference) that may be a number or a letter of one of the digits dialed.

If it is a number, then return 0 by value indicating that it is a valid digit. If the digit is a letter, then the number corresponding to the letter is returned by reference, and return 0 by value indicating that it is a valid digit. Here are the letters associated with each digit.




2;A B C
3;D E F
4;G H I
5 ;J K L
6 ;M N O
7; P Q R S
8; T U V
9; W X Y Z

If the digit entered is not one of the valid digits or one of the valid letters, return –1 by value indicating that you have an invalid digit.

A phone number never begins with a 0, so the program should flag an error if such a number is entered. Make ReadDials() return –2 in this case.

A phone number never begins with 555, so the program should flag an error if such a number is entered. Make ReadDials() return –3 in this case.

A phone number always has a hyphen (-) in the 4th position. Make ReadDials() return –4 in this case (if it doesn't have a hyphen in the 4th position). If a hyphen is in any other position, it is considered an invalid digit.

If the phone number is valid, the main calls the AcknowledgeCall function to write the converted number to the output file.

All the logic of the program should be put in functions that are called from Main(): ReadDials() and AcknowledgeCall().

The ToDigits() function is called from the ReadDials() function and is used to convert each letter entered individually into a digit and to verify that the user has entered a valid phone number. Have the program work for any number of phone numbers.

In the ToDigits() function uses the toupper function to convert any letters entered to uppercase. All the error messages are to be written to the output file from main() based on the return value from the functions.

Continue processing until the user enters a Q.

You will set up the 8 char variables to hold the digits of the phone number in main() and pass the variables to the functions by reference.

Sample Output from the Program

Enter a phone number (Q to quit): 213-2121
Phone Number Dialed: 213-2121

Enter a phone number (Q to quit): asc-dfer
Phone Number Dialed: 272-3337

Enter a phone number (Q to quit): 555-resw
ERROR - Phone number cannot begin with 555

Enter a phone number (Q to quit): 098-8765
ERROR - Phone number cannot begin with 0

Enter a phone number (Q to quit): 12345678
ERROR - Hyphen is not in the correct position

Enter a phone number (Q to quit): @34-*uyt
ERROR - An invalid character was entered

Enter a phone number (Q to quit): Q
Press any key to continue . . .


Step 2: Processing Logic
Using the pseudocode below, write the code that will meet the requirements.
Main Function
Declare the char variables for the 8 digits of the phone number
while true
Call the ReadDials function passing the 8 digits
by reference. ReadDials returns an error code by
value.
If the return value is -5, exit the do while loop
If the error code is -1, display the
error message "ERROR - An invalid character was entered".
If the error code is -2, display the
error message "ERROR - Phone number cannot begin with 0".
If the error code is -3, display the
error message "ERROR - Phone number cannot begin with 555".
If the error code is -4, display the
error message "ERROR - Hyphen is not in the correct position".
Otherwise, call the AcknowledgeCall function
End-While
ReadDials function
Input the first digit
If a Q was entered, return -5.
Input the rest of the phone number
Call the ToDigit function for each of the 7 digits
not for digit 4
If ToDigit returns -1, return -1
If digit 4 is not a hyphen, return -4.
If digit 1 is 0, return -2.
If digits 1 - 3 are 5, return -3
Otherwise, return 0
ToDigit function
Convert the digit to upper case
Use a switch statement to determine if the digit is valid
and convert the letters to digits
If the digit is invalid, return -1.
If the digit is valid, return 0.
AcknowledgeCall function
Display the Phone Number.




Last edited on
Then, for each digit, the program will use a function named ToDigit(), which receives a single char argument (pass by reference) that may be a number or a letter of one of the digits dialed.


It sounds like they want ToDigit to handle just one character at a time. So you'd call the function for each of the 7 non-hyphen digits one at a time, sending d1 etc. by reference, and performing the checks on that d1 character - you don't need to create a new local variable d in ToDigit - you want function to act on whatever existing variable you pass in.
I'm sorry if this is a really dumb question, but I'm not quite gathering how to pass them to the toDigit one by one. How would I do this?

I imagine there would need to be a loop, but my efforts aren't working.

Is that why my code program won't even recognize when I enter a number in the fourth place holder or a 555 for the first three numbers?

I altered my code a bit in an attempt to make it work, if you can, please give me an example of how to make a loop in order to make the function work.

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
int readDials(char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8)
{
	cout << "Enter an eight digit phone number with the fourth digit being a hyphen (Press Q to quit): ";
	cin >> d1; 
	int rtrnValue = 0;
	//Switch scenarios: Quit
	switch (d1)
	{
	case  'q': rtrnValue = -5; return rtrnValue;
	case 'Q': rtrnValue = -5; return rtrnValue;
	case '1': case '0': rtrnValue = -2;
	default: rtrnValue = 0;
	}//would the loop be here, kind of like (cin >> &d; toDigits(d); and something to increment the cin d#?
		cin >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8;
	toDigits(d); //call function: toDigits
	if (d4 != '-')
	{
		rtrnValue = -1;
	}
	else if ((d1 == 5) && (d2 == 5) && (d3 == 5))
	{
		rtrnValue = -3;
	}
	//Switch Scenarios: Errors
	switch (rtrnValue)
	{
	case -1: return rtrnValue;
	case -2: return rtrnValue;
	case -3: return rtrnValue;
	case -4: return rtrnValue;
	default: rtrnValue = 0;
	}
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int toDigits(char &d)
{
	//Would there be a loop before this? I tried to do a dowhile, 
	//for, and while, but things didn't work out the right way. 
	//Very sorry, I'm just not comprehending this completely yet.
		toupper(d);
		switch (d)
		{
		case 'A': case 'B': case 'C': d = 2; break;
		case 'D': case 'E': case 'F': d = 3; break;
		case 'G': case 'H': case 'I': d = 4; break;
		case 'J': case 'K': case 'L': d = 5; break;
		case 'M': case 'N': case 'O': d = 6; break;
		case 'P': case 'Q': case 'R': case 'S':d = 7; break;
		case 'T': case 'U': case 'V': d = 8; break;
		case 'W': case 'X': case 'Y': case 'Z': d = 9; break;
		case '0': case '1':case '2': case '3': case '4': case '5': case '6': case '7': case '8': break;
		default: return -1;
		}
}
I played around a bit with this. I have an if statement checking that each of the digits is valid, not using a loop. Seems to work OK so far, but maybe someone else has another creative solution. I know they don't want you to use an array.

Your 555 check isn't working because with a char you need to check for '5' not just 5. The assignments in the conversion of letters to numbers also need to have the number in single quotes.

I spaced out the cases in the toDigits just to make sure everything was there. It looks like you're missing case '9'. Edit: and you need to return 0 if the digit is valid.

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
#include <iostream>
#include <cctype>
using namespace std;

int ReadDials(char&, char&, char&, char&, char&, char&, char&, char&);
int toDigit(char&);
void AcknowledgeCall(char, char, char, char, char, char, char, char);

int main()
{
	//Declare the char variables for the 8 digits of the phone number
	char d1=' ', d2=' ', d3=' ', d4=' ', d5=' ', d6=' ', d7=' ', d8=' ';
	int checkvalue = 1;

	while (checkvalue !=-5)
	{
		checkvalue = ReadDials(d1, d2, d3, d4, d5, d6, d7, d8);

		switch (checkvalue)
		{
			case -5:	break;
			case -1:	cout << "ERROR - An invalid character was entered." << endl;
						break;
			case -2:	cout << "ERROR - Phone number cannot begin with 0" << endl;
						break;
			case -3:	cout << "ERROR - Phone number cannot begin with 555" << endl;
						break;
			case -4:	cout << "ERROR - Hyphen is not in the correct position" << endl;
						break;
			case 0:		AcknowledgeCall(d1, d2, d3, d4, d5, d6, d7, d8);
					break;
			default:	break;
		}
	}

	return 0;
}	

int ReadDials(char& rd1, char& rd2, char& rd3, char& rd4, char& rd5, char& rd6, char& rd7, char& rd8)
{
	cout << "Enter a phone number (Q to quit): ";
	cin >> rd1;
	//check for Quit
	if (rd1=='Q' || rd1=='q')
		return -5;
	//input remaining characters
	cin >> rd2 >> rd3 >> rd4 >> rd5 >> rd6 >> rd7 >> rd8;

	//check for invalid formats	
	if (rd1=='0') 	//check for 0 in first position
		return -2;
	if (rd1=='5' && rd2=='5' && rd2=='5' ) //check for 555 in first 3 positions
		return -3;
	if (rd4!='-')// check for hyphen in fourth position
		return -4;

	//check for any invalid digits in toDigit function except position 4 (hyphen)
	if (toDigit(rd1) == 0 && toDigit(rd2) == 0 && toDigit(rd3) == 0 && toDigit(rd5) == 0 && toDigit(rd6) == 0 && toDigit(rd7) == 0 && toDigit(rd8) == 0)
		return 0;
	else
		return -1;
}

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

void AcknowledgeCall(char a1, char a2, char a3, char a4, char a5, char a6, char a7, char a8)
{
	cout << "Phone Number Dialed: " << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << endl;
}
Last edited on
Thank you very much for your help! I think I've managed to figure out most of what had me confused, and your code was very clear and easy to understand. I'll have to work on that!

Once again, I appreciate your assistance and willingness to go above and beyond while answering the questions. I hadn't expected you to go and write the whole code!

A quick last question if you don't mind, when you set the variables inside the functions to (char, char, char) or (char&, char&, char&), why does it allow you to do this without variable names?

Also, why does the acknowledgeCall function not require the & symbols? Are they not being referenced to?

Hope these questions aren't too dumb. I'd just like to know for future reference.
when you set the variables inside the functions to (char, char, char) or (char&, char&, char&), why does it allow you to do this without variable names?


With the function prototype declaration, you just need to tell the compiler the return type of the function, the name of the function, and the number and type of parameters. You don't need to include the parameter names - the compiler will just ignore them at that point.

Also, why does the acknowledgeCall function not require the & symbols? Are they not being referenced to?


The acknowledgeCall function just prints out the values of the variables. The function has no need to change the original value, so by the principle of least privilege, you don't need to give it that access - you can just pass the variables by value, not by reference. The other functions, ReadDials and toDigit, do change the value of the variable passed to them, so the variables do need to be passed by reference there.

Passing by reference - rd1 is sent to toDigit function
if (toDigit(rd1) == 0

int toDigit(char& td)

toDigit function sees the & operator and knows not to create a new memory location for a local variable td, but to consider td an alias, or just another name of, the original variable rd1 (which in turn is another name for d1 in main function).

So any change to td is actually a change to the original d1 variable.
case 'Z': td = '9';
Last edited on
That's very cool! Thank you for explaining to me! I'm pretty certain I understand it now.

Just in case, I'm going to repeat what you said in my own words.

So, the value of the entries is between each function, and the names are all temporary. But, when the value is changed they have to pass by reference so that the value changes when it returns?

The & operator allows the function to know it doesn't need a new memory location and that it is considered a temporary name?

And lastly, all changes of value made are made to the variables entered originally, therefore replacing their original values?
I think you have it. If you send parameters by reference - you're not creating new variables in the called function - you're creating an alias to the variable in the calling function. If you send parameters by value - you're creating variables in the called function which get a copy of the value from the calling function.

Here's a short example.

Pass by value - original value is not modified. 
The function gets a copy of number to work with 
but it has no access to change number in the main function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

void doublenum(int x)
{
	x = 2*x;
}
int main() 
{
	int number=5;
	cout << number << endl;
	doublenum(number);
	cout << number;
	return 0;
}
5
5


Pass by reference - original value is modified. 
x in this case is just another name for number, so the
variable in main is changed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

void doublenum(int &x)
{
	x = 2*x;
}
int main() 
{
	int number=5;
	cout << number << endl;
	doublenum(number);
	cout << number;
	return 0;
}
5
10
Last edited on
Oh, that example is great! It makes it easier to put the image in my head.

This made perfect sense upon looking at the examples, of course, without your explanation I wouldn't have gotten it at all.

Thank you, thank you, thank you so much!
Topic archived. No new replies allowed.