Thread: My code is doing stuff that I didn't tell it to do!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    My code is doing stuff that I didn't tell it to do!

    This is really weird...
    My program is doing something funky...something that is contrary to what my code says (I know you wont believe this, but if you continue to read, I'll explain).

    For example (this is an abbreviated version of my code):

    Code:
    if (!obj.atEndOfFile()) {
        cout<< "Not yet at end of file." <<endl;
        script_found = obj.findScriptTag(storage_str1);
        cout<< "script_found: " << script_found <<endl;
    }
    
    if (script_found == false) {
        cerr<< "Error! File did not contain any Javascripts!\n"
                    "Please pass a file with a javascript in it!\n\n"
                    "Exiting out...";
        return 1;
    }
    Both of these if statements are in a do-while loop, with the code in the same order I posted it. Here is what the relevant output is:

    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    script_found: 0
    Error! File did not contain any Javascripts!
    Please pass a file with a javascript in it!
    The two bolded lines are what I'm talking about. The returned value of the obj.findScriptTag() function is absolutely true (I checked this by outputting "Function is about to return true." right before the "return true" statement inside the findScriptTag() function, and as you can see from my quoted output above, this statement was outputted. Now, if you look closer at the above code, you'll see that in the topmost if statement, which is entered if input_stream is not at end of file, there is only ONE output statement (as the last statement of that block of code) for the value of "script_found" which is the name of a bool variable given the initial value of false when I defined it right before entering the do-while loop, while in the output, on the other hand, it shows it outputted two DIFFERENT values of that variable along with the text "script_found: " in front of it.

    So, to summarize, in short, its outputting the original (correct) value of "script_found", though it is bizarrely before the output statement "Not yet at end of file." which is actually BEFORE it in the code, then its outputting the statement I just mentioned, and then the same statement with "script_found" again, only this time the value of script_found is 0 instead of 1. And it is clear that that's what the end value of "script_found" must be (even though the function of which return value was assigned to that variable): false, seeing as the if statement which checks that value is entered.

    Now can anyone explain why the output is different than what the code is telling it to do?

    BTW, I tried manually deleting the .obj files of the program, so they would be created again, thinking that maybe it was using prior code, but it still does the same thing. If you need any more details, just ask.
    Last edited by Programmer_P; 01-14-2011 at 04:50 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cygwin on win64
    By Vanzemljak in forum Tech Board
    Replies: 3
    Last Post: 01-12-2011, 04:28 PM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM