Thread: Issues calling a function from within a function.

  1. #1
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605

    Issues calling a function from within a function.

    Currently having some issues with these two functions. Offswitch works as intended when called by itself, but does not function when called from changestatus.

    Code:
    int offswitch(string arg3);
    int changestatus(string arg1,string arg2);
    
    int changestatus(string arg1,string arg2)
    {
        string token;
        int linenumber = 0;
        int verify;
        ifstream check ( "C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg", ios::in);
        while(getline(check,token))
        {
             
             
             linenumber++; //uses loop to add 1 to the variable for every time getline executes before finding it's mark.
        if (token.find(arg1)!= string::npos)
          {
              offswitch(arg2);
              verify = 1;
              linenumber = 0; //reset linenumber variable to zero for reuse
          } //closing if bracket
       
        else
          {
               ofstream choice_one_op ( "C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg", ios::app );
                      choice_one_op<<"g_godMode = 1";
                      choice_one_op<<"\n";
                      choice_one_op.close();
           }
         
          
          
        } //closing while bracket
        
    }
    
    int offswitch(string arg3)
    {
    string line;
    ifstream in("C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg");
    if( !in.is_open())
    {
    cout << "FILECHECK FAILED. CHECK YOUR CRYSIS DIRECTORY.\n";
    system("PAUSE");
    return 0;
    }
    ofstream out("C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy_backup.cfg");
    while( getline(in,line) )
    {
    if(line != arg3)
    out << line << "\n";
    }
    in.close();
    out.close();
    remove("C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg");
    rename("C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy_backup.cfg","C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg");
    }
    Also, the else code seems to always execute, regardless of whether or not changestatus finds the string in the file.

    Would someone mind explaining to me what the issue is here? I've been fiddling with it for a couple of hours now to no avail.

    I appreciate any advice.
    Last edited by civix; 11-05-2010 at 08:53 PM.
    .

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    I don't see where changestatus is called from offswitch.

    As for the else code: the while loop is going through the entire input file, so unless the search string occurs only in the last line of the file, isn't there going to be a subsequent line that triggers the else code?

  3. #3
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Offswitch is being called from changestatus. Fixed the OP.

    Would moving the entire if statement outside the loop help, or would it just complicate things?
    .

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    If you are supposed to find the search string at most once in the check file, you probably want to close the file before calling offswitch and break out of the while loop after returning from offswitch.

    And I don't see any reason for the assignments to verify and linenumber since those are local vars that will disappear when changestatus returns anyway.

  5. #5
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Okay, now I've got offswitch working properly when called from changestatus, and added the filestream to add the string to the file should it not be present, but the else code is now not executing. Should I be breaking out of the loop after the filestream completes?

    Code:
    int changestatus(string arg1,string arg2)
    {
        string token;
        int linenumber = 0;
        int verify;
        ifstream check ( "C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg", ios::in);
        while(getline(check,token))
        {
             
             
             linenumber++; //uses loop to add 1 to the variable for every time getline executes before finding it's mark.
        if (token.find(arg1)!= string::npos)
          {  
              
              check.close();
              offswitch(arg2);
              break;
              verify = 1;
              linenumber = 0; //reset linenumber variable to zero for reuse
              
              
              
              
          } //closing if bracket
          else
          {
              
              ofstream choice_one_op ( "C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg", ios::app );
                      choice_one_op<<"g_godMode = 1";
                      choice_one_op<<"\n";
                      choice_one_op.close();
                      break;
                      }
              
         
          
          
        } //closing while bracket
    EDIT: Breaking out of the loop after the filestream finishes now, and the else code is executing. However, I'm back to the problem of having the else code execute no matter what.
    Last edited by civix; 11-05-2010 at 10:37 PM.
    .

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    See comments below:

    Code:
        if (token.find(arg1)!= string::npos) {
              check.close();
              offswitch(arg2);
              break;
            // The following 2 lines will never be executed, but they're useless anyway.
              verify = 1;
              linenumber = 0; //reset linenumber variable to zero for reuse
        } //closing if bracket
        else {
              break;   // Delete this.  Because of this line, the next line will never be executed.
              ofstream choice_one_op
                     ( "C:\\Program Files(x86)\\ElectronicArts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg", ios::app );
                      choice_one_op<<"g_godMode = 1";
                      choice_one_op<<"\n";
                      choice_one_op.close();
    }

  7. #7
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Yes. I moved the break to after the filestream closes so as to allow the code to execute, but stop it before it executes an infinite number of times.

    The only remaining issue is that the else code ALWAYS executes.

    Example. I run the program with the string in the file. This function should remove it, but instead, the else code executes and I just wind up with the same string in the file twice. When the else code is here, it's as if the if code does not execute.
    .

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by civix View Post
    EDIT: ... I'm back to the problem of having the else code execute no matter what.
    I find that very hard to believe.

    Try this:
    Code:
        if (token.find(arg1)!= string::npos) {
              check.close();
              offswitch(arg2);
              break;
        } //closing if bracket
        else {
              ofstream choice_one_op
                     ( "C:\\Program Files(x86)\\ElectronicArts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg", ios::app );
                      choice_one_op<<"linenumber = "  << linenumber << "\n";
                      choice_one_op<<"g_godMode = 1";
                      choice_one_op<<"\n";
                      choice_one_op.close();
        }
    See what you end up with in diff_easy.cfg.

  9. #9
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    That code appends:

    linenumber = 1
    g_godmode = 1

    in diff_easy.cfg.

    That happened with g_godmode already in the file so as to be removed.

    If you think it would help, I could zip up a directory, source, and executable so you can see what it's doing first-hand. This has me so confused.
    .

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    That would be difficult since I'm not running Windows.

    I assume when you wrote "g_godmode = 1" that was a typo.

    I'm wondering though, why the line "g_godMode = 1" is hard-coded, but the search string arg1 is a variable. Is the argument being sent to changestatus EXACTLY the same as the hard-coded line? Is it possible that there are any blank spaces at the end of the line either in the argument or in the file, or any other non-printing characters, such as 'CR'?

    By the way, was "g_godMode = 1" on the first line of the file before you ran that test?
    Last edited by R.Stiltskin; 11-05-2010 at 11:20 PM.

  11. #11
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    I'll change the output filestream to accept the argument from changestatus.

    EDIT: Did that, nothing changed.
    Last edited by civix; 11-05-2010 at 11:23 PM.
    .

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    I think this is what you want:

    Code:
    int changestatus(string arg1,string arg2)
    {
        string token;
        int linenumber = 0;
        int verify = 0;
        ifstream check ( "C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg", ios::in);
        while(getline(check,token)
        {
            linenumber++; //uses loop to add 1 to the variable for every time getline executes before finding it's mark.
            if (token.find(arg1)!= string::npos)
            {
                check.close();
                offswitch(arg2);
                verify = 1;
                break;
            } //closing if bracket
        }
        if( !verify ) // eof reached and string not found
        {
            ofstream choice_one_op ( "C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg", ios::app );
            choice_one_op<<"g_godMode = 1";
            choice_one_op<<"\n";
            choice_one_op.close();
        }
    }
    You were going into that else code every time you read a line that wasn't the search string.
    Can't believe I didn't see that earlier.

  13. #13
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Modified your code a little bit to put an increment on verify and told the ofstream to accept an argument from checkstatus, that way I can use the function for multiple items, as I'm sure it's pretty obvious exactly what I'm doing here. Everything is now working as intended.

    I really appreciate your help on this. Thanks alot, man.
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM