How efficient is it

Hey guys,its been awhile, but i've been messing around with C++ again, and i've come to an assignment where i had to create a log in system, but also with the option of adding a new user, now please note that i just mess around with C++ from time to time and im an absolute beginner, my request from you is to tell me could i have done this in any shorter way than mine presented in this code, constructive criticism much appreciated :)

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  #include <iostream>
#include <string>

using namespace std;

string n_user,n_pass;

int startMenu()
{
    cout << "Invalid choice! Please try again:" << endl;
    cout << " " << endl;
    cout << "1.Log on to the system" << endl;
    cout << "2.Add new user to the system" << endl;


}

int AddUser()
{
    cout << " Initiating procedures to add new user. . . ." << endl;
    cout << " " << endl;
    cout << "Enter new username: " << endl;
    cin >> n_user;
    cout << "Choose your password: " << endl;
    cin >> n_pass;
    cout << "Creating new accout...Complete! " << endl;
}

int pass_check(string user, string pass)
{
        cout << "Hello,please enter your Username: " << endl;
        cin >> user;
        cout << "Password: " << endl;
        cin >> pass;

    if(user == "Ryze" && pass == "ryze")
    {
        cout << "System activated for Ryze!" << endl;
    }
    else if(user == "Garen" && pass == "garen")
    {
        cout << "System activated for Garen!" << endl;
    }
    else if(user == "Fizz" && pass == "fizz")
    {
        cout << "System activated for Fizz!" << endl;
    }
    else
    {
        cout << "Wrong password. System blocked to unknown access!" << endl ;
    }
}

int new_pass_check(string d_user,string d_pass)
{
        if(d_user == n_user && d_pass == n_pass)
    {
        cout << "System activated for "<< n_user <<"!" << endl;
    }
    else
    {
        cout <<" Wrong password. System blocked to unknown access!" << endl;
    }
}
int main()
{
    string user,pass;
    int choice;

    cout << "Hello please choose an option:" << endl;
    cout << " " << endl;
    cout << "1.Log on to the system" << endl;
    cout << "2.Add new user" << endl;
    cin >> choice;

    if(choice == 1)
    {
        pass_check(user,pass);
    }
    else if(choice == 2)
    {
        string d_user,d_pass;

        AddUser();
        cout << " " << endl;
        cout << "Please enter your new account data to access the system: " << endl;
        cout << " " << endl;
        cout << " Input Username: " << endl;
        cin >> d_user;
        cout << " Input password: " << endl;
        cin >> d_pass;
        new_pass_check(d_user,d_pass);

    }
    else
    {
        do
    {
        startMenu();
        cin >> choice;

        if(choice == 1)
        {

            pass_check(user,pass);
        }
        else if(choice == 2)
        {
            string d_user,d_pass;

            AddUser();
            cout << " " << endl;
            cout << "Please enter your new account data to access the system: " << endl;
            cout << " " << endl;
            cout << " Input Username: " << endl;
            cin >> d_user;
            cout << " Input password: " << endl;
            cin >> d_pass;
            new_pass_check(d_user,d_pass);
        }
    }while( !((choice == 1) || (choice == 2)));

    }






    return 0;
}
You need a way to keep track of users such as a vector or array of users and passwords.

pass_check should be generalized to handle any number of users and passwords rather than hard coding them.

new_pass_check shouldn't be needed. When you add a user, the standard pass_check function should work for the new user.

What happens if I want to add two new users, then go back and logon as the first new user?

If you really want to generalize this, you should load the users and passwords from a file, then when adding a new user add that username and password to the file.



I appreciate your input very much :D, the thing is the chapter about arrays is coming next for me now after im done with indepth details about Functions and Enumerators i think it was, but i will keep in mind, and yea, the assignment was for really amateur coders so the usernames and passwords were hardcoded just so we'd have a general idea how it would work ,if u get what i mean :) , but now i know that i should pay additional attention to the Arrays chapter,seeing how you suggested me to have used it here.
Topic archived. No new replies allowed.