Thread: need help with a for loop

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    78

    need help with a for loop

    What I am trying to do is select the number of days that I want to add temperatures. So if I type in 6, then I want to be able to type in 6 temperatures individually, one after the other. However, once I type in the first one and hit enter, the for loops skips through the remaining 5. Once I am able to type in the temperatures for the amount of days I select, I need to calculate the avg temps for those days and display it. Here is my code. Any help would be greatly appreciated. Tommy
    [CODE]
    #include <stdio.h>
    #include <conio.h>

    int main()
    {

    int count, days;
    float temp, total, avg;


    total=0.0;

    printf("\nPlease enter the number of days temperatures you wish to enter: ");
    scanf("%d", &days);

    for (count=0;count<days;++count)
    {
    printf("\nTemperature is: ");
    scanf("%.2f", &temp);

    total=total+temp;

    }

    avg=total/days;
    printf("\n\nThe average temperature was: %5.2f\n", avg);

    getch();
    return 0;


    }






    Here is the results so far:

    Please enter the number of days temperatures you wish to enter: 6

    Temperature is: 54.3

    Temperature is:
    Temperature is:
    Temperature is:
    Temperature is:
    Temperature is:

    The average temperature was: -107374176.00

    I now only need to add the temps. Do I need another printf or scanf statement? Thanks again for your help.....

    CODE]

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf("%.2f", &temp);
    Rule 1: scanf is not printf. The two are just similar enough to confuse you. Try this instead:
    Code:
    scanf("%f", &temp);
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    change your for statement to read

    for(count=0;count<days;count++)
    beacuse I think the ++ before count is causing a problem

    and when you want to clear the keyboard buffer, if I understand your code correctly, you can write another fuction that uses getchar

    i.e
    Code:
    void clear(){
     while(getchar()!='\n');
    }
    or you can use fflush and put it after your scanf in your for loop.
    Also if you want to add all the temps together you can do that in your for loop for example you could do this after your scanf

    temp+=temp

    and it should work other than that your code looks great

    Hope that helps you and good luck
    Last edited by jccharl; 03-10-2004 at 11:16 PM.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    78

    Thanks jccharl

    Thanks for your help. You guys (and gals) really know your stuff. I hope to be just as good some day. After reviewing what you said and applying the different suggestions to my code, the only thing that was wrong with my code was:
    Code:
    scanf("%.1f", &temp);
    ....Just take out the .1 and just have %f instead works great.

    Can you explain why the the %1.f was causing me the problem? The ++count works fine and I ran the code using ++count and ran it using the count++ and they both ran identically. I was just curious as to why a .1f can make a HUGE impact on my code!! I understand that scanf isnt the same thing as printf and I was using scanf to to accept user input. Anyways, you fixed my dilema. I really appreciate it. Have a great day!! Tom

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    78

    Thanks jccharl

    Thanks for your help. You guys (and gals) really know your stuff. I hope to be just as good some day. After reviewing what you said and applying the different suggestions to my code, the only thing that was wrong with my code was:
    Code:
    scanf("%.1f", &temp);
    ....Just take out the .1 and just have %f instead works great. The program adds the temps and creates the averages fine. I didnt need the fflush as well, even though it probably wouldn't hurt :-)

    Can you explain why the the %1.f was causing me the problem? The ++count works fine and I ran the code using ++count and ran it using the count++ and they both ran identically. I was just curious as to why a .1f can make a HUGE impact on my code!! I understand that scanf isnt the same thing as printf and I was using scanf to to accept user input. Anyways, you fixed my dilema. I really appreciate it. Have a great day!! Tom

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    78

    Thanks jccharl

    Thanks for your help. You guys (and gals) really know your stuff. I hope to be just as good some day. After reviewing what you said and applying the different suggestions to my code, the only thing that was wrong with my code was:
    Code:
    scanf("%.1f", &temp);
    ....Just take out the .1 and just have %f instead works great. The program adds the temps and creates the averages fine. I didnt need the fflush as well, even though it probably wouldn't hurt :-)

    Can you explain why the the %1.f was causing me the problem? The ++count works fine and I ran the code using ++count and ran it using the count++ and they both ran identically. I was just curious as to why a .1f can make a HUGE impact on my code!! I understand that scanf isnt the same thing as printf and I was using scanf to to accept user input. Anyways, you fixed my dilema. I really appreciate it. Have a great day!! Tom

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >beacuse I think the ++ before count is causing a problem
    This possibility only exists when the increment (or decrement) is embedded in a larger expression. Whe used alone, ++i and i++ are equivalent.
    Code:
    void clear(){
     while(getchar()!='\n');
    }
    Or more generally:
    Code:
    void clear ( FILE *in )
    {
      int c;
    
      while ( ( c = fgetc ( in ) ) != EOF && c != '\n' )
        ;
    }
    Of course, if you find yourself doing this often it's a sign that your input algorithms are possibly flawed.

    >or you can use fflush
    Or not. Using fflush on input streams is undefined and quite wrong. fflush ( stdin ) is one of the constructs that the majority of this forum's members can spot immediately and give a detailed explanation of why it's wrong.

    >Can you explain why the the %1.f was causing me the problem?
    The %f flag for scanf doesn't support a precision specification. By trying to use one the flag is malformed and results in undefined behavior, which on your system results in scanf failing with odd results.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed