Hi, I am new to this web site and new to C++ programming. I am still in school and learning everyday. I am not catching on to some things so I was wondering if one can Help me.

My problem I am working right now has to do with averaging temperature in fahrenheit using a loop structure. I cannot get it to calculate no matter what I think it should be. My entries will be 78, 85, 87, 75, 86, 90, 80, 83, 90, 70. Can someone please show me or tell me what I need to do? I'd so very much appreciate it, thanks.


Sounds like you could use an array to store all those numbers, then have an

int sum

that you'd use to hold the cumulative sum of the temperatures as you looped through the array.

Something like this:

int temp[5]; //I forget how to assign values at initialization, if you even do that...
int sum = 0;
int ave = 0; //Calculated average, the output.

for(i = 0; i<5; i++)
     sum = sum + temp[i]; //The new value is the old value plus the number stored in this section of the array.


ave = sum/5;  //calculates average of five temperatures.

Shame on me for not having used arrays in long enough to actually forget the details on 'em, but it's the concept that's important here.

If you have a bunch of related numbers or characters or strings, using an array is a good way to organize them so you can access them iteratively. The for loop (or while loop, if you prefer) allows you to move through the array and use all the values if necessary, as long as you are careful to not exceed the size of the array.

There are ways to make the loop not require a hardcoded array length, as well. It'd be worthwhile for you to learn 'em so you know what you CAN do even if you don't know the exact details... I'm sure that others will disagree with me on that point.

You say you've tried something, so post your best effort so far.

Don't forget this when you do post code.

Every time I do this it only calculate the last given temperature inputed and devides it by 10 (since there is suppose to be 10 given entries inputed). Below is my coding

//declare variables
double averageTemperature
int degrees
string title
//Initialize variables
degrees = 0;
//Display The Program Title
     cout << "          Temperature Counter\n";
     cout << "          -------------------------\n";
//Ask the user for temperature in fahrenheit
     cout << "Enter the degrees in fahrenheit or -99 to quit: ";
     cin >> degrees;
//Repeat the following instructions while degrees is not -99.
while (degrees != -99)
//Increment the count for temperature
      if (degrees > 10)
             (averageTemperature = degrees / 10);
//ask for another temperature to repeat the loop
      cout << "Enter the degrees Fahrenheit or -99 to quit: ";
      cin >> degrees;
//display the report
      cout << endl;
      cout << "The average temperature is : " << averageTemperture << endl;
//display the histogram plot for each category
      cout << endl;
      title = "Degrees";
      plot(title, degrees);
return 0;

For a while loop, the iterator (typically int i) is incremented at the end of the loop. Then, you can use i + 1 as your denominator in calculating the average, and do the cumulative sum of the contents of the array until the final entry (-99) is reached.

> cin >> degrees;
You need to add to a running total of temperatures, then divide that total by 10 to get the average.

> cin >> degrees;
You need to add to a running total of temperatures, then divide that total by 10 to get the average.

Where would that go in the code?

Where would that go in the code?

You could use a for loop to receive input 10 times.

It would go something like

for (int i=0;i<=10;i++)
sumoftemp += inputtemp; //the running total

Assuming you receive 10 values for input, you would then just divide the sum by 10. This would be obviously be after the loop.

> for (int i=0;i<=10;i++)
Which loops 11 times...

Ah you're right. Silly me, that was after the edit as well :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.