Calculations Not Coming Out Correct

Mar 12, 2017 at 10:35pm
I'm not asking for anyone to do my home work, I just cant figure out why this isn't working. Once you enter more than one value for beginningReading, endingReading, and gallonsRequired, it only adds up the beginningrReading variable. The other two keep the original value you enter. I coded them the same way. (I apologize for the messy code) Thanks.



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
#include <iostream>
#include <iomanip>
using namespace std;

int main () {
	//Variables
	int tempBeginningReading = 0;
	int tempEndingReading = 0;
	int tempGallonsRequired = 0;
	int beginningReading = 0;
	int endingReading = 0;
	int gallonsRequired = 0;
	char userChoice;
	int numberOfMiles = 0;
	int numberOfGallons = 0;
	int userContinue = 0;
	int num = 0;
	int count = 0;
	float tempPricePerGallon = 0;
	float pricePerGallon = 0;
	float totalPricePerGallon = 0;
	float gasAverage = 0;



	//Start of program
	while (userContinue == 0) {
		cout << "Enter the beginning odometer reading >   ";
		cin >> tempBeginningReading;
		beginningReading = tempBeginningReading + beginningReading;
		cout << "Enter the ending odometer reading >   ";
		cin >> endingReading;
		endingReading = tempEndingReading + endingReading;
		cout << "Enter the gallons required for fill-up >   ";
		cin >> gallonsRequired;
		gallonsRequired = tempGallonsRequired + gallonsRequired;
		cout << "Enter 'y' to enter more readings or 'n' to quit >   ";
		cin >> userChoice;
		
		num++;    //Counter for average

		


		 //For TEST purposes***************
		cout << beginningReading << endl;
		cout << endingReading << endl;
		cout << gallonsRequired << endl;
		system("PAUSE");
		//***********************************
		
		if (userChoice == 'y') {
			
		}
		else if (userChoice == 'n') {
			userContinue++;
		}
	}


	    for (count = 1; count < 2; count++) {
			cout << "Enter price per gallon at Gas Station #" << count << ">   ";
			cin >> tempPricePerGallon;
			pricePerGallon = tempPricePerGallon + pricePerGallon;
		}
		
		
		
	    for (count = 2; count < 3; count++) {
			cout << "Enter price per gallon at Gas Station #" << count << ">   ";
			cin >> tempPricePerGallon;
			pricePerGallon = tempPricePerGallon + pricePerGallon;
		}

		for (count = 3; count < 4; count++)	{
			cout << "Enter price per gallon at Gas Station #" << count << ">   ";
			cin >> tempPricePerGallon;
			pricePerGallon = tempPricePerGallon + pricePerGallon;
		}
		
		count--;
		gasAverage = pricePerGallon / count;
		cout << "Your average gas cost is $" << fixed << setprecision (2) << gasAverage << endl;

	return 0;
}
Mar 12, 2017 at 10:51pm
not entirely sure about it but your for loops seem to be outside the while loop, is that intentional?

also, empty if statement is pointless, just do the else part as a single if statement, and let the else do nothing ride by doing nothing.

Mar 12, 2017 at 10:59pm
Would you mind explaining the logic behind line 30, 33, and 36 to me? I'm a little confused about what you are trying to do.

In line 30:
 
beginningReading = tempBeginningReading + beginningReading;


It looks like you are assigning variable beginningReading which is assigned 0 to start, equal to what is input by the user plus 0?

What is the purpose of tempBeginningReading?

Try beginningReading +=; instead it should add them together if they choose to keep going with adding more readings.

Where is num++ coming into play, I only see it on line 40.


Mar 12, 2017 at 11:04pm
Once you enter more than one value for beginningReading, endingReading, and gallonsRequired, it only adds up the beginningrReading variable. The other two keep the original value you enter. I coded them the same way.


You did not code them the same way.

beginningReading:
1
2
		cin >> tempBeginningReading;
		beginningReading = tempBeginningReading + beginningReading;


endingReading:
1
2
		cin >> endingReading;
		endingReading = tempEndingReading + endingReading;


gallonsRequired:
1
2
		cin >> gallonsRequired;
		gallonsRequired = tempGallonsRequired + gallonsRequired;


Note that the underlining in the above code snippets is not identical.

Last edited on Mar 12, 2017 at 11:05pm
Mar 12, 2017 at 11:06pm

OUTPUT

Enter the beginning odometer reading > 10
Enter the ending odometer reading > 10
Enter the gallons required for fill-up > 10
Enter 'y' to enter more readings or 'n' to quit > y

10
10
10

Press any key to continue . . .

Enter the beginning odometer reading > 10
Enter the ending odometer reading > 10
Enter the gallons required for fill-up > 10
Enter 'y' to enter more readings or 'n' to quit > y

20
10
10

Press any key to continue . . .





I changed the beginningReading = tempBeginningReading + beginningReading;
to
beginningReading += tempBeginningReading and so on.

But the out put is still the same (above).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

	//Start of program
	while (userContinue == 0) {
		cout << "Enter the beginning odometer reading >   ";
		cin >> tempBeginningReading;
		beginningReading += tempBeginningReading;
		cout << "Enter the ending odometer reading >   ";
		cin >> endingReading;
		endingReading += tempEndingReading;
		cout << "Enter the gallons required for fill-up >   ";
		cin >> gallonsRequired;
		gallonsRequired += tempGallonsRequired;
		cout << "Enter 'y' to enter more readings or 'n' to quit >   ";
		cin >> userChoice;
Last edited on Mar 12, 2017 at 11:08pm
Mar 12, 2017 at 11:09pm
I changed the beginningReading = tempBeginningReading + beginningReading;
to
beginningReading += tempBeginningReading and so on.

That wasn't an issue. See my reply above.
Mar 12, 2017 at 11:15pm



Once you enter more than one value for beginningReading, endingReading, and gallonsRequired, it only adds up the beginningrReading variable. The other two keep the original value you enter. I coded them the same way.


You did not code them the same way.

beginningReading:

cin >> tempBeginningReading;
beginningReading = tempBeginningReading + beginningReading;


endingReading:

cin >> endingReading;
endingReading = tempEndingReading + endingReading;


gallonsRequired:

cin >> gallonsRequired;
gallonsRequired = tempGallonsRequired + gallonsRequired;


Note that the underlining in the above code snippets is not identical.



I changed
beginningReading:

cin >> tempBeginningReading;
beginningReading = tempBeginningReading + beginningReading;


endingReading:

cin >> endingReading;
endingReading = tempEndingReading + endingReading;


gallonsRequired:

cin >> gallonsRequired;
gallonsRequired = tempGallonsRequired + gallonsRequired;


to



beginningReading:

cin >> tempBeginningReading;
beginningReading += tempBeginningReading;


endingReading:

cin >> tempEndingReading;
endingReading += tempEndingReading;


gallonsRequired:

cin >> tempGallonsRequired;
gallonsRequired += tempGallonsRequired;



And I'm still having the same issue.
Mar 12, 2017 at 11:18pm
Okay, I'm taking a look at it now. Give me a few minutes.
Mar 12, 2017 at 11:26pm
What if instead of using a while loop, use a for loop and ask the user how many inputs they want to do?

1
2
3
4
5
6
7
8
9
10
11
12
13
int userInput = 0
double sum = 0;

cout << "How many inputs would you like?";
cin >> userInput;
cout << "" << endl;

for (int i = 0; i <= userInput; i++)
     {
                cout << "Enter the beginning odometer reading >   ";
		cin >> beginningReading;
		sum += beginningReading;
     }


Something like this.
Last edited on Mar 12, 2017 at 11:27pm
Mar 12, 2017 at 11:31pm
Unfortunately I have to have that while loop. Its in the requirements.
Mar 12, 2017 at 11:37pm
That's unfortunate.

Do this:
Add doubles beginSum, endSum, and gallonSum.

Use this code:
1
2
3
4
cout << "Enter the beginning odometer reading > ";
cin >> beginningReading;
beginSum += beginningReading;
cout << "" << endl;


This should add all beginningReading inputs into beginSum.
Mar 12, 2017 at 11:41pm
Here's what I got and it seems to work.

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
	while (userContinue == 0)
	{
		cout << "Enter the beginning odometer reading -> ";
		cin >> beginningReading;
		beginSum += beginningReading;
		cout << "" << endl;

		cout << "Enter the ending odometer reading -> ";
		cin >> endingReading;
		endSum += endingReading;
		cout << "" << endl;

		cout << "Enter the gallons required for fill-up -> ";
		cin >> gallonsRequired;
		gallonSum += gallonsRequired;
		cout << "" << endl;

		cout << "Enter 'y' to enter more readings or 'n' to quit >   ";
		cin >> userChoice;

		if (userChoice == 'y' || userChoice == 'Y') 
		{
			userContinue = 0;
		}
		else if (userChoice == 'n' || userChoice == 'N') 
		{
			userContinue++;
		}
	}

	cout << beginSum << endl;
	cout << endSum << endl;
	cout << gallonSum << endl;
Mar 12, 2017 at 11:43pm
Yep that seemed to work for me also. I appreciate it. I guess I just had them a little backwards. Thank you.
Mar 12, 2017 at 11:44pm
No problem, glad I was able to help.
Topic archived. No new replies allowed.