Like Tree2Likes
  • 2 Post By grumpy

While statement doesnt work if adding in line within

This is a discussion on While statement doesnt work if adding in line within within the C++ Programming forums, part of the General Programming Boards category; The function below has always worked for me until I tried to add in a counter on line 14. As ...

  1. #1
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181

    While statement doesnt work if adding in line within

    The function below has always worked for me until I tried to add in a counter on line 14. As soon as I add in this counter, the while loop is completely neglected. Also mysteriously if I add in line 14 while initialising c to some random value it works again. These seemingly unrelated changes in code make the while loop not execute while the other change makes it work again. Can anyone explain?

    Code:
    struct masternode* foo(ifstream* inputfile) {
        string word;
        int i = 0;
        char c; //defining c to some initialised value makes it work again after adding in line 14.
        struct masternode* root = NULL;
        while (c!=EOF){
            if((c = (*inputfile).get()) != ' ' && c != ':' && c!= ';' && c!='\n')
            {word += c; cout<<"aaa";}
            else {
                root = insert(root, word);
                word.clear();
                cout << "aaaa";
            }
            //i++; 
        }
        return root;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,710
    Well when you don't initialize c you have undefined behavior when you check it for EOF on line 6. Always initialize your variables before you try to use them in a calculation.

    Jim

  3. #3
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by jimblumberg View Post
    Well when you don't initialize c you have undefined behavior when you check it for EOF on line 6. Always initialize your variables before you try to use them in a calculation.

    Jim
    I thought that too. But how come if I keep line 14 out AND keep c uninitialized, it works flawlessly each and every time?
    Or lemme guess, that's one of the mysterious outcomes of undefined behavior? ;-)

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    228
    Quote Originally Posted by Vespasian View Post
    Or lemme guess, that's one of the mysterious outcomes of undefined behavior? ;-)
    Yep. Undefined == anything may happen, including that your program may actually work (some of the time).

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Quote Originally Posted by Vespasian View Post
    Or lemme guess, that's one of the mysterious outcomes of undefined behavior? ;-)
    Possibly. I think it is more likely that your notion of "works flawlessly" is itself flawed.

    If your code is working flawlessly, you're either getting lucky in other ways, or you're not testing well enough. The while loop can't end, since no char can ever be equal to EOF. EOF is implementation defined, but it is an int value that cannot be represented as a char.

    Why don't you pass the input stream by reference, instead of using a pointer? If you must use a pointer (which I doubt) the syntax inputfile->get() is usually preferable to *inputfile.get().
    jimblumberg and stahta01 like this.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Vespasian View Post
    I thought that too. But how come if I keep line 14 out AND keep c uninitialized, it works flawlessly each and every time?
    Or lemme guess, that's one of the mysterious outcomes of undefined behavior? ;-)
    Undefined doesn't mean random. The variable c initially contains garbage (generally the prior value stored at that point on the stack) but that could be highly consistent from execution to execution (or it might not be).

    As to why adding the increment changes anything - the compiler probably optimizes i out of existence without that, and thus changes the stack frame of your method, changing where c is located and changing which garbage value gets pulled into it.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ADC doesnt work
    By kim15 in forum C Programming
    Replies: 7
    Last Post: 05-08-2013, 06:09 AM
  2. Why doesnt this work
    By digdug4life in forum C++ Programming
    Replies: 13
    Last Post: 06-19-2005, 03:22 PM
  3. Why doesnt this work
    By Jotun in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2004, 04:55 PM
  4. How come this doesnt work?
    By correlcj in forum C Programming
    Replies: 2
    Last Post: 07-11-2002, 06:36 PM
  5. why doesnt this work?
    By brad123 in forum C Programming
    Replies: 3
    Last Post: 04-23-2002, 05:26 PM

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