Need some help with coding

This is a discussion on Need some help with coding within the C Programming forums, part of the General Programming Boards category; x won't be EOF ever. You can't input EOF with scanf() because it's a macro return value, not a usable ...

  1. #16
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    x won't be EOF ever. You can't input EOF with scanf() because it's a macro return value, not a usable character from the stream. I'd do it like this:
    Code:
    // EOF is negative, so the loop stops the right way
    // any failure won't return 1, so the loop stops the right way
    // everything else is 1 and the loop continues
    while ( scanf( "%d", &x ) != 1 ) {
      if ( x > max)
        max = x;
      if (x < min)
        min = x;
      sum += x;
      counter++;
    }

  2. #17
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    noir u posted this code
    Code:
    while ( 1 ) 
    {
      if ( scanf("%d", &x) != 1 ) 
     {
        if ( x > max)
          max = x;
        if (x < min)
          min = x;
        sum += x;
        counter++;
      }//if 
    else 
    {
        break;
     }//else
    }//while
    I dont get it, why is there a 1 inside the while perameter.
    So its saying if the scanf doesnt return a result of 1 (that is it returns eof or something invalid like a char) you do all the if loops in the encased if loop. Doesnt make sense to me.

    Heres what i did to my program, still not working

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
          int x;
          int min = 1000000000;
          int max = -1000000000;
          float sum;
          float avg;
          float counter = 0;
          printf("\nEnter your integers: <EOF> to stop\n");
          while (scanf("%d", &x) != EOF)
          {
                scanf("%d", &x);
                if ( x > max)
                max = x;
                if (x < min)
                min = x;
                if (x != EOF)
                sum += x;
                counter++;
          }//while
          avg = sum / counter;
          printf("Your average is: %f \n Your maximum is: %d \n Your minumum is: %d \n", avg, max, min);
          system("PAUSE");
          return 0;
    }

  3. #18
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    I dont get it, why is there a 1 inside the while perameter.
    1 means true, it loops forever and lets something inside the loop break. That something was the return value of scanf().
    Heres what i did to my program, still not working
    When you call scanf() twice, you try to read two numbers. The way you're working with EOF isn't right. I posted something that works twice. You should try those out before just throwing pieces together than you don't really understand. Then you'll have something that works and you can take your time figuring out why your other stuff didn't.

  4. #19
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    So if i put scanf in the parameters of the while loop i dont need to execute it in the while loop?

    What is "break;"?

    Maybe im not understanding what you guys are trying to tell me because im not asking the right questions. My teacher didnt really go over eof that much in class, he just stated how to execute it in c prompt. In fact some of the students where telling him they couldnt get eof to work, but hes a crazy old guy so....
    Im not sure i understand the while (1) loop you coded so ill try the other one you posted. I dont get that one either but at least i understand the coding in it.

  5. #20
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
          int x;
          int min = 1000000000;
          int max = -1000000000;
          float sum;
          float avg;
          float counter = 0;
          printf("\nEnter your integers: <EOF> to stop\n");
          while ( scanf( "%d", &x ) != 1 )
          {
                if ( x > max)
                max = x;
                if (x < min)
                min = x;
                if (x != EOF)
                sum += x;
                counter++;
          }//while
          avg = sum / counter;
          printf("Your average is: %f \n Your maximum is: %d \n Your minumum is: %d \n", avg, max, min);
          system("PAUSE");
          return 0;
    }
    
    //result
    /*  
    Enter your integers: <EOF> to stop
    13
    Your average is: 1.#INF00
     Your maximum is: -1000000000
     Your minumum is: 1000000000
    Press any key to continue . . .
    */
    Looks like i used your code. What did i do wrong.

  6. #21
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    So if i put scanf in the parameters of the while loop i dont need to execute it in the while loop?
    Exactly.

    What is "break;"?
    It exits from the while() loop, no questions asked. The condition could be true or false. Any code following the break is not executed. http://www.cprogramming.com/tutorial/c/lesson3.html

    Maybe im not understanding what you guys are trying to tell me because im not asking the right questions. My teacher didnt really go over eof that much in class, he just stated how to execute it in c prompt. In fact some of the students where telling him they couldnt get eof to work, but hes a crazy old guy so....
    I'll just say it again.

    For DOS or Windows, type CTRL-Z. Sometimes you have to do this on a blank line; sometimes you have to type enter afterwards (especially if it isn't on a blank line). It depends on the compiler.

    For UNIX or Linux, type CTRL-D. This must be on a blank line and there's no need to press enter afterwards.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Close, but no banana. First of all, you want ==1, not !=1. Secondly:
    Code:
          while ( scanf( "&#37;d", &x ) != 1 )
          {
                if ( x > max)
                max = x;
                if (x < min)
                min = x;
                if (x != EOF)
                sum += x;
                counter++;
          }//while
    You don't need that line anymore. Try leaving it out and it should work (if you change the !=1). (Leave the sum+=x in.)

    See, scanf() returns the number of items successfully read, or EOF at end of file. Since you told it to read one item ("%d"), its return value will be 1, 0, or EOF, the last two of which indicate failure. To loop while it continues succeeding, you want to use while(scanf("%d", &x) == 1).

    It would return 0 if you entered, say, a letter instead of a number.
    Last edited by dwks; 03-28-2007 at 03:38 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #23
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Allright those last two posts helped alot.

    Btw when you talked about eof earlier you didnt mention ctrl z for windows ctrl d for linux you just said ctrl d or z, and you didnt mention some compilers you use eof with the last input integer or eof by itself, that was what was confusing me.
    The while (scanf("&#37;d", x) != 1 ) i got from noir btw, is he trying to do something different or was that a typo?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
          int x;
          int min = 1000000000;
          int max = -1000000000;
          float sum;
          float avg;
          float counter = 0;
          printf("\nEnter your integers: <EOF> to stop\n");
          while ( scanf( "%d", &x ) == 1 )
          {
                if ( x > max)
                max = x;
                if (x < min)
                min = x;
                sum += x;
                counter++;
          }//while
          avg = sum / counter;
          printf("Your average is: %f \n Your maximum is: %d \n Your minumum is: %d \n", avg, max, min);
          system("PAUSE");
          return 0;
    }
    
    //result
    /*  
    
    Enter your integers: <EOF> to stop
    32
    54
    5678
    -8
    2^Z
    ^Z
    
    
    ^Z
    Your average is: 1597691913886918000000000000000000.000000
     Your maximum is: 5678
     Your minumum is: -8
    Press any key to continue . . .
    */
    eof seems to work wierd on my computer, 1st i tried endering it with my integer, then i tried entering it by itself, then i pressed enter a few times then i tried it by itself and it worked, average isnt working, but the min and max are.
    Last edited by Alphawaves; 03-28-2007 at 03:48 PM.

  9. #24
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    I did some playing around, and if you just press ctrl z by itself after youve entered everything it works as intended.

    I also put sum and counter in my printf for the results, entering 13 43 57 75678 and 0 the counter is 0 and sum is 1075052544 So theres something wrong with the coding, im guessing it has to do with them being float types.

  10. #25
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You need to initialized sum to zero.

    Turn on compiler warnings -- they tell you about that sort of thing. In Dev-C++, choose Compiler Options and in the "Pass these parameters to the compiler" add -W -Wall.

    [edit] BTW, the minimum and maximum values an int can hold are in <limits.h>.
    Code:
    #include <limits.h>
    
          int min = INT_MAX;
          int max = INT_MIN;
    [/edit]

    [edit=2] You don't really need avg. You could do this (I wrapped the long lines):
    Code:
          avg = sum / counter;
          printf("Your average is: &#37;f \n Your maximum is: %d \n\
     Your minumum is: %d \n", avg, max, min);
    ->
    Code:
          printf("Your average is: %f \n"
        "Your maximum is: %d \n"
        "Your minumum is: %d \n", sum / counter, max, min);
    That code is valid BTW. You can use it in your program.

    The preprocessor concatenates adjacent string literals. (Whew.) So if you use
    Code:
    printf("hi"     " there");
    it's the same as
    Code:
    printf("hi there");
    [/edit]
    Last edited by dwks; 03-28-2007 at 03:59 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #26
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    That seems to have got it to work, dunno how though cuz counter was messed up as well.

    The average will give me something like 22.00000000 because its a float, is there any way to omit those decimals if theyre just zeros?

    Thanks alot, to everyone. Honestly i think i learned more in here than the 3 hours i spent in class last wednesday. However, is this the way you would have done it? Is my coding generally good i mean, or should i be looking at it different.

    I have a chem lab so ill try to hop back on in a few hours.

    edit: read your edit ^^ ill check it out more thoroughly when i get back, but i think i understand.

  12. #27
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    The average will give me something like 22.00000000 because its a float, is there any way to omit those decimals if theyre just zeros?
    You can limit the output to a given number of digits like so:
    Code:
    printf("&#37;.2f\n", 3.14159);  /* prints 3.14 */
    Eliminating zeros only is harder.

    Thanks alot, to everyone. Honestly i think i learned more in here than the 3 hours i spent in class last wednesday. However, is this the way you would have done it? Is my coding generally good i mean, or should i be looking at it different.
    Besides what I mentioned in my previous post, that's almost exactly how I would have coded it. I'd probably use doubles, but not much different.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #28
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Allright, well i let it be then. Thanks again.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-20-2009, 06:22 PM
  2. Coding Guideline ....!!
    By imfeelingfortun in forum Tech Board
    Replies: 8
    Last Post: 10-08-2006, 08:09 AM
  3. Before Coding
    By cyberCLoWn in forum C++ Programming
    Replies: 16
    Last Post: 12-15-2003, 02:26 AM
  4. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 05:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21