Thread: Same Function diffrent Outcome

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    Same Function diffrent Outcome

    I Have 3 Functions,
    1 Addmember
    2 Deletemember
    3 UpdateFileNum

    now, when a member is added to my file, i want that the function UpdateFileNum reads a number from a file and increases that number and write it to an other file

    when a member is deleted, i want the opposite to happen, the function UpdateFileNum reads a number from a file and decreases that number and write it to an other file

    How can i do this? i do not want to write 2 diffrent functions... ( because i need more things to be done and it would take a lot of time and thinking

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Okay, I may be a total noob here, but nonetheless:

    To use it in a single function, you'd have to do the following:

    Assuming your Addmember function is triggered when you enter a '1' when you run the program, and your Deletemember function is triggered when you enter a '-1' when you run the program, and you exit the program once you have entered a '0':

    You should have the program query you for an entry (obviously), Then continue to an 'if' statement.

    If your entry equals 1, then it retrieves the FileNum from the specified file, adds 1 to it, and deposits it into another target file.
    On the 'else' statement, add an 'if'.
    If your entry equals -1, then it retrieves the FileNum from the specified file, decreases it by 1, and deposits it into another target file.
    on the 'else' of this 'if', the program should terminate.
    On the 'else' of the first 'if', it terminates the program as well.

    I think the main problem you'd have is: Since you read it from one file, and write it to another, the next time you run the program, it will still read the old number, not the new one.

    ...did this help you at all? If not, I'll go delete this post and not bother replying to people before I better grasp the whole thing -_-'.

    Sorry for not adding code, but that just opens up opportunity for me to go wrong :-p.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Pass in an extra parameter that will specify which way you want it to preform. Maybe an enum?
    Code:
    enum enUpdateFile
    {
        eUpdateAdd,
        eUpdateDel
    }
    
    UpdateFileNum(/*your parameters*/, enUpdateFile update);
    Woop?

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    Not that I'd understand what prog-bman is saying, so if he's fixed it all, never mind me, but:

    Is there reasoning behind putting the new value into a different file?
    The 'main problem' I'd pointed out earlier could actually just be avoided altogether (I read fast :-p) by:

    - First opening the file that contains your FileNum number;
    - Reading that number and storing it as an integer called FileNum; (you could have it displayed if you want)
    - Closing the file;
    - Asking for input (to determine to either Add, Delete or Exit)
    - Editing your FileNum by use of one of your functions.
    - Reopening the file, now deleting the contents of it with the use of:
    ofstream a_file ( "test.txt", ios::trunc );
    - Closing the file;
    - Reopening the file again, this time using:
    ofstream a_file ( "test.txt", ios::app ); adding your newly edited FileNum integer to it.

    Wow, this is probably complicated and would take long to execute, wouldn't it?
    But...this is what I could think of using my basic knowledge. It *should* work if you enter the code correctly.

    I couldnt help myself from posting in here...my apologies if I'm plunging the project into chaos...you're free to happily ignore me.

    (p.s. the use of this ofstream I just found on the C programming.com site, lesson 10.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM