Thread: need help with this endless loop

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    need help with this endless loop

    Hello. I'm new here as much as I'm new to C programming.

    I need help with my homework. The program is supposed to process a collection of daily high temperature, and count and print the number of hot days, pleasant days, and cold days. After that, it needs to find the average temperature of entered values.

    hot days temperature = 85 or higher
    pleasant days temperature = 60-84
    cold days temperature = under 60

    I'm pretty sure my logic is off the whack, so if anyone can give me advice, it will be highly appreciated. It is giving me an endless loop and when I enter any value, the cygwin program just hangs. Thanks!

    Code:
    #include <stdio.h>
    #define QUIT_NUM -99
    
    double temp, average_temp, average_temp_final;
    int count_days, hot_days, pleasant_days, cold_days;
    
    int main(void)
    
    {
    
    printf("Enter the temperature of any selected day.\n");
    printf("Enter value -99 to quit counting\n");
    scanf("%lf", &temp);
    white(temp != QUIT_NUM) 
    { 
    if (temp >= 85)
    ++hot_days;
    else if (temp <= 84 && temp >=60)
    ++pleasant_days;
    else if (temp < 60
    ++cold_days;
    else
    printf("Value is not determinable");
    
    }
    
    average_temp = temp + average_temp;
    count_days = hot_days + pleasant_days + cold_days;
    average_temp_final = average_temp*average_temp/count_days;
    
    printf("There were total of %f, hot_days\n");
    printf("There were total of %f, pleasant_days\n");
    printf("There were total of %f, cold_days\n");
    printf(The average temperature between those days were %d, average_temp\n");
    
    return(0)
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There are typos in that code which would prevent it from compiling entirely. Is it the same as the code you are using?

    Anyway, your scanf line is outside the while loop -- which means you can only enter a value once, and then it just loops around with that.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    try also indent your code better.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Your code is full of typos. Specially in the printf. i dont know how is ur code even compiling without errors.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Quote Originally Posted by MK27 View Post
    There are typos in that code which would prevent it from compiling entirely. Is it the same as the code you are using?

    Anyway, your scanf line is outside the while loop -- which means you can only enter a value once, and then it just loops around with that.
    no i just looked at the cygwin and typed it down because i dont know how to copy it out from cygwin. So that's probably why there are many typos.

    ill try initiating the loop after the scanf statement then.

    thanks for the comment

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fscanf endless loop
    By krum in forum C Programming
    Replies: 3
    Last Post: 12-05-2007, 12:14 AM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM