mercredi 26 mai 2021

username and password validation ************

This is a block of code that is want to be executed

for (int i = 0; i < username_vector.size(); i++)
{
    if (user_name2 == username_vector[i])
    {
        for (int j = 0; j < password_vector.size(); i++)
        {
            if (password2 == password_vector[i])
                cout << "Welcome to your World" << endl;
        }
    }
    cout << "Your Username is wrong Or Your PASSWORD you've enterd if wrong" << endl;

*This is my project the idea is I ask the user if you new user or you already logged in, if the user logged in then i check his username and password, if the username and password is the same i print "Welcome to your World" otherwise print "Your Username is wrong Or Your PASSWORD you've entered is wrong" and if the user is new i take his username and password and insert them into file.

the problem is When I check the the username and password then I want to print "Welcome to your World" otherwise print "Your Username is wrong Or Your PASSWORD you've entered is wrong" it didn't print the program exit*

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <functional>

using namespace std;

std::function<bool(const std::string &, const std::string &)> comparator = [](const std::string &left, const std::string &right)
{
    // Lambda function to compare 2 strings in case insensitive manner
    return std::equal(left.begin(), left.end(), right.begin(), [](const char &l, const char &r)
                      { return (::toupper(l) == ::toupper(r)); });
};

string user_name, password;
vector<string> username_vector;
vector<string> password_vector;

const char alphnum[] = "0123456789"
                       "!@#$%^&*"
                       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                       "abcdefghijklmnopqrstuvwxyz";

int size_alphnum = sizeof(alphnum) - 1;
char alphnum_generate();
string password_generator();
void sign_up();
void user_count();
void check();
// To Check IF USER is Exist

int main()
{
    int have_acc;
    cout << "1- Already have account\n2- New User\n";
    cout << "Enter here 1 OR 2 --> ";
    cin >> have_acc;

    if (have_acc == 1)
        check();

    if (have_acc == 2)
    {
        sign_up();
        user_count();
    }
}

char alphnum_generate()
{
    return alphnum[rand() % size_alphnum];
}
string password_generator()
{

    srand(time(0));
    int n, c = 0, s = 0;
    cout << "Enter The Length of Your PASSWORD: ";
    cin >> n;
    cout << "Your PASSWORD --> ";

label:
    char C;
    string D;
    for (int i = 0; i < n; i++)
    {
        C = alphnum_generate();
        D += C;

        if (isdigit(C))
            c++;

        if (C == '!' || C == '@' || C == '$' || C == '%' || C == '^' || C == '&' || C == '*' || C == '#')
            s++;
    }
    if (n > 2 && (s == 0 || c == 0))
        goto label;

    cout << D << endl;
    return D;
}

void sign_up()
{
    // TO STORE USER NAME AND PASSEWORD

    // USER NAME
    user_name;
    cout << "Enter Username: ";
    cin.clear();
    cin.ignore();
    getline(cin, user_name);
    username_vector.push_back(user_name);

    // PASSWORD
    password;
    password = password_generator();
    password_vector.push_back(password);
    // DECLARE FILE
    ofstream oFile;
    //OPEN FILE
    oFile.open("testfile.txt", ios::app);

    // INSERT THE USER_NAME  AND PASSWORD Into File
    for (int i = 0; i < username_vector.size(); i++)
    {
        oFile << "Username: " << username_vector[i];
    }
    oFile << endl;
    for (int i = 0; i < password_vector.size(); i++)
    {
        oFile << "Password: " << password_vector[i] << endl;
    }
    oFile << "===========================\n";
}

void user_count()
{
    ifstream iFile("testfile.txt");
    vector<string> store_data;
    string line;
    int count = 0;
    // To read each line and insert them into Vector "store_data";
    while (getline(iFile, line))
    {
        store_data.push_back(line);
    }
    // How many User had Entered
    for (int i = 0; i < store_data.size(); i++)
    {
        if (store_data[i] == "===========================")
            count++;
    }
    // diplay how many User
    cout << count << " of User logged in\n";
}

void check()
{
    vector<string> username_vector2;
    vector<string> password_vector2;

    string user_name2;
    cout << "Enter Username: ";
    cin.clear();
    cin.ignore();
    getline(cin, user_name2);
    username_vector2.push_back(user_name2);

    // PASSWORD
    string password2;
    cout << "Enter Your PASSWORD: ";
    getline(cin, password2);
    password_vector2.push_back(password2);
    // DECLARE FILE
    //ifstream find;
    //OPEN FILE
    //find.open("testfile.txt");
    
    for (int i = 0; i < username_vector.size(); i++)
    {
        if (user_name2 == username_vector[i])
        {
            for (int j = 0; j < password_vector.size(); i++)
            {
                if (password2 == password_vector[i])
                    cout << "Welcome to your World" << endl;
            }
        }
        cout << "Your Username is wrong Or Your PASSWORD you've enterd if wrong" << endl;
    }
}

Aucun commentaire:

Enregistrer un commentaire