Formula call not executing

Write your question here.
Hello,

I am having difficulty with my formula call and not sure where its coming from..

My program runs and reads the following, then after press any key to continue terminates

How many liters did you use?

how many miles did you travel?

process returned -1073741819 (oxC0000005) execution time : 9.125 s
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
#include <iostream>
using namespace std;

double mpg_formula(double miles_travelled, double liters_used);
    //Returns miles per liter used.


int main( )

{
    char answer = 'y';
    double mpg, liters_used, miles_travelled;

    cout << "Solution to Homework 3 Project1"<<endl;

    cout << "How many liters did you use?";
    cin >> liters_used;

    cout << "How many miles did you travel?";
    cin >> miles_travelled;

    mpg = mpg_formula(miles_travelled, liters_used);

    cout << "your miles per gallon was" << endl;
    cout << mpg <<endl;

    cout<< "would you like to try another?";
    cin >> answer;

    return 0;

}

    double mpg_formula(double miles_travelled, double liters_used)
{

    const double Liter_Per_Gallon = 0.264179;
    double Liters_Per_Gallon;

    miles_travelled / (liters_used * Liters_Per_Gallon);

    return (mpg_formula(miles_travelled, liters_used));
}






looking at this quickly. one issue is you can only return one value per function. Unless this is some sort of recursive return that I am not seeing... Maybe I am wrong, but that is what is jumping out at me.

EDIT:

Also I believe it is better practice to declare your constants outside (above) of main. Maybe your function prototype takes care of that, but to me it seems a little confusing.

Its is also not clear (from your comments - to me at least so no offense ) what calculation your performing.
Last edited on
Maybe this is what causes the error:

1
2
3
4
5
6
7
8
9
10
char answer = 'y';
/...
  .../


cout<< "would you like to try another?";
cin >> answer;

// you should use a do - while loop () here,, before return 0;
    return 0;


something like this:
1
2
3
4
5
6
7
8
9
do {
/... your code
  ... /

cout<< "would you like to try another?";
cin >> answer;
} while ( answer == 'y' );

return 0;
Last edited on
Thank you for the quick replies everyone. Not sure what you mean by "can only return one value per function'?

I added the do-while before return 0, and still receiving same answer. Thanks again for the help.
It's a stack overflow. Your mpg_formula() never stops calling itself to find a return value. The calculation is also wrong as far as I can tell.
Last edited on
Sorry this is what I mean, only one value...

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

#include <iostream>
using namespace std;

int myfunction(int foo, int bar );
int main( )

{

    int var1, var2;

    cout << "Enter a number: " << endl;
    cin >> var1;
    cout << "Enter a number: " << endl;
    cin >> var2;

    int result;

    result = myfunction(var1, var2);


    return 0;

}

int myfunction(int foo, int bar ){
    int return_value;

    return_value = foo + bar;

    return return_value;

}

ahh gotcha, great thanks!!
Topic archived. No new replies allowed.