Loop Question

This program calculates average miles per gallon. Calculates average gas price and calculates the cost for a car to go one mile. Everything is executing just fine, the only problem I am having is say I enter just 1 beginning - end odometer reading and one gas reading, if I stop the loop there, it still asks for the price paid at gas station 2 and 3. How do I make it to where say the person enters 10 different gas stops, I need to be able to enter 10 different prices, same with just one gas stop.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  #include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
	double startingMiles, endMiles, totalMiles, averageMiles, ppg, totalPPG, countPPG, averagePPG, perMile, gallons, totalGallons;
	startingMiles = 0;
	endMiles = 0;
	totalMiles = 0;
	totalGallons =0.0;
	char more;
	more = 'y';
	while (more == 'y' || more == 'Y') // allow both Y and y answer
	{
		cout << "\n Enter the beginning odometer reading >" ; // Starting Reading
		cin >> startingMiles;
	

		while (startingMiles < endMiles)
		{
			cout << "\n Input error! end odometer reading must be greater than the last reading! "<<endl; // If Start is Greater than End, Output Error
			cout <<"Enter the ending odometer reading > ";
			cin >> endMiles;
		}


		cout << "\n Enter the ending odmeter reading >"; //End Reading
		cin >> endMiles;
		while (endMiles <= startingMiles)
		{
			cout << "\n Input error! end odometer reading must be greater than starting reading! "<<endl; // If Start is Greater than End, Output Error
			cout <<"Enter the ending odometer reading > ";
			cin >> endMiles;
		}
		cout << "\n Enter the gallons required for your fill-up > " ; // Gallons in Fill up
		cin >> gallons;
		totalGallons +=gallons; //accumulate running gallons total

		while (gallons <= 0)
		{
			cout << "\n Input error! Please Re-Enter > ";
			cin >> gallons;
		}
		totalMiles += endMiles - startingMiles; // calculate total miles driven

		cout <<endl;


		cout << "Enter y to enter more readings or n to quit> "; // Does customer have more inputs
		cin >> more;

			if (more != 'Y' && more != 'y' && more != 'N' && more != 'n')  // Validate input allowing only YyNn answers
		{
			cout <<"Invalid entry. Please enter Y or N";
				cin >> more;

		}

	
	}
	cout <<endl; 

	averageMiles = totalMiles / totalGallons; // Calculate the averageMiles

	totalPPG = 0;
	for (countPPG = 0; countPPG < 3; countPPG++)
	{
		cout << "\n Enter the price per gallon at station #" << countPPG + 1 << " $" ;
		cin >> ppg;
		while (ppg <= 0)
		{
			cout << "\n That is not a valid price. Please Enter Again $"<<endl;
			cin >> ppg;
		}
		totalPPG += ppg;
	}
	averagePPG = totalPPG / countPPG; // calculate average miles per gallon
	
	perMile = averagePPG / averageMiles;

	cout <<endl;
	cout << setprecision(1) << fixed;
	cout << "Your cars average miles-per-gallon is> " << averageMiles <<endl;
	cout << setprecision(2) << fixed;
	cout << "You bought gas at an average $" << averagePPG << " per gallon" <<endl;
	cout << "The cost for your car to 1 mile is $" << perMile <<endl;



	if (averageMiles >= 0 && averageMiles <= 15) // determine effeciency of vehicle
	{
		cout << "Your car is Very Inefficient " <<endl;
	}
	else if (averageMiles >= 16 && averageMiles <= 30)
	{
		cout << "Your car is Reasonably Effecient " <<endl;
	}
	else if (averageMiles >= 31)
	{
		cout << "Your car is Very Efficient " <<endl;
	}

	system("pause");
	return(0);
}
Wrong Info.
Last edited on
@chicofeo,

She does not want us asking the number of gas stations, only relying on the input y/n loop. If the user stops after 1 input, the asking for amount paid for gas would only be one time. If the user inputs 3 different fill ups, then the asking amount paid would be 3 times.
Why do you need a separate loop to enter the PPG? Since you want to enter the PPG once each time the user enters 'y', why not simply do it inside that loop?

EDIT: If there's some reason you need a separate loop, then the reason for your problem should be obvious - you've hardcoded that loop to iterate 3 times. You should change it to iterate the same number of times as the user chose to fill up.
Last edited on
There are couple of things about the code i have questions. However, my focus is to help you in the current situation. The code below, I added it some stuff that I marked as /************/ I add it an accumulator to be used depending on the user answer in the y/n input.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    double startingMiles, endMiles, totalMiles, averageMiles, ppg, totalPPG, countPPG, averagePPG, perMile, gallons, totalGallons;
    startingMiles = 0;
    endMiles = 0;
    totalMiles = 0;
    totalGallons = 0.0;
    /**********************/
    int numGasStations = 0; // Declare and Initialize the variable accumulator.
    /**********************/
    char more;
    more = 'y';
    while (more == 'y' || more == 'Y') // allow both Y and y answer
    {
	   cout << "\n Enter the beginning odometer reading >"; // Starting Reading
	   cin >> startingMiles;


	   while (startingMiles < endMiles)
	   {
		  cout << "\n Input error! end odometer reading must be greater than the last reading! " << endl; // If Start is Greater than End, Output Error
		  cout << "Enter the ending odometer reading > ";
		  cin >> endMiles;
	   }


	   cout << "\n Enter the ending odmeter reading >"; //End Reading
	   cin >> endMiles;
	   while (endMiles <= startingMiles)
	   {
		  cout << "\n Input error! end odometer reading must be greater than starting reading! " << endl; // If Start is Greater than End, Output Error
		  cout << "Enter the ending odometer reading > ";
		  cin >> endMiles;
	   }
	   cout << "\n Enter the gallons required for your fill-up > "; // Gallons in Fill up
	   cin >> gallons;
	   totalGallons += gallons; //accumulate running gallons total

	   while (gallons <= 0)
	   {
		  cout << "\n Input error! Please Re-Enter > ";
		  cin >> gallons;
	   }
	   totalMiles += endMiles - startingMiles; // calculate total miles driven

	   cout << endl;


	   cout << "Enter y to enter more readings or n to quit> "; // Does customer have more inputs
	   cin >> more;

	   /***********************************/
	   if (more == 'Y' || more == 'y') // If yes, then the accumulator will add 1 gas stop
		  numGasStations++;

	   else if (more == 'N' || more == 'n') // If no, then there will be AT LEAST 1 gas stop.
		  numGasStations = 1;
	   /***********************************/

	   if (more != 'Y' && more != 'y' && more != 'N' && more != 'n')  // Validate input allowing only YyNn answers
	   {
		  cout << "Invalid entry. Please enter Y or N";
		  cin >> more;

	   }


    }
    cout << endl;

    averageMiles = totalMiles / totalGallons; // Calculate the averageMiles

    totalPPG = 0;

     /*************************************/
    // Change it from 3 to numGasStations to loop as many as required, pending the answer from the user
    // To add more data.
    for (countPPG = 0; countPPG < numGasStations; countPPG++) //Run the loop up to the number of gas stations.
    {
	   cout << "\n Enter the price per gallon at station #" << countPPG + 1 << " $";
	   cin >> ppg;
	   while (ppg <= 0)
	   {
		  cout << "\n That is not a valid price. Please Enter Again $" << endl;
		  cin >> ppg;
	   }
	   totalPPG += ppg;
    }
    averagePPG = totalPPG / countPPG; // calculate average miles per gallon

    perMile = averagePPG / averageMiles;

    cout << endl;
    cout << setprecision(1) << fixed;
    cout << "Your cars average miles-per-gallon is> " << averageMiles << endl;
    cout << setprecision(2) << fixed;
    cout << "You bought gas at an average $" << averagePPG << " per gallon" << endl;
    cout << "The cost for your car to 1 mile is $" << perMile << endl;



    if (averageMiles >= 0 && averageMiles <= 15) // determine effeciency of vehicle
    {
	   cout << "Your car is Very Inefficient " << endl;
    }
    else if (averageMiles >= 16 && averageMiles <= 30)
    {
	   cout << "Your car is Reasonably Effecient " << endl;
    }
    else if (averageMiles >= 31)
    {
	   cout << "Your car is Very Efficient " << endl;
    }

    system("pause");
    return(0);
}
Lines 21-26: Your editing doesn't seem right. Line 18, you're checking the starting mileage. Line 24-25: You ask for the ending miles. If the test at line 21 fails, you will never input the correct starting mileage.

Lines 96-100: What happens if averageMiles is 30.5?
Topic archived. No new replies allowed.