News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

Stomped...

Started by Deimler, February 18, 2011, 12:17:03 AM

Previous topic - Next topic

Deimler

Hi, the program that I'm trying to run is simple, but im still having a bit of trouble figuring out the while loop function.

The program runs like so:
      Find out the average temperature from an input, using while loop of course.

example: 98, 87, 50, 90, 87, 50

average = 77

77 = Pleasant Day..
-----------------------------------------------------------------------
The problem with the program is that it only stops at the first input... and does nothing more.

     
#include <stdio.h>
#include <math.h>

int
main(void)
{
int temp; // temperature of the day
int x = -1;
int total = 0;



printf("Input the temperature -> \n    Input  999  to stop -> \n");
scanf("%d", &temp);

while (temp != 999){
total = temp + total; // adds up the temperature
x++;
}


total = total / x; //solves the average  devides the total by the number of temperature

if (total >= 85)
printf("Hot Day\n");
else if (total >= 60 && total <= 84)
printf("Pleasant Day\n ");
else
printf(" Cold Day\n");

return 0;
}





Also I've realized that the problem might be in this line of code ->

while (temp != 999){
total = temp + total; // adds up the temperature
[u]x++;[/u]
}   


Since the x++ doesn't have anything to do with it, I wanted to know how I could make it interact with the statement.
Also thank you in advance  :)

CommonTater

You need to build a loop around your input statements...

Stefan Pendl

You have build an endless loop by placing the user interaction outside of it.


   while (temp != 999){
     printf("Input the temperature -> \n    Input  999  to stop -> \n");
     scanf("%d", &temp);

     if  (temp == 999) break;

    total = temp + total; // adds up the temperature
    x++;
  }
---
Stefan

Proud member of the UltraDefrag Development Team

Deimler

hahah... wow, I didn't see that one... Ty It's working now.