NO

Author Topic: Stomped...  (Read 2215 times)

Deimler

  • Guest
Stomped...
« 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.

     
Code: [Select]
#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 ->

Code: [Select]
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

  • Guest
Re: Stomped...
« Reply #1 on: February 18, 2011, 12:22:16 AM »
You need to build a loop around your input statements...

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Stomped...
« Reply #2 on: February 18, 2011, 12:23:09 AM »
You have build an endless loop by placing the user interaction outside of it.

Code: [Select]
   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

  • Guest
Re: Stomped...
« Reply #3 on: February 18, 2011, 12:24:45 AM »
hahah... wow, I didn't see that one... Ty It's working now.