Passing values between 2 functions

When I try to compile my program, I get a build error that n1 and n2 aren't being initialized. I want to start my first function Input(), send the values to main, and then copy the values to Compute() to display the computed value.Any assistance is greatly appreciated.

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

class Race
{
	int from;
	int to;
public:
	int Input(int from, int to);
	int Compute(int n1, int n2);
};

int Race::Input(int from, int to)
{
	cout << "Beginning lap = ";
	cin >> from;	
	cout << endl << "Ending lap = ";
	cin >> to;

	return (from, to);
}

int Race::Compute(int n1, int n2)
{
	int total;

	total = n2 - n1;
	cout << "You have done " << total << " laps" << endl;

	return total;
}

int main()
{
	Race A;
	int from, to, n1, n2;
//Run Input() function and return values to main
	A.Input(from, to);
//Copy values into Compute() and display the calculated file.
	A.Compute(n1, n2);

	system("pause");
	return 0;
}
http://www.cplusplus.com/doc/tutorial/functions/
int /* <- pay attention to that */ Race::Input(int from, int to)
you say that you would return one integer
if you say that you would return one integer, then you can only return one integer
You cannot return two integers, you cannot return a double, you cannot return an string.
You can only return one integer.


The two parameters of your function (from and to) are passed by copy
They are different variables that the one on main(), so any change that you make on them would not have any effect in the variables on main()
They also have no relation with the member variables of the class.


> I get a build error that n1 and n2 aren't being initialized
You never assign any value to those variables.
¿where do you think that you are setting them?
Last edited on
int /* <- pay attention to that */ Race::Input(int from, int to)
you say that you would return one integer
if you say that you would return one integer, then you can only return one integer
You cannot return two integers, you cannot return a double, you cannot return an string.
You can only return one integer.


Thanks for the link and for the explanation.

>
I get a build error that n1 and n2 aren't being initialized
You never assign any value to those variables.
¿where do you think that you are setting them?


1
2
3
4
5
6
//Run Input() function and return values to main
	A.Input(from, to);
n1 = from;
n2 = to;
//Copy values into Compute() and display the calculation.
	A.Compute(n1, n2);


Sorry, I should have been more descriptive here. My intention here is to run A.Input and have it return its value so that it goes into A.Compute. I thought that by issuing a return statement in A.Input, it returns the value in main so I can move it over to the next function like how i showed.
Last edited on
1
2
n1 = from;
n2 = to;
that's exactly what's missing in your first post.
It would still not work, as you are passing `from' and `to' by copy

You may pass them by reference instead
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
void Race::Input(int &from, &int to) //note the &
//modify the prototype according
//also note the `void', do not return anything
{
	cout << "Beginning lap = ";
	cin >> from;	
	cout << endl << "Ending lap = ";
	cin >> to;

	//return (from, to);
}

int main()
{
	Race A;
	int from, to, n1, n2;
//Run Input() function and modifying the variables passed
	A.Input(from, to); //the call remains the same

	n1 = from; //setting the variables
	n2 = to;
//call the other function
	A.Compute(n1, n2);

	// system("pause"); //see http://www.cplusplus.com/forum/articles/11153/ and http://www.cplusplus.com/articles/iw6AC542/
	return 0;
}



Also, you don't use the state of the object at all, so you may reconsider the design and not use classes (or use the state instead of passing variables around)


Perhaps you've got a conceptual error. The name of the parameter is irrelevant, only the function knows about it. These would be valid calls
1
2
3
A.Compute(from, to);
A.Compute(from, from);
A.Compute(42, 13);
Last edited on
Topic archived. No new replies allowed.