Elephant SQL with c++ login system - c++

i created a cleint that requires you to register and login to acces certain apps but txt is not a efficent idea especially when i gave it to my friends so am trying to use a online data base but have 0 experince on how to do so so any help would be appreciated try to make words as simple as possiible still a biggener i tried searching online and trying diffrent ways but coudnt find something i could understand ao am still not sure of how to implement this and if its even possible in c++
here is my initial code for login and registering
void login() {
system("color 0B");
int counts = 0;
string userid, password, id, pass;
system("cls");
cout << "\t\t\t Please Enter your username and password : " << endl;
cout << "\t\t\t USERNAME:";
cin >> userid;
cout << "\t\t\t PASSWORD:";
cin >> password;
ifstream input("records.txt");
while (input >> id >> pass) {
if (id == userid && pass == password) {
counts = 1;
system("cls");
}
}
input.close();
if (counts == 1) {
secret(userid);
}
else {
cout << "\nLOGIN ERROR \n Username or password is inccorect\n\n";
main();
}
};
void Register() {
system("color 0B");
system("cls");
string ruserid, rpassword, rid, rpass;
string choice_2;
cout << "\t\t\t Welcome to my application you can register below\n";
cout << "press 1 to register \n";
cout << "press 2 to get back to the main menu \n";
cout << "\t\t\t Enter your option : ";
cin >> choice_2;
if (choice_2 == "1") {
string lock;
cout << "Enter registration password\n";
cout << "input password : ";
cin>>lock;
if (lock == "register_2006") {
system("cls");
cout << "\t\t\t Enter The USERNAME : ";
cin >> ruserid;
cout << "\t\t\t Enter The PASSWORD : ";
cin >> rpassword;
ofstream f1("records.txt", ios::app);
f1 << ruserid << ' ' << rpassword << endl;
system("cls");
cout << "\n\t\t\t Registration is Successful \n";
main();
}
else { cout << "Wrong password please contact to get it\n"; }
main();
}
else if (choice_2 == "2") {
system("cls");
main();
}
else {
cout << "wrong choice try again";
Register();
}
}

Related

C++ ifstream is reading last line only

I am working on a program that allows a user to register an account. When a user registers for an account, the username and password are output to a text file "database.txt".
There is also an option for a user to search for their password by inputing their username if they forget their password. I find that this works fine when there is only one user registered in the .txt file database. However, when there is more than one user, the forgot password function returns the password of the most recent registered user, no matter which username is searched.
database.txt looks like this:
user1 111
user2 222
user3 333
No matter which user is entered to find the password, the function will always return 333, which is the most recent registered user's password. How do I fix this so that whichever user is searched, their password will output?
Here is the function:
void forgot()
{
int ch;
system("cls");
cout << "Forgotten? We're here to help." << endl;
cout << "Choose one of the options below: " << endl;
cout << "1. Forgot my password" << endl;
cout << "2. Forgot my username" << endl;
cout << endl;
cout << "Enter your choice: ";
cin >> ch;
switch(ch)
{
case 1: // search for username to find password
{
int count = 0;
string searchUser, su, sp;
cout << "Enter your username: ";
cin >> searchUser;
ifstream searchUserName("database.txt");
while(searchUserName >> su >> sp)
{
if(su == searchUser)
{
count = 1;
}
}
searchUserName.close();
if(count == 1)
{
cout << "Account found!" << endl;
cout << "Your password is: " << sp;
cin.get();
cin.get();
system("cls");
menu();
}
else
{
cout << "Sorry, that user ID does not exist." << endl;
cout << "Please contact our service team for more details." << endl;
cin.get();
cin.get();
system("cls");
menu();
}
break;
}
case 2: // search for password to find username
{
int count = 0;
string searchPass, su2, sp2;
cout << "Enter your password: ";
cin >> searchPass;
ifstream searchPassword("database.txt");
while(searchPassword >> su2 >> sp2)
{
if(sp2 == searchPass)
{
count = 1;
}
}
searchPassword.close();
if(count == 1)
{
cout << "Your password has been found." << endl;
cout << "Your username is: " << su2;
cin.get();
cin.get();
system("cls");
menu();
}
else
{
cout << "Sorry, we couldn't find your password in our database." << endl;
cout << "Please contact our team for more information." << endl;
cin.get();
cin.get();
system("cls");
menu();
}
break;
}
default:
cout << "Invalid choice, please try again." << endl;
system("cls");
forgot();
}
}
You should break out of the while loop once you have found the user name here:
while(searchUserName >> su >> sp)
{
if(su == searchUser)
{
count = 1;
break; // add this
}
}
Now it will continue to overwrite a previously found user name until searchUserName >> su >> sp is no longer true which is once it encounters EOF in most cases.
The other part of the program has the same problem.
Personnally I would rewrite the code such that the condition whether a matching password has been found or not is part of the loop condition.

(C++) Read username and password from a file output problem

my program of registration and login encountered a few problems during output :
I have to register a new user and pass first(even if i alrd have previous usernames and passwords stored in the text file im trying to retrieve it from), after that only i can login using previous username and passwords and this repeats after i close the debug window and start debugging again (if i directly choose to login upon running the program, it will output "invalid username or password")
when logging out from a newly registered username, the program jumps to the
int main() AND DISPLAY "1. Register...."
but logging out from previous usernames, it jumps to
void login() and display "Username:"
*note: the last function isn't complete yet but i think it doesn't affect it (?) (the program worked fine before i added the void accountPage()tho)
*i am not supposed to use pointers plus i'm very new to c++
the code is a bit long but its just a lot of simple functions, i would rly appreciate it if someone can point out my mistake anywhere
#include <iomanip>
#include <cctype>
#include <fstream>
#include <string>
using namespace std;
//Global Variables
int Choice1;
int mobile, ic;
string user, pass, name, inUser, inPass;
//Function Prototypes
void register_user();
void login();
void bookRoom();
bool CheckCredentials(string, string);
void accountPage();
int main()
{
cout << "Welcome to Cozy Homes!\n";
cout << "Operating hours: 11am - 10pm Tuesday - Sunday\n\n\n\n";
do {
cout << "\n1. Register\n";
cout << "2. Log In\n";
cout << "3. Exit\n";
cout << "Please enter a number:";
cin >> Choice1;
if (Choice1 == 1)
{
register_user();
}
else if (Choice1 == 2)
{
login();
}
else if (Choice1 == 3)
{
cout << "Exiting now...\n";
return 0;
}
else if (Choice1 < 1 || Choice1 > 3)
{
cout << "Please choose a number from the menu!" << endl;
}
} while (Choice1 != 3);
system("pause");
return 0;
}
//Register page
void register_user()
{
cin.ignore();
cout << "\n\n\n" << "New Username: ";
getline(cin, user);
cout << endl;
cout << "New Password: ";
getline(cin, pass);
cout << endl;
cout << "Full name: ";
getline(cin, name);
cout << endl;
cout << "Mobile Number: ";
cin >> mobile;
cout << endl;
cout << "Ic Number (without \" - \"): ";
cin >> ic;
cout << endl;
cout << "Registered Successfully!" << endl;
cout << endl;
//Store username and password in login file
ofstream l("login.txt", ios::app);
if (!l.is_open()) {
cout << "could not open file \n";
}
l << user << " " << pass << endl;
l << endl;
l.close();
//Store other details in customer file
ofstream c("customer.txt", ios::app);
if (!c.is_open()) {
cout << "could not open file \n";
}
c << user << endl;
c << pass << endl;
c << name << endl;
c << mobile << endl;
c << ic << endl;
c << '\n';
c.close();
}
//Log in page
void login()
{
do
{
cout << "\nUsername: ";
cin >> inUser;
cout << "Password: ";
cin >> inPass;
if (CheckCredentials(inUser, inPass) == true)
{
cout << "\nLogin sucessful!" << endl;
cout << "Welcome, " << inUser << endl;
cout << endl;
accountPage(); // Redirects user to their account page after successfully logged in
}
else
cout << "\nInvalid username or password. " << endl;
} while (CheckCredentials(inUser, inPass) != true);
}
//Validate their username and password
bool CheckCredentials(string inUser, string inPass)
{
string u;
string p;
bool status = false;
ifstream f;
f.open("login.txt");
if (!f.is_open())
{
cout << "Unable to open file!\n";
}
else if (f)
{
while (!f.eof())
{
f >> u >> p;
if (inUser == u && inPass == p)
{
status = true;
}
else
{
status = false;
}
}
}
f.close();
return status;
}
//Account Page
void accountPage()
{
int Choice2;
do
{
cout << "1. Profile\n";
cout << "2. Book a Room\n";
cout << "3. Cancel Booking\n";
cout << "4. Logout\n";
cout << "Please enter a number: ";
cin >> Choice2;
if (Choice2 == 1)
{
}
else if (Choice2 == 2)
{
}
else if (Choice2 == 3)
{
}
else if (Choice2 == 4)
{
cout << "Logging out.....\n\n\n\n";
cout << endl;
}
} while (Choice2 != 4);
}
//Booking page
void bookRoom() {
cout << " ";
}
```
Like this :
void login()
{
bool loggedin = false;
while(!loggedin)
{
cout << "\nUsername: ";
cin >> inUser;
cout << "Password: ";
cin >> inPass;
if (CheckCredentials(inUser, inPass) == true)
{
loggedin = true;
cout << "\nLogin sucessful!" << endl;
cout << "Welcome, " << inUser << endl;
cout << endl;
accountPage(); // Redirects user to their account page after successfully logged in
}
else
cout << "\nInvalid username or password. " << endl;
}
}
or like this:
void login()
{
bool loggedin = false;
do
{
cout << "\nUsername: ";
cin >> inUser;
cout << "Password: ";
cin >> inPass;
if (CheckCredentials(inUser, inPass) == true)
{
loggedin = true;
cout << "\nLogin sucessful!" << endl;
cout << "Welcome, " << inUser << endl;
cout << endl;
accountPage(); // Redirects user to their account page after successfully logged in
}
else
cout << "\nInvalid username or password. " << endl;
}
while(!loggedin)
}
As this is a school assignment I did not bother to test the code myself.
It is just meant to get you futher and I did only minimal changes.
The point of your error is that the faulty function calls checkcredentials before a user and pasword is entered in the system.
If this solves your problem please mark as solution.
l << user << " " << pass << endl;
l << endl; <---- creates an empty line in your file. Is this intentional ?
l.close();
Clearly there is more bugs in your program. But this will get you further.
Have to do my own job now ;-)

How do I save passwords and usernames to a .txt file in c++. I still want to be able to retrieve them if the user tries to login

I'm starting a console based online bank(for fun). I was wondering how I would save usernames and passwords of people registering an account(perhaps a .txt file?). I was also wondering how I would go about checking the .txt file for the username and password when they attempt to log in. Any help is appreciated. Here is the source code
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <string>
#include <stdlib.h>
using namespace std;
bool gameOver;
// global variables
string EntryChoice;
int numberOfIncorrect = 5;
string NewUsername;
string NewPassword;
string LoginUsername;
string LoginPassword;
string NewFirstName;
string LoginFirstName;
string NewLastName;
string LoginLastName;
int Newage;
string Newgender;
int Loginage;
string LoginGender;
//declaration of functions
void Login();
void SignUp();
void BankCheck();
void BankError();
void Bank()
{
cout << "Welcome to the Bank of National Arabs\n";
cout << "-------------------------------------\n";
cout << "|To Sign Up type (Register) then Press Enter|\n";
cout << "|To Login type (Login) then Press Enter|\n";
cin >> EntryChoice;
BankCheck();
}
void BankCheck()
{
if (EntryChoice == "Login" || EntryChoice == "LOGIN" || EntryChoice == "login")
{
Login();
}
else if (EntryChoice == "REGISTER" || EntryChoice == "register" || EntryChoice == "Register")
{
SignUp();
}
else
{
system("cls");
BankError();
}
}
void BankError()
{
if (numberOfIncorrect == 1)
{
exit(1);
}
numberOfIncorrect -= 1;
cout << "Welcome to the Bank of National Arabs\n";
cout << "-------------------------------------\n";
cout << "|To Sign Up type (Register) then Press Enter|\n";
cout << "|To Login type (Login) then Press Enter|\n";
cout << "|ERROR| " << numberOfIncorrect << " Tries Left >> ";
cin >> EntryChoice;
BankCheck();
}
void Login()
{
system("cls");
cout << "Bank of United Arabs Login Page\n";
cout << "-------------------------------\n";
cout << "Username: ";
cin >> LoginUsername;
cout << "\nPassword:";
cin >> LoginPassword;
}
void SignUp()
{
system("cls");
cout << "Welcome to the Register Page\n";
cout << "----------------------------\n";
cout << "Age: ";
cin >> Newage;
cout << "\nGender: ";
cin >> Newgender;
cout << "\nFirst name";
cin >> NewFirstName;
cout << "\nLast Name";
cin >> NewLastName;
cout << "\nUsername: ";
cin >> NewUsername;
cout << "\nPassword";
cin >> NewPassword;
}
int main()
{
Bank();
return 0;
}
Yes, you can do so. Probably, everytime you input data from the user while registering, you can simultaneously store it into a txt file using c++ file operators(Read this : https://www.bogotobogo.com/cplusplus/fstream_input_output.php ), followed by a delimiter to mark the end of each individual record.
Keep the format in the text file as :
UserID1
Username1
Password1#
UserID2
Username2
Password2#
Here # is a delimiter.
For retrieving the records search for the username or id in the .txt file by reading records using the above delimiter. If the record is found, separate the record values by line and store them in a variable and then compare with the input data values. Continue searching till the EOF flag is false.

How to make seperate menus that work based on the login

So for the program I am making, I wanted to make a login that goes to two different menus then go back to the login. I'm not sure how to approach it now.
It goes something like:
string User;
string Pass;
int Option;
void Login(){
cout << "Enter your username: ";
cin >> User;
cout << "Enter your password: ";
cin >> Pass;
}
void Admin(){
system("CLS");
cout << "Welcome Admin" << endl;
cout << "------------" << endl;
cout << "1. Do something" << endl;
cout << "2. Do something else" << endl;
cout << "3. Log out" << endl;
cout << "4. Quit Program" endl;
cin >> Option;
}
void User(){
system("CLS");
cout << "Welcome User" << endl;
cout << "------------" << endl;
cout << "1. Do another thing" << endl;
cout << "2. Do something other things don't do" << endl;
cout << "3. Log out" << endl;
cout << "4. Quit Program" endl;
cin >> Option;
}
int main(){
Login();
if(User == "admin" && Pass == "admin"){
Admin();
if(Option == 3){
// What should I add here if would want to return to login then to user menu
}
}
else
User();
}
Well, if you want to return to login menu you can use a loop:
int main()
{
while(1)
{
Login();
if(User == "admin" && Pass == "admin")
{
Admin();
}
else
{
User();
}
if(Option == 3) continue;
if(Option == 4) break;
}
return 0;
}
upd: sorry, forgot about the loop :)

How to store Username and Password into a file in 2 void functions?

I'm going to simplify my question... How would I implement an If/else statement or any statement that would take data the user enters from my createaccount function and verifies it against the login function? If the login does not match what is in the file "accounts.txt", it would not work and loops back to the mainmenu function.
using namespace std;
char mainMenu();
void createAccount();
void login();
char bankingMenu(); // Prototypes Declared Here
void deposit();
void withdraw();
void displayBalance();
void ShowTransHist(); // Gets transaction history from the file path string and display
int main()
{
char choice;
while (choice != 'q' || choice != 'Q')
{
choice = mainMenu();
if (choice == 'q' || choice == 'Q') break;
switch (choice)
{
case 'l':
case 'L':
login();
break;
case 'c':
case 'C':
createAccount();
break;
case 'v':
case 'V':
cout << "Thank you for using our bank and Future Computer Programmer ATM Machine! \nFor your continued support, we are offering 3% cash back on all debit purchases." << endl;
}
}
return 0;
}
char mainMenu() // Function to display the main menu, not banking menu
{
char choice;
cout << "Please select an option from the menu below:" << endl;
cout << "l -> Login" << endl;
cout << "c -> Create New Account" << endl;
cout << "v -> View Promotions" << endl;
cout << "q -> Quit" << endl;
cout << "Enter your choice: " << endl;
cin >> choice;
return choice;
}
void createAccount() // Takes and stores users login information, username and password
{
string username;
string password;
cout << "Please create a username";
cin >> username;
cout << "Please create a password";
cin >> password;
ofstream createaccount;
createaccount.open("accounts.txt");
createaccount << username << " " << password;
createaccount.close();
cout << "Information saved" << endl;
}
void login() // Takes information stored in create account
{
string username;
string password;
cout << "Please enter your username";
cin >> username;
cout << "Please enter your password";
cin >> password;
ifstream savedaccount;
savedaccount.open("accounts.txt");
savedaccount >> username >> password;
savedaccount.close();
cout << "Login successful";
}