debugging a mpg calcuator

I have trouble pinpointing the what causes an endless loop.

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
//Ch9AppE07.cpp
//Calculates and displays the miles per gallon
//Created/revised by <Jett Bailes> on <11.20.09>

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
double getMiles();
double getNumGallons();
double calcMpg(double);

int main()
{	
	//declare variables
	double miles       = 0.0;
	double gallons     = 0.0;
	double milesPerGal = 0.0;
	
	//enter input items
	miles = getMiles();
	gallons = getNumGallons();

	//calculate miles per gallon
	milesPerGal = calcMpg(miles);

	//display output item
	cout << fixed << setprecision(1);
	cout << "Miles per gallon: " << milesPerGal << endl;

    return 0;
}   //end of main function

//*****function definitions*****
double getMiles()
{
	double numMiles = 0.0;
	cout << "Enter the number of miles: ";
	cin >> numMiles;
	return numMiles;
}	//end of getMiles function

double getNumGallons()
{
	double numGals = 0.0;
	cout << "Enter number of gallons: ";
	cin >> numGals;
	return numGals;
}	//end of getNumGallons function

double calcMpg(double distance)
{
	double galUsed = 0.0;
	return distance / galUsed;
}	//end of calcMpg function


These endless loops always throw me for a loop. (haha, a little programming humor)
There aren't any loops in that code.
execute it and you'll see what I mean..
57
58
59
60
61
double calcMpg(double distance)
{
	double galUsed = 0.0;
	return distance / galUsed; // division by zero
}

Something's not quite right here...

1
2
3
4
5
6
7

double calcMpg(double distance)
{
	double galUsed = 0.0;
	return distance / galUsed;
}	//end of calcMpg function


Edit: Beaten by a minute!
Last edited on
hmmm. I'm not sure how to go about fixing it. I get the same results after declaring the galUsed variable in the main and passing it to the function. I'm confused.
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
//Ch9AppE07.cpp
//Calculates and displays the miles per gallon
//Created/revised by <Jett Bailes> on <11.20.09>

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
double getMiles();
double getNumGallons();
double calcMpg(double, double);

int main()
{	
	//declare variables
	double miles       = 0.0;
	double gallons     = 0.0;
	double milesPerGal = 0.0;
	double galUsed = 0.0;
	
	//enter input items
	miles = getMiles();
	gallons = getNumGallons();

	//calculate miles per gallon
	milesPerGal = calcMpg(miles, galUsed);

	//display output item
	cout << fixed << setprecision(1);
	cout << "Miles per gallon: " << milesPerGal << endl;

    return 0;
}   //end of main function

//*****function definitions*****
double getMiles()
{
	double numMiles = 0.0;
	cout << "Enter the number of miles: ";
	cin >> numMiles;
	return numMiles;
}	//end of getMiles function

double getNumGallons()
{
	double numGals = 0.0;
	cout << "Enter number of gallons: ";
	cin >> numGals;
	return numGals;
}	//end of getNumGallons function

double calcMpg(double distance, double galUsed)
{
	return distance / galUsed;
}	//end of calcMpg function
Since its value is still zero, the result would be the same.
If you read your code carefully, the variable you should pass is already declared and assigned to a value...
It is still 0.0 when you pass it to the function, since you never actually get input into it.

EDIT: 遅かった・・・。
Last edited on
Topic archived. No new replies allowed.