why does this loop work, but the other doesn't?

This is a discussion on why does this loop work, but the other doesn't? within the C++ Programming forums, part of the General Programming Boards category; This loop works just dandily (dandily?) Code: while(fin.good() && type == 'v') { fin >> type; if(type == 'v') fin ...

  1. #1
    Shadow12345
    Guest

    why does this loop work, but the other doesn't?

    This loop works just dandily (dandily?)
    Code:
    while(fin.good() && type == 'v') {
    fin >> type;
    if(type == 'v')
    fin >> x >> y >> z;
    numverts++;
    }
    However its evil twin does not
    Code:
    while(type == 'v') {
    fin >> type;
    if(type == 'v')
    fin >> x >> y >> z;
    numverts++;
    }
    and yes type is initialized to 'v'
    char type = 'v';


    and even if type wasn't initialized it would simply exit the loop

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>why does this loop work, but the other doesn't?
    What "doesn't work" about it? Please be clear with describing your problems.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    i'm guessing it doesn't go in to the loop.. don't know why though. are you using the same files for both loops? and if the one works, why not just use it..?

  4. #4
    Shadow12345
    Guest
    well what i meant was the one that doesn't work never exits the loop, as in for some reason the conditions are never met.

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    What else could type be when read from the file? My guess is that the last thing it puts in type is 'v' at the end of the file, then since theres nothing else to get, it remains 'v' thus the infinite loop. Would also explain why the first one exits, because at the end of the file fin.good() would be false. But its hard to say without seeing the file being read.

  6. #6
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    The first one exits when it reaches the end of the file, the second one doesn't check if it's reached the end of the file ( so if it gets there, and at that point type == 'v' it will loop forever )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 03:45 AM
  2. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  3. Replies: 26
    Last Post: 06-15-2005, 03:38 PM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

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