In: Computer Science
Create a C++ login program using file for 2 user. Usernames must be in one file and password in the other file. Ask user for their usernames and password if it matches, they logged in successfully. If it doesn’t match,they have 3 trials ,then they have to sign in and create a username and password. After creating a username and password, they have to go to the login page again. Must work for visual studio code
SOURCE CODE
/* In this program we shall assume that the username and passwords are stored in such a way that the position of the username matches the position of its corresponding password */
#include <iostream>
#include <fstream>
//macro defining the number of usernames to be handled by program
#define USER_COUNT 2
using namespace std;
//function prototypes
void read_username(string [], fstream &);
void read_pass(string [], fstream &);
bool check_pass(string, string *, int);
int check_username(string, string *);
int main()
{
//defining variables
fstream username_file, pass_file;
string usernames[2], passwords[2];
//reading usernames from a file
read_username(usernames, username_file);
//reading passwords from a file
read_pass(passwords, pass_file);
string username, password;
int index;
//prompt to enter username
cout << "\nEnter username: "; cin >> username;
//check if username exists, if it exists store the index number
//of username in a variable
index = check_username(username, usernames);
//if username exists, ask for password
if(index != -1)
{
cout << "Enter password: "; cin >> password;
//chek password
if(check_pass(password, passwords, index))
cout << "Verified. Username and password match";
else
cout << "Invalid password";
}
//if username does not exixt
else
cout << "Invalid username...\nExiting...";
return 0;
}
//this function reads all usernames from a file and stores it in an array of strings
void read_username(string usernames[], fstream &file)
{
//opening the file
file.open("username.txt", ios::in);
//check if file was opened successfully
if(!file)
{
cout << "\nError opening file...";
return;
}
int i = 0;
string temp;
//read the file contents one by one and store in array
while(file >> temp)
{
usernames[i] = temp;
i++;
}
//close the file
file.close();
}
//this function reads all passwords from a file and stores it in an array of strings
void read_pass(string passwords[], fstream &file)
{
//opening the file
file.open("password.txt", ios::in);
//check if file was opened successfully
if(!file)
{
cout << "\nError opening file...";
return;
}
int i = 0;
string temp;
//read the file contents one by one and store in array
while(file >> temp)
{
passwords[i] = temp;
i++;
}
//close the file
file.close();
}
//this function checks the existence of username and returns the index of its position in array
int check_username(string username, string usernames[])
{
for(int i = 0; i < USER_COUNT; i++)
if(usernames[i] == username)
return i;
return -1;
}
//this function matches a username against its password
bool check_pass(string password, string passwords[], int index)
{
return passwords[index] == password;
}
//username.txt
user1@dumb
user2@smart
//password.txt
user1pass
user2pass
OUTPUT