Save user data

Hi i finally got my variables to work I'm in beginning stages trying to make a game need to know how to save ID AND PASSWORD ENTERED BYE THE USER and retrieve it for later so they can log on its the basic beginning to any game here my code so far AND is c++ the best program trying to save dat




#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>


using namespace std;
int Login;
string password2;

using std::cerr;
using std::endl;

using std::ofstream;









std::string getStringOfMinLength(const std::string& prompt, unsigned minLength)
{





std::cout << prompt << '\n' << "(min length = " << minLength << ")\n" ;

std::string str ;
while ( std::cin >> str && str.length() < minLength )
std::cout << "Not long enough! (min length = " << minLength << ")\n" ;

return str;
}

int UserSetup()

{












std::string userName = getStringOfMinLength("Please enter a user ID", 6) ;
repeat2:
std::string password1 = getStringOfMinLength("Please enter a password", 7) ;


cout << "Please RENTER your password\n";
cin >> password2;

if (password1 != password2)
{
cout << "You have ReEnter your password incorrectly \n";
goto repeat2;
}

if (password1 == password2)
{

cout << "You Now registered with treasure Island\n";
cout << " User name: " << userName << " Password: " << password1 << '\n';
}
}

int Welcome()

{
cout << "Welcome to Adam's Quiz Game\n";
cout << "Login Enter 1 " "Create New Account Enter 2 \n" "Press 3 to Quit\n";

cin >> Login;

if (Login ==2)
{
UserSetup();
}

if (Login ==1)
{
cout << "bye\n";
}

}






int main()

{

Welcome();
ofstream outdata;
outdata.open("User");



system("PAUSE");
return 0;


}
you look to be doing fine. what exactly is the problem ?
Really Ok So Filestream out source will save the file right and then how do i call data back to the program
you don't have to output information which is not required. like User name: & password:

just write the real name and password. plain text will do at this stage.

the way you are writing, the same way you are going to read.. so each record will start with a new line. once, you write the name and password, write a new line.
when reading, you can use, ifstream and getline(). keep reading lines in std::string and extract name and password from each line.. you can do whatever you want..

something like:
1
2
3
4
5
6
7
8
ifstream ifs("file path");
std::string str;
while(ifs.good())
{
getline(ifs, str);
//do whatever you want to with str;

}
Last edited on
Ok so my question now once user registers and account now I want him to go back and Login how set my program up to do something like that
ask the user to login. ask for a username and password. See, if that entry exists in the file. If it does, you let the user login and show information related to that user.

this is how a beginner would do. :-) To make it secure, you would like to save hashes of username and password rather than plain text.
Last edited on
I once did something similar to this. I had a register and login option. When registering I had them choose an username and that would be the name of the .txt file, and then the password would be stored there. (I used c_str() ). As already said, main problem is that someone could just go take a look at those files and see the password. I haven't done hashing but know how some of the simpler algorithms work from taking Security+. If you want it set up so that they can only have one account then have it save a bool i.e
 
bool hasRegistered = true

1
2
3
4
5
6
7
8
9
10
11
int main()
{
if(hasRegistered = true)
{
// login code here
}
else
{
// register code here
}
}

That would be for if the file already exists and you added an option to reset everything to create a new account. For the first time you would just check if the file is open.
1
2
3
4
5
6
7
8
9
ifstream file;
file.open(somestring.c_str());
if(file.is_good)
{
// check whether or not there is an account
}
else{
// run register code
}
Topic archived. No new replies allowed.