Pelles C forum

C language => Beginner questions => Topic started by: Deimler on February 18, 2011, 12:17:03 AM

Title: Stomped...
Post by: Deimler on February 18, 2011, 12:17:03 AM
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  :)
Title: Re: Stomped...
Post by: CommonTater on February 18, 2011, 12:22:16 AM
You need to build a loop around your input statements...
Title: Re: Stomped...
Post by: Stefan Pendl on February 18, 2011, 12:23:09 AM
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++;
  }
Title: Re: Stomped...
Post by: Deimler on February 18, 2011, 12:24:45 AM
hahah... wow, I didn't see that one... Ty It's working now.