While != EOF loop question

This is a discussion on While != EOF loop question within the C Programming forums, part of the General Programming Boards category; I've written the following code and I want it to keep looping around collecting input (only asking the question once ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    35

    While != EOF loop question

    I've written the following code and I want it to keep looping around collecting input (only asking the question once at the start) until the user uses EOF (aka ctrl+d) to "say I have no more input". I have a do while loop but obviously the while part is wrong but I'm not sure what needs to be != EOF if not number? Any suggestions?
    Thanks.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       int number;
       char unit[3];
    
       printf("Enter a number between 0-200 followed by its units: ");
       do
       {
          scanf("%d%60s", &number, unit);
          if (strlen(unit) > 2)
          {
              printf("Units are too long\n");
          }
          else if (number < 0 || number > 200)
          {
              printf("Number too large\n");
          }
          else
          {
              printf("Entered number: %d\tEntered unit:%s\n", number, unit);
          }
       } while (number != EOF);
    
       return(0);
    }

  2. #2
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    scanf doesn't output EOF as a token. scanf will only *return* EOF. see the prototype:

    Code:
    int scanf ( const char * format [ , argument , ...] );
    so assign the return value to a variable, and test that against EOF

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by jephthah View Post
    scanf doesn't output EOF as a token. scanf will only *return* EOF. see the prototype:

    Code:
    int scanf ( const char * format [ , argument , ...] );
    so assign the return value to a variable, and test that against EOF
    Thanks, I got it working using something like this

    Code:
    testForEOF = scanf("%d%s", &number, unit);
    while (testFor EOF > 0)
    {
       ...
    }
    I can't seem to find this anywhere, but is EOF always returned as -1? ie could I change the while so that it was this and have it always work or could the return value be -5 for EOF?

    Code:
    while (testForEOF != -1)

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    > scanf("%d%60s", &number, unit);
    Also, allowing input of up to 60 characters into a space where there is only room for two (and a \0) is bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Haven't I seen this before? C while != EOF question? - Yahoo! Answers

    Exact same question and code :P.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by User Name: View Post
    Haven't I seen this before? C while != EOF question? - Yahoo! Answers

    Exact same question and code :P.
    Yeah, and if you saw the response I got it was go ask this question in a real computer forum, which is here, which is why I reposted the same question, pretty much word for word before deleting the other post and it's not so helpful response.

  7. #7
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I can't make you understand, and you reposted before I responded :P. But honestly, Yahoo Answers isn't good for anything computer/programming related, you're better off asking here, unless you need to ask how the power button works, they might be able to answer that . Sorry I couldn't help before.

  8. #8
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    yahoo answers is good for something.

    mostly just lulz.

    but hey. what do you expect?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  2. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-07-2006, 12:43 AM
  3. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  4. Question about restarting a loop
    By librab103 in forum C Programming
    Replies: 10
    Last Post: 07-19-2003, 01:31 PM
  5. Question on for loop
    By anatazi in forum C Programming
    Replies: 4
    Last Post: 05-24-2002, 01:01 PM

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