3 Functions with Loops

Hi guys,

Trying to solve an issue while studying but I am stuck.

Basically,

I have 3 functions.

For the first function, I have to convert an input from gallons to cubic centimeters.

For the second function, I have to convert another input from pounds to grams.

The third function tests those 2 numbers and returns a True or False. My question is, how do I get the new values from function 1 and 2 to run in the third function.

I do not know how to save the result of function 1 as a variable to later use in function 3.

Thanks!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
bool function3(int value1, int value2)
{
   ...
}

main()
{
   int firstValue = function1();
   int secondValue = function2();

   bool result = function3(firstValue, secondValue);
}
Last edited on
Hi everyone.. what am I doing wrong here ? Please explain.

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

double convOne(double input2) {
	double output1;
	output1 = input2/0.003;
	return output1;
}

double convTwo(double input1) {
	double output2;
	output2 = input1/.0003;
	return output2;
}

bool goldTest(double output2, double output1) {
	int fact1 = output2 * 20;
	int fact2 = output1 * .98;
	int fact3 = output1 * 1.02;
	if ((fact1 > fact2) && (fact1 < fact3)) {
		cout << "This works" << endl;
	return true;
	}
	else {
		cout << "This does not work" << endl;
	}
	return false;
}

int main() {

	double input1;
	double input2;
	double output1;
	double output2;

    cout << "Enter input1 " << endl; 
	cin >> input1; 
	cout << "Enter input2 " << endl;
	cin >> input2;
	convTwo(input1);
	convOne(input2);
    Test(convTwo(output2), convOne(output1));

	return 0;
}
Several things wrong. Line 41 calls an undefined function Test(), looks like it should be goldTest().

At lines 39 and 40 functions convOne() and convTwo() are called, but the returned value is simply thrown away.

It should look something like this:
 
    double number = convTwo(input1);

or use one of the previously-defined variables, like this:
 
    output1 = convTwo(input1);


Also, the variables output1 and output2 are not initialised or assigned any particular value, hence they may contain any random garbage value. At line 41 the two functions convTwo() and convOne() are then called using this garbage data, so the value returned will also be garbage.

This isn't an error,
1
2
3
4
5
double convOne(double input2) {
    double output1;
    output1 = input2/0.003;
    return output1;
}

but the code can be simplified:
1
2
3
double convOne(double input) {
    return input/0.003;
}

You have a mix of two approaches.

If you don't need the values of output1 and output2 in your main function, you can just do:

 
goldTest(convTwo(input1), convOne(input2));


It makes for more concise code, but may not be as clear.

If you need the values locally,

1
2
3
double output2 = convTwo(input1);
double output1 = convOne(input2);
goldTest(output2, output1);


Jim
Topic archived. No new replies allowed.