### 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
 ``123456789101112`` ``````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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` `````` 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,
 ``12345`` ``````double convOne(double input2) { double output1; output1 = input2/0.003; return output1; }``````

but the code can be simplified:
 ``123`` ``````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,

 ``123`` ``````double output2 = convTwo(input1); double output1 = convOne(input2); goldTest(output2, output1);``````

Jim
Topic archived. No new replies allowed.