Need help with problem

The point of the program here is to convert a list of temperatures Fahrenheit to Celsius, then at the end give the average of both Fahrenheit and Celsius.

The issue i am running into is calculating the average at the end. I am supposed to use a sentinel to signal that i am done entering data.

Can someone help me, and show me what to do here?

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


void main() 
{	

	double fahr = 0, cent = 0, fahrsum = 0, centsum = 0, numtemp = 0;  

	cout.precision(1); 
	cout.setf(ios::fixed); 
	cout.setf(ios::showpoint);
	cout << setw(19) << "Fahrenheit" << setw(17) << "Centigrade" << endl;

	do
	{
		cin >> fahr;
		cent = (5.0 / 9.0) * (fahr - 32.0);
		cout << setw(19) << fahr << setw(17) << cent << endl;
		numtemp++;
		fahrsum = fahr + fahr;
		centsum = cent + cent;

	}	while(fahr != -99);
		cout << setw(15) << "Average    " << fahrsum/numtemp << setw(17) << centsum/numtemp << endl;
	
}

Well first off, I wouldn't use a post-test for this. If I enter your sentinel value right off the bat, it's still gonna get processed.
I don't really agree with your sentinel value, but it'll work for all temperatures on this planet.

What problem are you having with it?
Thanks for your reply.

Are you saying i should just use a while loop?

I am having trouble calculating the average at the end once the sentinal is entered. I cannot figure this thing out.

Thanks!
Last edited on
1
2
fahrsum = fahr + fahr;
centsum = cent + cent;


The sum of all the temperatures so far in the loop should not be the current temperature plus the current temperature.


I believe you meant to write:

1
2
fahrsum += fahr;
centsum += cent;


Last edited on
You are calculating the sums wrong because you aren't keeping track of the sum of it. Whatever the last value You should have:

farhsum = farhsum + farh;
centsum = centsum + cent;

Or this is popular notation that does the same thing:

farhsum += farh;
centsum += cent;

You are also going to run into the problem with when you do input -99 it is going to convert that and calculate that as part of your average. That is what ResidentBiscuit meant by a post-test. I assume you don't want to do that.
Last edited on
Thanks for your help its much appreciated. I have the averages working, but like you said the sentinel is not working properly, and getting calculated into the total.

I tried following some tutorials online about sentinels, but could not figure out what i am doing wrong here.

Can someone help me out?

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


void main() 
{	

	double fahr = 0, cent = 0, fahrsum = 0, centsum = 0, numtemp = 0;  


	cout.precision(1); 
	cout.setf(ios::fixed); 
	cout.setf(ios::showpoint);
	cout << setw(19) << "Fahrenheit" << setw(17) << "Centigrade" << endl;
	while(fahr != -99)
	{
		cin >> fahr;
		cent = (5.0 / 9.0) * (fahr - 32.0);
		numtemp++;
		fahrsum = fahrsum + fahr;
		centsum = centsum + cent;
		cout << setw(19) << fahr << setw(17) << cent << endl;
	}

	cout << setw(15) << "Average    " << fahrsum/numtemp << setw(17) << centsum/numtemp << endl;

}
Topic archived. No new replies allowed.