Im trying to write a program and having issues with what im trying to do and sadly i cant find the info in my c++ programming book. Let me explain what im trying to do.
I have a program that greets the players. I then ask them if they want to create a new account or login.
I need to find a way to have players create there account username And password and have the whole thing stored so when i rerun the program the users account is still there. I was thinking of using a array[3] since i want max 4 account to be created. I know how to ask the player for his username and password and store it in a variable however i dont know how to get it stored and be able to re-use it when i recompile the program or run it.
So basicly i want the user to be able to create up to 4 accounts max.
Maybe im getting a bit ahead of myself how ever i like to be able to read code and reverse engineer it. Thats mostly how i learn stuff. So if anyone can help me figure this out. If you post code please explain what your code does with // comments.
#include <iostream>
#include <string>
#include <map>
#include <fstream>
// lookup table: key == name mapped data == password
// http://en.cppreference.com/w/cpp/container/mapusing name_to_password_map = std::map< std::string, std::string > ;
bool make_entry( name_to_password_map& passwd_map, std::string name, std::string pword )
{
// http://en.cppreference.com/w/cpp/container/map/insertreturn passwd_map.insert( { name, pword } ).second ;
}
bool save_to_file( const name_to_password_map& passwd_map, std::string file_name )
{
// http://en.cppreference.com/w/cpp/io/basic_ostream
std::ofstream file(file_name) ; // open the file for output
// http://www.stroustrup.com/C++11FAQ.html#forfor( constauto& pair : passwd_map ) // for each name-password pair in the map
{
file << pair.first << '\n' // write the name on the first line
<< pair.second << '\n' ; // and the password on the next line
}
return file.good() ;
}
name_to_password_map load_from_file( std::string file_name )
{
name_to_password_map passwd_map ;
// http://en.cppreference.com/w/cpp/io/basic_istream
std::ifstream file(file_name) ; // open the file for input
std::string name ;
std::string pword ;
while(
std::getline( file, name ) && // try to read the first line into name
std::getline( file, pword ) // try to read the next line into pword
) // while a name-password pair was read successfully
{
// http://en.cppreference.com/w/cpp/container/map/operator_at
passwd_map[name] = pword ; // add the name-password pair toi the map
}
return passwd_map ;
}
// for testing: print out the contents of the pass word file
void show_contents( std::string file_name )
{
std::cout << "\ncontents of file '" << file_name << "'\n-----------------------\n"
<< std::ifstream(file_name).rdbuf() ;
}
int main()
{
const std::string passwd_file_name = "etc_passwd.txt" ;
{
name_to_password_map passwd_map ; // create a name-password map
// accept entries from the user and add them to the map
std::string name ;
std::string pword ;
while( std::cout << "name (enter an empty name to exit)? " &&
std::getline( std::cin, name ) && !name.empty() &&
std::cout << "password? " && std::getline( std::cin, pword ) )
{
if( make_entry( passwd_map, name, pword ) ) std::cout << "name and password added\n" ;
else std::cout << "duplicate name: ignored\n" ;
}
// save the names and passwords to the file
save_to_file( passwd_map, passwd_file_name ) ;
}
show_contents(passwd_file_name) ;
{
// create a name-password map
name_to_password_map passwd_map = load_from_file(passwd_file_name) ;
// verify that things have been read correctly
for( constauto& pair : passwd_map ) // for each name-password pair in the map
{
std::cout << "name: '" << pair.first << "' password: '" << pair.second << "'\n" ;
}
}
}
Finally, make it your code (write it on your own.)