question about while loops

This is a discussion on question about while loops within the C++ Programming forums, part of the General Programming Boards category; Why is this an acceptable condition for a while loop: Code: while (scanf("%d", &number) !=EOF) But this isn't: Code: while ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    question about while loops

    Why is this an acceptable condition for a while loop:
    Code:
    while (scanf("%d", &number) !=EOF)
    But this isn't:
    Code:
    while ((cin >> number) !=EOF)

  2. #2
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    My guess is because scanF is a function, and cin is an object... I don't know...
    If you ever need a hug, just ask.

  3. #3
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    cin >> number will not return teh EOF on failure!!!1
    .sect signature

  4. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    So what would be an acceptable condition similar to the one above using cin?

    I'm pretty sure there's a way to use cin and EOF in a while loop statement?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'm pretty sure there's a way to use cin and EOF in a while loop statement?
    Yes, there is:
    Code:
    while ( cin>> number ) {
      // Loop until cin fails in any way
    }
    
    if ( cin.fail() )
      // Error
    
    if ( cin.eof() )
      // End of file
    
    etc...
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. noob question re: switch case & loops
    By scooglygoogly in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2008, 11:08 AM
  2. question on GCD and for loops
    By dals2002 in forum C Programming
    Replies: 9
    Last Post: 03-15-2008, 01:59 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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