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

This is a discussion on My code is doing stuff that I didn't tell it to do! within the C++ Programming forums, part of the General Programming Boards category; This is really weird... My program is doing something funky...something that is contrary to what my code says (I know ...

  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 03:50 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    would the do-while running 1 more time than you expect result in this output? (think >= where you really want >)
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's in a do-while loop. Don't you expect it to happen more than once?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,166
    You have provided "abbreviated code". Usually, when people do that, they leave out code that is relevant to the problem, but they (incorrectly) deem irrelevant. Try posting a small but complete example of code that illustrates your problem.

    In particular, the code you have given does not account for the output "Not yet at end of file" which, apparently, is occurring after the first if() block but before the second. I assume the line "Function is about to return true" line is output by the obj.FindScriptTag() call. In which case, the "Not yet at end of file" line should should be the first line output, not the third.

    The most likely explanation is that script_found at the two points in code are at different scopes (i.e. despite having the same name, they are not the same variable). That would occur, for example, if your function is recursive - there can be more than one instance of the script_found variable in scope, each corresponding to a call of your function.

    The code occurring in a loop might also account for the problem.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by ಠ_ಠ View Post
    would the do-while running 1 more time than you expect result in this output? (think >= where you really want >)
    Quote Originally Posted by tabstop View Post
    It's in a do-while loop. Don't you expect it to happen more than once?
    I knew that question would be asked.
    And the answer (to the first poster)...

    No, because those two if statements I posted above are the first statements in said do-while loop, and nothing else happens to "script_found" through the rest of the loop (or for that matter, anywhere else in the code). That's why its so weird.
    I can post the code for the whole loop if you want, but the code's kind of long.

    And the answer (to the second poster)...

    No, because of the reason I just gave to poster #1.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    is there a logical flaw in your findScriptTag() function so that it cannot handle the final tag correctly?

    and yes, please include the rest of the loop
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by grumpy View Post
    You have provided "abbreviated code". Usually, when people do that, they leave out code that is relevant to the problem, but they (incorrectly) deem irrelevant. Try posting a small but complete example of code that illustrates your problem.

    In particular, the code you have given does not account for the output "Not yet at end of file" which, apparently, is occurring after the first if() block but before the second. I assume the line "Function is about to return true" line is output by the obj.FindScriptTag() call. In which case, the "Not yet at end of file" line should should be the first line output, not the third.
    Actually, the bolded statement is not quite true. If you re-look at the code I posted above, you'll see that outputting "Not yet at end of file." is actually the FIRST output line of the first if statement. And, you are correct in that the line "Function is about to return true" is outputted in obj.findScriptTag(). And yes, it should be the first output line, not the third, which is why I said the code is not doing what I told it to do.
    The most likely explanation is that script_found at the two points in code are at different scopes (i.e. despite having the same name, they are not the same variable). That would occur, for example, if your function is recursive - there can be more than one instance of the script_found variable in scope, each corresponding to a call of your function.
    And yet it is not...

    Complete relevant code example:

    Code:
    //.....................
    //obj.findScriptTag() definition:
    bool C_script_operations::findScriptTag(string& storage_str) {
    
        if (!storage_str.empty())
            return false;
    
        string buffer_str;
        string search_str;
    
        do {
           getline(input_stream, buffer_str);
           if (input_stream.good()) {
               cout<< "storage_str is: " << storage_str <<endl;
               search_str = "<script";
               if (containsStr(buffer_str, search_str)) {
                   cout<< "containsStr returned true for script start tag." <<endl;
                   storage_str += buffer_str;
                   storage_str += '\n';
                   search_str = "</script>";
                   do {
                       getline(input_stream, buffer_str);
                       storage_str += buffer_str;
                       storage_str += '\n';
                       if (!input_stream.bad()) {
                           if (containsStr(buffer_str, search_str)) {
                               cout<< "containsStr returned true for script end tag." <<endl;
                               storage_str = stripStr(storage_str, "<script", search_str.c_str());
                               cout<< "storage_str is: " << storage_str <<endl;
                               num_of_js_found++;
                               cout<< "Function is about to return true." <<endl;
                               return true;
                           }
                       }
    
                       else {
                           cout<< "input_stream is bad in inner loop." <<endl;
                           break;
                       }
                   } while (!input_stream.eof());
               }
          }
          else {
              cout<< "input_stream is bad in outer loop." <<endl;
              break;
          }
        } while(!input_stream.eof()); //end of file has not been reached
    
        cout<< "input_stream is about to return false...";
        return false;
    }
    
    //int main() 
    //...do while loop definition:
    string storage_str1 = "";
        string storage_str2 = "";
        int num_of_js_found;
        bool script_found = false; //this is the only place that script_found is declared/defined
        do {
            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;
            }
    
            else {
                bool script_type_Javascript = obj.scriptTypeIsJavascript(storage_str1);
                if (!script_type_Javascript) {
                    cerr<< "Error! The file you passed did not contain any Javascripts!\n"
                           "Please pass a file that contains at least one Javascript!\n\n"
                           "Exiting out...";
                    return 1;
                }
    
                num_of_js_found = obj.getNumOfJSFound();
                output_filepath1 += "ExtractedJS";
                output_filepath2 += "ExtractedJS";
                if (num_of_js_found < 10) {
                    output_filepath1 += '0';
                    output_filepath2 += '0';
                }
                output_filepath1 += num_of_js_found;
                output_filepath1 += ".html";
                output_filepath2 += num_of_js_found;
                output_filepath2 += ".js";
                ofstream output_stream1(output_filepath1.c_str());
                ofstream output_stream2(output_filepath2.c_str());
                output_stream1<< output_stream1, "<-------------------------------------------------------------\n"
                                                 "This file has been generated by ExtractJavascriptFromHtmlPage.\n"
                                                 "Original HTML file this code was generated from: ";
                output_stream1<< output_stream1, input_filepath.c_str();
                output_stream1<< output_stream1, "\nHave a nice day.\n-->\n\n";
                output_stream1<< output_stream1, storage_str1.c_str();
                output_stream1.close();
                storage_str2 = obj.stripStr(storage_str1, "<script", "</script>", C_script_operations::exclusive);
                for (unsigned int i = 0; i < storage_str2.size(); i++) {
                    if (storage_str2.at(i) == '>') {
                        char* p1 = &storage_str2.at(i);
                        char* p2 = &storage_str2.at(storage_str2.size() - 1);
                        storage_str2 = obj.stripStr(storage_str2, p1, p2);
                        storage_str2.erase(storage_str2.begin()); //erase the '>' character of string
                        break;
                    }
                }
                output_stream2<< output_filepath2, "/*************************************************************\n"
                                                   "This file has been generated by ExtractJavascriptFromHtmlPage.\n"
                                                   "Original HTML file this code was generated from: ";
                output_stream2<< output_filepath2, input_filepath.c_str();
                output_stream2<< output_filepath2, "\nHave a nice day.\n"
                                                   "*************************************************************/\n\n";
                output_stream2<< output_stream2, storage_str2;
                output_stream2.close();
            }
        } while (!obj.atEndOfFile());
    Last edited by Programmer_P; 01-14-2011 at 04:10 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Programmer_P View Post
    Actually, the bolded statement is not quite true. If you re-look at the code I posted above, you'll see that outputting "Not yet at end of file." is actually the FIRST output line of the first if statement. And, you are correct in that the line "Function is about to return true" is outputted in obj.findScriptTag(). And yes, it should be the first output line, not the third, which is why I said the code is not doing what I told it to do.
    That was the point. That is the first output line of the loop -- therefore that is where the loop starts. Everything before that comes from a previous iteration of the loop. Don't confuse "the output scrolled off my screen" with "the output starts in the middle of where I expected it to".

  9. #9
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Code:
        if (!storage_str.empty())
            return false;
    I never see you explicitly empty out your storage_string1, maybe your stripString calls are missing something

    actually, it looks like I misunderstood what your problem was, my vote goes with tabstop, it scrolled off the screen
    Last edited by ಠ_ಠ; 01-14-2011 at 04:18 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    That was the point. That is the first output line of the loop -- therefore that is where the loop starts. Everything before that comes from a previous iteration of the loop. Don't confuse "the output scrolled off my screen" with "the output starts in the middle of where I expected it to".
    No, actually it does not. See, the do-while loop inside int main() iterates so long as obj.atEndOfFile() returns false. For each iteration, its going to start outputting with the first output statement at the top of the loop: namely, the "Not yet at end of file." line, which is inside the if statement which checks to see that eof has not yet been reached, which now that I think about it, should have an associated else statement which sets a bool variable which the following if statement will check to ensure that it doesn't falsely annouce there are no Javascripts inside html file which is passed to program.

    What I'm trying to say is...there is absolutely NO other place in the code where "script_found" (the bool which is assigned the return value of obj.findScriptTag()) is referenced. The only place is at the top of the loop, with those two if statements I gave in the first post of this thread.
    Now, look at the code in those if statements again, since it appears you didn't look at it good enough the first time (no offense). As you can see, the first line of the first if statement block is this:

    Code:
    cout<< "Not yet at end of file." <<endl;
    Second line of the same if statement:

    Code:
    script_found = obj.findScriptTag(storage_str1);
    which, like previously mentioned, will produce the output line "Function is about to return true." with that function call (indicating that the function is about to return TRUE, not FALSE, therefore the resulting value of script_found should be true).

    Third and final line of the same if statement:

    Code:
    cout<< "script_found: " << script_found <<endl;
    Now the lines of the second if statement:

    Code:
    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;
    }
    Therefore, what SHOULD be displayed on the screen is this:

    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Since script_found is TRUE, not FALSE, the second if statement should not be entered at all. But this is the actual output:

    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!

    Exiting out...
    Now you should understand better where I'm coming from, and that the result simply just does not match the code.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  11. #11
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by ಠ_ಠ View Post
    I never see you explicitly empty out your storage_string1, maybe your stripString calls are missing something

    actually, it looks like I misunderstood what your problem was, my vote goes with tabstop, it scrolled off the screen
    Good eye. I guess I should have had the function empty out the storage_str instead of returning false if it wasn't empty.

    Fixed that. Now its saying this instead:

    Function is about to return true.
    script_found: 1
    Error! The file you passed did not contain any Javascripts!
    Please pass a file that contains at least one Javascript!

    Exiting out...
    It seems that the problem now is not obj.findScriptTag() any longer, but is instead obj.scriptTypeIsJavascript(). I'm going to go over that function's definition now, and look for logical errors. Thanks.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Programmer_P View Post
    Since script_found is TRUE, not FALSE, the second if statement should not be entered at all. But this is the actual output:

    Quote:
    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!

    Exiting out...
    Now you should understand better where I'm coming from, and that the result simply just does not match the code.
    My point is that my bet is that the output isn't what you have above, but rather:
    Code:
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    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!
    and that the first 25 lines have scrolled off your terminal screen. You have not yet convinced me otherwise.

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by tabstop View Post
    My point is that my bet is that the output isn't what you have above, but rather:
    Code:
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    Function is about to return true.
    script_found: 1
    Not yet at end of file.
    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!
    and that the first 25 lines have scrolled off your terminal screen. You have not yet convinced me otherwise.
    Ah, ok. I see what you were saying now.
    But actually, that wasn't the exact complete output, though you were close. Its this:

    Not yet at end of file.
    storage_str is:
    storage_str is:
    storage_str is:
    storage_str is:
    storage_str is:
    storage_str is:
    storage_str is:
    storage_str is:
    storage_str is:
    storage_str is:
    storage_str is:
    storage_str is:
    storage_str is:
    storage_str is:
    storage_str is:
    containsStr returned true for script start tag.
    containsStr returned false for script end tag.
    Code:
    storage_str is: <script type="text/javascript">
    //<!CDATA[
    var theForm = document.forms['Form'];
    if (!theForm) {
        theForm = document.Form;
    }
    function __doPostBack(eventTarget, eventArgument) {
        if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
            theForm.__EVENTTARGET.value = eventTarget;
            theForm.__EVENTARGUMENT.value = eventArgument;
            theForm.submit();
       }
    }
    //]]>
    </script>
    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!

    Exiting out...
    So you were partly right. I didn't think to expand the terminal window, and look at what was outputted before the contents of storage_str, though I should have. I forgot I had even outputted the contents of storage_str inside obj.findScriptTag().
    So I guess it does make sense.

    Thanks guys.
    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, 03: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, 12:33 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

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