Player account creation! Help please

Alright im what we call a beginner programmer.

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.

Thx in advance
Regards DestLuck
Last edited on
Read these first:
std::string - https://cal-linux.com/tutorials/strings.html
std::map - http://www.cprogramming.com/tutorial/stl/stlmap.html
file io - http://www.cprogramming.com/tutorial/lesson10.html

Then look at the following sample code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <string>
#include <map>
#include <fstream>

// lookup table: key == name mapped data == password
// http://en.cppreference.com/w/cpp/container/map
using 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/insert
    return 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#for
    for( const auto& 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( const auto& 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.)
Thx for the links ill get to reading right now.
Cant wait to learn
Topic archived. No new replies allowed.