-
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?
-
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.
-
Read the FAQ list -- there's bound to be a wealth of information there.
-
-
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...
-
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
-
>> 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.
-
>> i was wanting to be able to validate user input.
What do you mean by validate user input? Be more specific.
-
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.
-
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.
-
>>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.
-
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.
-
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?
-
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.
-
actually, that would be nice too -- but i meant at the beginning of the code (source)