Login/Registration Program

I'm working on semester project and I need help with the login/registration parts I'm studying file handling and my concepts are kind of weak when it comes to FH (File Handling) Anyway you guys can give me AN EXAMPLE PROGRAM WHICH SHOWS HOW TO READ PASSWORDS AND REGISTER?

PLEASE BE SIMPLE IN APPROACH
I have studied up to 2D Arrays
NO pointers or vectors and yes I'm a beginner The language is C++

What do you mean with reading passwords and registering?

Do you want a thing like:

Username: <user types in their username here>
Password: <user types in their password here>

Username and password gets saved in a file such as database.dat?

Be more clear with what you want so that we can help you to guide you through this.
first of all, I am going to assume you at least started working with fstream. If fstream is not your area, then it's not good. The only other method that I know of is using pointers.
in that case I've got something for you, though:
-you need to know strings (string name)
OR
-you need to know C strings (char name[30])
if you know at least one of them, this should be it.
if not, then there's a method which uses conio.h library:
-getch(), which detects 1 character being pressed
-kbhit() or something, which checks whether a key has recently been pressed (this is not for the context, but it could help you in the future if you know it exists and how to combine the two)
for this method you'd have to input 1 by 1, each character, into files and then into console
that would mean it's more complex (in terms of "more probable to bring bugs")
but now that I think of it, it could be a more compact code, since it's all loops and character readings
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
#include<iostream>
#include<fstream>
//#include<cstring>
//for C strings
using namespace std;
int main()
{
    string
    command, //to choose what to do each time
    name, password, //the ones that are found in the file
    inName, inPassword, //the ones you are going to input from keyboard
    registerName, registerPassword; //also what you're going to input
    //and if you know C strings, just replace that with something like
    /*
    char
    command[9],
    name[31], password[31], //it could be any size, but like this you have got 30 characters at your
    //disposal, if you consider it to be enough
    inName[31], inPassword[31],
    registerName[31], registerPassword[31];
    */
    while (1)
    {
        cout<<"(register/exit/login)\n"
            <<"Command: ";
        getline(cin, command);
        //(for C strings)
        //cin.get(command, 9);
        //cin.get();
        if (command=="exit") //(for C strings) if (!strcmp(command, "exit"))
        {
            return 1;
        }
        if (command=="register") //(for C strings) if (!strcmp(command, "register"))
        {
            //open file for registration
            ofstream g("registration.txt"); //ofstream is the one for getting data from the file, 
            //and the file does not even have to exist. If it's ofstream, it'll take care of it for you.
            //but be warned that if there is a file called "registration.txt" in the name folder as the
            //.exe file, the contents will be deleted
            if (!g.is_open()) //if it's not open, then there is no such file with the given name inside
            //the folder (that is, in the folder where the .exe file is going to be)
            {
                cout<<"could not open file\n"; //just so that you know why it won't work if it doesn't
                return 0;
            }
            cout<<"\n\n\n" //3 newlines
                <<"New Username: ";
            getline(cin, registerName); //input from keyboard will go into registerName
            cout<<"New Password: ";
            getline(cin, registerPassword); //input from keyboard will go into registerPassword
            g<<registerName; //this basically says "put whatever's to the right (registerName) into
            //g ("registration.txt")".
            g<<'\n'; //and now there will be a new line
            g<<registerPassword; //and now the password
            //all placed safely in the file that g opened
            g.close(); //always make sure you close the file, or else you might end up with some nasty
            //stuff in the memory
        }
        if (command=="login") //(for C strings) if (!strcmp(command, "login"))
        {
            //open file, and then put the name and password into the strings
            ifstream f("registration.txt"); //ifstream is the one for getting data from the file, and
            //let us assume you've already created a file called "registration.txt"
            if (!f.is_open()) //if it's not open, then there is no such file with the given name inside
            //the folder (that is, in the folder where the .exe file is going to be)
            {
                cout<<"could not open file\n"; //just so that you know why it won't work if it doesn't
                return 0;
            }
            getline(f, name, '\n'); //reads the user name from file f (which is using "registration.txt")
            getline(f, password, '\n'); //reads the password from file f (which is using "registration.txt")
            //also, if you tell the file to get you that text up until '\n', that's when you know it reads
            //the whole line at most, and won't go any further
            //and that is done by the 3rd parameter
            f.close(); //you don't need it open now, since you have the name and password from the file

            //login
            while (1)
            {
                //you are going to input the name and password here
                cout<<"\n\n\n"
                    <<"Enter Username: ";
                getline(cin, inName);
                cout<<"Enter Password: ";
                getline(cin, inPassword);
                //or this, if you are working with C strings (second version of declaration)
                //cin.get(inName, 31);
                //cin.get();
                //cin.get(inPassword, 31);
                //cin.get();
                //and the "cin.get()" after each input line is necessary, or else[...]
                //no idea what's happening inside istream, but it's mandatory if you don't want your
                //input to get stuck or worse
                if (inName==name && inPassword==password)
                {
                    cout<<"Login Successful\n" //the '\n' is a character, so that's why I can add it 
                    //and it will automatically output a newline in console, alongside the string
                        <<"Welcome, "
                        <<inName;
                    break; //just exit the while loop if you've entered the valid account
                }
                cout<<"incorrect name or password\n"; //if you haven't entered the valid account,
                //then the while loop is not done yet. So that's why this output is without condition
            }
            //now do something about the account
        }
        cout<<"\n\n\n\n\n"; //give it 5 newlines
    }
    return 1;
}

Topic archived. No new replies allowed.