Thread: recalling data

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    33

    recalling data

    Ok, i was wondering if there is a site i can go to so i can learn to recall data from a text file or store data within the program.

    example: Enter Name: Bob
    "stores name"
    ends program

    Restart program
    Last User: Bob
    Change Name: Joe
    "stores name"
    ends program

    ect...

    any help with this?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't know of a site, but all you need to do is use fstreams (which are a lot like cout and cin). Start with something simple like just the name. Use an ofstream to write out the name to the file (truncate the file, do not append). Once that is working, add code to open the file with an ifstream and read from it (just like you would with cin). Save and display the name that was in the file.

    Once that is working, combine the two so that you try to open the file. If the file doesn't exist, then the program is being run for the first time, so you have an empty name, otherwise you get the current name from the file. Then display the name and ask for a new name, and save the new name out to the file. Once this is working, you can try adding more data and figuring out how to read and write it in the same format every time.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Read the FAQ list -- there's bound to be a wealth of information there.

  4. #4
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    here's a good site. Hope it helps
    http://www.cplusplus.com/doc/tutorial/tut6-1.html
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    well, i understand I/O to a point, i was wanting to be able to validate user input. I checked out the validate user input in the FAQ, but it was not what i was looking for. Thats what i am really wanting...

  6. #6
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    here is a thread, that i started, almost with the same problem (i think it was almost the same problem). have a look at that.

    http://cboard.cprogramming.com/showthread.php?t=68358
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> here's a good site. Hope it helps
    http://www.cplusplus.com/doc/tutorial/tut6-1.html

    At first glance that doesn't look like a good site to me.

    It uses non-standard code. You might be able to just change the headers and get it to work, but you might not - the standard fstream classes are different than the old versions used by that tutorial.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i was wanting to be able to validate user input.

    What do you mean by validate user input? Be more specific.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    ok -- like, when you enter the "password", it says something like Yay! and then it asks you if you want to change your "password", if you choose yes, then it says enter new "password" and when you change it, it saves it and then when you re-run the program, the "password" is equal to what you changed it to be.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Assuming you are using C++ strings, simply save the password from the file into a string variable, then read in the user's password from cin into a different string variable. Compare the two strings with == or != and act appropriately depending on whether they are equal or not.

    If they change their password, write the new password out to the file as I mentioned in my first post.

  11. #11
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    >>At first glance that doesn't look like a good site to me.

    alright.

    helped me out though. i sorta forgot i didn't use standard; i don't standerdizationalmize, right now.
    Clear the mines from our Shazbot!
    Get the enemy Shazbot!

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's fine. Many people don't know the difference, then wonder why the code doesn't work. IMO it would be better to not point them to anything than to show them a tutorial with incorrect and questionable code. At the very least people should be warned that the code they are looking at is not so good.

  13. #13
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    ok, but how would i verify the data in the text file... i can get it to store the password, it asks you to reenter and it verifies it, but i am getting confused in how to implament that same code into the beginning:
    Code:
    string pass2;
    	cout << "Please enter it again: ";
    	cin >> pass2;
    	if ( pass != pass2 ) {
    		system("cls");
    		cout << "Password verification is incorrect!" << endl;
    		system("pause");
    	}
    	else {
    		system("cls");
    		cout << "Password verification is correct!" << endl;
    		system("pause");
    	}
    would this same concept be used in the beginning of the program too?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    When you say the beginning, do you mean the first time the user runs the program and there is no password? If that's what you are referring to, then I would check to see if the stored password is empty (the file did not exist) and then skip the password confirmation. You could then go straight to the set password part, which would save the new password or the changed password the same way.

    If that's not what you meant, then a little more information about the structure of the program might help.

  15. #15
    Registered User
    Join Date
    Jul 2005
    Posts
    33
    actually, that would be nice too -- but i meant at the beginning of the code (source)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM