C++ Tutorial 6 Attatchment // User Database - Registration & Authentication // Learn by doing

in #steemit8 years ago (edited)

User Database Authentication & Registration

header

TUTORIAL 6 ATTACHMENT

Here it is as promised.

You are going to need to get used to using header files for larger programs

The files for this program are as follows:

  1. authfunctions.h
  2. main.cpp
  3. user.db

User Database 1.0

img

authfunctions.h


This file will contain all of the programs major functions
The contents are as follows:

#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <vector>
#include <fstream>
using namespace std;
int register_user()
{
    system("cls");
    string uzr, paz;
    cout << "Authentication Database Program 1.0\n"
         << "~~~~~~~~~~~~Registration~~~~~~~~~~~~\n"
         << "                                    \n"
         << "~Desired Username: ";
    cin >> uzr;
    cout << "~Desired Password: "; 
    cin >> paz;
    //SEARCH TO SEE IF THE USER EXISTS
    //IF NOT IN DB, THEN ADD USER TO DB, IF IN DB, CLOSE.
    fstream authfile;
    authfile.open("user.db");
    if(authfile.is_open())
    {
        vector<string> filelines;
        string procstring;
        while(getline(authfile,procstring))
        {
            filelines.push_back(procstring);
        }
        stringstream uss(filelines[0]);//Users stream
        stringstream pss(filelines[1]);//Paswords stream
        istream_iterator<string> ubegin(uss), uend;//Users iterators
        istream_iterator<string> pbegin(pss), pend;//Passwords iterators
        vector<string> users(ubegin, uend); //Where we store every user
        vector<string> passwords(pbegin, pend); //Where we store every password.
        bool alreadyexists = false;
        for(int i=0; i<users.size();i++)//Do this while we haven't parsed the entire database
        {
            if(uzr == users[i])
            {
                alreadyexists = true;
                break;
            }
        }
        if(alreadyexists == true)
        {
            cout << "This username already exists.\n";
            system("pause");
            system("cls");
            authfile.close(); //Never leave a file open when you stop using it.
        }
        else
        {
            authfile.close(); //Closing the read stream
            authfile.open("user.db"); //Opening the write stream
            users.push_back(uzr);
            passwords.push_back(paz);
            string userline, passwordline;
            for(int i = 0; i<users.size(); i++)
            {
                userline = userline + users[i] + " ";
            }
            for(int i = 0; i<passwords.size(); i++)
            {
                passwordline = passwordline + passwords[i] + " ";
            }
            authfile << userline << "\n" << passwordline;
            authfile.close();
            system("pause");
            system("cls");
        }
        
    }
    else
    {
        cout << "Fatal Error: Can't connect to database!\n";
        system("pause");
    }
}
int auth(string u, string p)
{
    bool authstate = false;
    fstream authfile;
    authfile.open("user.db");
    if(authfile.is_open())
    {   
        vector<string> filelines;
        string procstring;
        while(getline(authfile,procstring))
        {
            filelines.push_back(procstring);
        }
        stringstream uss(filelines[0]);//Users stream
        stringstream pss(filelines[1]);//Paswords stream
        istream_iterator<string> ubegin(uss), uend;//Users iterators
        istream_iterator<string> pbegin(pss), pend;//Passwords iterators
        vector<string> users(ubegin, uend); //Where we store every user
        vector<string> passwords(pbegin, pend); //Where we store every password.
        for(int i=0; i<users.size() || i<passwords.size();i++)//Do this while we haven't parsed the entire database
        {
            if(u == users[i] && p == passwords[i])
            {
                authfile.close();
                authstate = true;
                break;
            } 
        }
    }
    else
    {
        cout << "MAJOR ERROR Could not connect to database!\n";
    }
    return authstate;
}
int list()
{
    bool exit = false;
    list:
    system("cls");
    cout << "Welcome to our informational databse!\n1. Contacts\n2. Passwords\n3. Notes\n";
    cout << "Choose an option from the list above (ex: 1) or 0 to exit!\nYour choice: \n";
    int option;
    cin >> option;
    switch(option) 
    {
        case 0:
            {
                system("cls");
                cout << "Bye!\n";
                exit = true;
                break;
            }
        case 1:    
            {      
                system("cls");
                cout << "~Contact List~\n"
                     << "Name    Phone\nAmy     40003231\nJoe     40003231\n"
                     << "John    40003231" << endl;
                     break; 
            }               
        case 2:
            {
                system("cls");
                cout << "      ~My Password list~\n"
                     << "_______________________________\n"
                     << "|Website           | Password |" << endl
                     << "|==================|==========|" << endl
                     << "|www.gmail.com     | fluffy01 |\n"
                     << "|------------------|----------|\n"
                     << "|www.facebook.com  | 01234567 |\n"
                     << "|------------------|----------|\n"
                     << "|www.steemit.com   | hawh2h8a |\n"
                     << "|__________________|__________|\n";
                     break; 
            }
        case 3:
            {
                system("cls");
                cout << "~My notes - Read them~\n"
                     << "Note 1: Remember to feed your pets\n"
                     << "Note 2: Remember to add a ; to every line that finishes an instruction\n"
                     << "Note 3: Remember to follow me if you like my tutorials\n"
                     << "Note 4: Don't forget to add a break after each case in switches!\n"
                     << "Note 5: Don't forget to add a \\n after each comment so the text displays properly\n"
                     << "Note 6: Notice that one \\ in front of a newline inside a comment escapes the newline\n"
                     << "Note 7: If my tutorial is helping you please like, follow and even resteem it so you can help me aswell!\n";
                     break;
                
            }
        default:   
            {      
                cout << "There is no such option!\n";
                system("pause");
                break;
            }
    }
    system("pause");
    if(exit == false)
    {
            goto list;
    }
}

user.db


Create this with notepad.
This is our user database, the first line contains users, the second line contains passwords.
make sure it contains the following:

admin
1234

main.cpp


Here we run all of our functions together. The contents of main.cpp should be:

#include "authfunctions.h"
int main()
{
    bool exitprogram = false;
    authdb:
    system("cls");
    cout << "Authentication Database Program 1.0\n"
         << "What would you like to do?\n"
         << "1) Login\n2) Register\n"
         << "Select one of the options above or enter 0 to exit.\n"; 
    int swc;
    cin >> swc;
    switch(swc)
    {
    case 1:
    {
        system("cls");
        string u, p;
        cout << "Authentication Database Program 1.0\n"
             << "Please enter your credentials.\n"
             << "Username: ";
        cin >> u;
        cout << "Password: ";
        cin >> p;
        if(auth(u, p))
        {             
            cout << "Authentication Success!\n";
            system("pause");
            list();
            exitprogram = true;
        }
        else
        {
            cout << "Authentication Failure!\n";
            system("pause");
        }
        break;
    }
    case 2:
    {
        register_user();
        break;
    }
    case 0:
    {
        system("cls");
        cout << "Bye!";
        exitprogram = true;
    }
    }
    if(exitprogram == false)
    {
        goto authdb;
    }
    return 0;
}

Now you just have to make sure the header is added to your project in the compiler & make sure user.db is in the same directory as main.exe.

Compile & Run - Enjoy.

As always. I hope you enjoyed my tutorial, Upvote & Follow or even resteem if you liked it & feel free to ask me anything in the comments below!

Foo

The header is of my own design.
Everything written here is my own work.
Feel free to comment I'd love to hear your opinions!
List of images that are public domain:
acgranted

Sort:  

Respect for your own work! Thank you :-)

It looks all Greek to me. But I want to learn, so I will follow your work. Please follow me back. I have some stories to tell, stuff to teach, and interesting things to say.. ~Cheers~

Well, you just have to check my blog and start from the first one which is only introductory, a compiler such as dev c++ ( at least for this tutorial) and after that just follow the explanations and the code.
The first day you should get used to statements and adding the semicolon (;) after every statement ( not function. ) and how to print to the console so you get used to it, after that following my tutorials (1 per day, I made them in such a way that anyone can understand them) and playing gradually with what you've learned, you can most likely get used to it in one week if you're really dedicated. Or two if you aren't familiar at ALL with development.
After that you simply google what you need.
As i've included "Trick examples" so to speak, you will have to google a lot less though.

OK...thank you very much. I will do that. Also, thank you for following me. I hope you enjoy my writing--especially my science fiction writing-- as much as I can enjoy indulging my curiosity and desire to learn, from your posts. ~Cheers~

Also, since I am just really starting, and we all need a helping hand, I am going to start, daily, looking at the Blogs of my Followers, finding things I like that are over one day old and less than a month old, then upvoting them and resteeming them. I would ask you to do the same for me. This way, we all get continual exposure. That is what we all need, right? I am staring today. I will look at your Blog and resteem and upvote the ones I like.

I only post technological stuff, programming computers, technology cryptocurrency is ok
I don't want to spam my followers with things they might not want to see from me.

It's all good...still love your posts...

Coin Marketplace

STEEM 0.16
TRX 0.13
JST 0.027
BTC 57560.27
ETH 2570.10
USDT 1.00
SBD 2.48