This while loop never ends. For example, when i enter a wrong password it will keep on going to the "incorrect password" part over and over again.
Logo();
inFile.open("UsernamePassword.txt");
if (!inFile)
cout << "Unable to Open File";
else
{
cout << endl << endl << endl;
cout << " Please enter username: ";
cin >> user;
cout << " Please enter password: ";
cin >> pass;
while (username != user)
{
inFile >> username >> password;
if (user == username && pass == password)
{
cout << endl;
cout << "Welcome to CherryLunch!" << endl;
system("pause");
system("cls");
MainMenu();
}
else
{
cout << endl;
cout << " Invalid Username or Password!" << endl << endl;
system("pause");
system("cls");
}
}
}
inFile.close();
The while loop is infinite because you never allow the user to input a new password or username. When the if statement fails, it will return to loop header (where it will still be wrong) and continue onwards.
Give the user a chance to enter in a new user/pass combo and then the loop can still be finite (provided the user eventually provides the correct credentials).
move the cout and cin statements inside the while loop:
else
{
while (username != user)
{
cout << endl << endl << endl;
cout << " Please enter username: ";
cin >> user;
cout << " Please enter password: ";
cin >> pass;
inFile >> username >> password;
if (user == username && pass == password)
{
cout << endl;
cout << "Welcome to CherryLunch!" << endl;
system("pause");
system("cls");
MainMenu();
}
else
{
cout << endl;
cout << " Invalid Username or Password!" << endl << endl;
system("pause");
system("cls");
}
}
}
inFile.close();
It keeps on an infinite loop because you never ask if you reached the end of the file, so, if the file does not contain a username/password combination that matches the pair entered by the user, when the end of the file is reached, the line:
inFile >> username >> password;
Fails and username and password will contain the last entries seen on UsernamePassword.txt and the loop goes forever.
The following implementation of your program will test for the eof in the inFile file object:
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
int main() {
std::ifstream inFile;
std::string user, pass;
std::string username, password;
inFile.open("UsernamePassword.txt");
if (!inFile) {
std::cout << "Unable to Open File";
} else {
std::cout << std::endl << std::endl << std::endl;
std::cout << " Please enter username: ";
std::cin >> user;
std::cout << " Please enter password: ";
std::cin >> pass;
while (username != user && !inFile.eof()) {
inFile >> username >> password;
if (user == username && pass == password) {
std::cout << std::endl;
std::cout << "Welcome to CherryLunch!" << std::endl;
// Equivalent to the 'pause' command in linux
system("read -p 'Press any key to continue...' key");
// Equivalent to the 'cls' command in linux
system("clear");
// MainMenu();
} else {
std::cout << std::endl;
std::cout << " Invalid Username or Password!" << std::endl << std::endl;
system("read -p 'Press any key to continue...' key");
system("clear");
}
}
}
inFile.close();
}
You are trying to check for the username and password from the user and compare with all the usernames and passwords in the file.
Your approach seems fine. But you are printing "Invalid username..." after each comparison, not comparing all the usernames in the file. Hence move this output outside else block and place it in the while loop.
infile checks each line separately.
And also check the end of file.If the username and password are not found till the end of file, then print "Invald username" and provide the user to enter another set of name and password
Hope this helps!!
First, read the correct usernames and passwords from the file and save them in memory. That way, if the username/password validation fails the first time, you don't have to reopen or seek-to-beginning-in the file before checking on the next username/password typed by the user....
std::map usernames_passwords;
std::string username, password;
if (ifstream in("UsernamePassword.txt"))
while (in >> username >> password)
usernames_passwords[username] = password;
else
{
std::cerr << "Unable to open username/password file\n";
exit(EXIT_FAILURE);
}
Then prompt for login details until they're valid:
bool logged_in = false;
while (!logged_in &&
std::cout << "\n\n\n Please enter username: " &&
std::cin >> username &&
std::cout << " Please enter password: " &&
std::cin >> password)
{
// look for a match in the earlier-read login details...
auto it = usernames_passwords.find(username);
logged_in = it != std::end(usernames_passwords) && it->second == password;
}
// the while loop could also exit because "cin >>" failed, indicating EOF
// that could be because the user typed e.g. ^D (UNIX) or ^Z (Windows), or
// input was from a redirected file or pipe or over a network connection...
if (!logged_in)
{
std::cerr << "error reading login details from stdin\n";
exit(EXIT_FAILURE);
}
...ok - we know the username/password are good - do whatever else...
Related
so im trying to create a program to where it asks the user for a username and password whenever you go to register or log in. The code runs with no errors, except for whenever i go to log in it automatically logs me in without letting me type the username or password to actually log in. this is the code i have so car
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool IsLoggedIn()
{
string username, password, un, pw;
cout << "PLease enter your username!" << endl;
cin >> username;
cout << "Please enter your password!" << endl;
cin >> password;
ifstream read("data\\" + username + ".txt");
getline(read, un);
getline(read, pw);
if (un == username && pw == password)
{
return true;
}
else
{
return false;
}
}
int main()
{
int choice;
cout << "Press 1 to register\n Press 2 to Login" << endl;
cin >> choice;
if (choice == 1)
{
string username, password;
cout << "select a username:" << endl;
cin >> username;
cout << "select a password:" << endl;
cin >> password;
ofstream file;
file.open("data\\" + username + ".txt");
file << username << endl << password;
file.close();
main();
}
else if (choice == 2)
{
bool status = IsLoggedIn;
if (!status)
{
cout << "incorrect login" << endl;
system("pause");
return 0;
}
else
{
cout << "successfully logged in!" << endl;
system("pause");
return 1;
}
}
}
Change
bool status = IsLoggedIn;
(which assigns a function's address to a bool)
to:
bool status = IsLoggedIn();
(which calls the function and assigns the return value to a bool)
Your code currently never calls the function IsLoggedIn();
I am making a car rental service and am trying to write to an already existing file.
I made a previous file that has the username they entered as the name of the file. This is where the problem comes in, I made an if statement and used ofstream file and then did file.open(username + ".txt") but when I do this vs code says the identifier is unknown. The variable was made in the main function and the if statement is in the main function and I am unsure how to proceed. here is ever line of code I have written:
#include <iostream>
#include <string>
#include <fstream>
bool IsLoggedIn(){
std::string username, password, un, pw;
std::cout << "enter your username: "; std::cin >> username;
std::cout << "enter your password: "; std::cin >> password;
//checks the file for a password and username
std::ifstream read(username + ".txt");
getline(read, un);
getline(read, pw);
if(un == username && pw == password){
return true;
} else {
return false;
}
};
int main(){
int choice;
std::string option;
std::cout << "hello welcome to nathans car rental press 1 to register press 2 to login: "; std::cin >> choice;
if(choice == 1){
std::string username, password;
std::cout << "make a username: "; std::cin >> username;
std::cout << "make a password: "; std::cin >> password;
//makes a file and puts the username and password in it
std::ofstream file;
file.open(username + ".txt");
file << username << std::endl << password;
file.close();
main();
} else if (choice == 2){
bool status = IsLoggedIn();
if(!status){
std::cout << "you do not have an account" << std::endl;
system("PAUSE");
return 0;
}else{
std::cout << "welcome back" << std::endl;
system("PAUSE");
}
}
std::cout << "what car would you like to rent maserati=m lamborghini=l and ferrari=f" << std::endl;
std::cin >> option;
if(option == "m"){
std::cout << "you now have the maserati" << std::endl;
std::ofstream file;
file.open(username + ".txt");
}
};
the red squiggly line is at the username in the bottom in file.open()
you have to define your variables outside the if, if you want to use them
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.
I'm extremely new to C++, and can't really figure this out. I've tried a couple things, but I feel like I'm just missing something simple.
I have this console application where a user inputs a pre-defined password. If the password is incorrect, it prompts them to re-enter the password. If the password is correct, is simply ends the program, but I want it to say "Access granted!" then end.
A side issue I'm having is when more than word is entered as the password, "Access Denied" is printed for each word.
string password;
cout << "Please enter the password!" << endl;
cin >> password;
if (password == "test") {
cout << "Access granted!";
} else {
do {
cout << "Access denied! Try again." << endl;
cin >> password;
} while (password != "test");
}
return 0;
You need to output your "Access granted" message after the loop exits, and you also need to clear the stdin input after each failed attempt to discard any words that are still waiting to be read:
#include <limits>
string password;
cout << "Please enter the password!" << endl;
cin >> password;
if (password == "test") {
cout << "Access granted!";
} else {
do {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Access denied! Try again." << endl;
cin >> password;
} while (password != "test");
cout << "Access granted!";
}
return 0;
Live Demo
Which would be better written like this instead:
#include <limits>
string password;
cout << "Please enter the password!" << endl;
do {
cin >> password;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (password == "test") break;
cout << "Access denied! Try again." << endl;
}
while (true);
cout << "Access granted!";
return 0;
Live Demo
However, note that operator>> reads only 1 word at a time, so something like "test I GOT IN!" will also be accepted. You should use std::getline() instead to read an entire line at a time, instead of reading a word at a time:
#include <limits>
string password;
cout << "Please enter the password!" << endl;
do {
getline(cin, password);
if (password == "test") break;
cout << "Access denied! Try again." << endl;
}
while (true);
cout << "Access granted!";
return 0;
Live Demo
This is my code:
`
void Customer::validate_cust_username_and_password()
{
string uname, pword;
cout << "enter name: " << endl;
cin >> uname;
cout << "enter password: " << endl;
cin >> pword;
ifstream myfile("cust_username_and_password.txt");
if (myfile.is_open())
{
while (!myfile.eof())
{
if (uname == cust_username && pword == cust_password)
{
cout << "Login successfully." << endl;
cust_mainmenu();
break;
}
else
{
cout << "Wrong username or password!" << endl;
break;
}
}
myfile.close();
}
}
`
This is another code which store the username and password:
`void Customer::cust_register_name_and_password()
{
string un, pw;
ofstream myfile("cust_username_and_password.txt", ios::out | ios::app);
cout << "Enter Customer Username= " << endl;
getline(cin, un);
cout << "Enter Customer Password= " << endl;
getline(cin, pw);
myfile << endl << un << " " << pw << endl;
myfile.close();
cout << "Register Successfully." << endl;
system("pause");
}`
So the problem is when I enter the username and password which I already stored in the text file before, the output is only showing the "Wrong username or password!".
Really appreciate if anyone can help.
In your validate_cust_username_and_password function, you never read in the username and password. Add this:
myfile >> cust_username >> cust_password;
cust_username and cust_password are never set anywhere in this code, so they will never match the user's input (unless the user enters empty strings).
string uname, pword;
cout << "enter name: " << endl;
cin >> uname;
cout << "enter password: " << endl;
cin >> pword;
ifstream myfile("password.txt");
if (myfile.is_open())
{
while (!myfile.eof())
{
if (uname == "test" && pword == "0000")
{
cout << "Login successfully." << endl;
cust_mainmenu();
break;
}
else
{
cout << "Wrong username or password!" << endl;
break;
}
}
myfile.close();
}
}
Where did cust_username magically appear. Was Paul Daniels involved. Paul I take that back you are not as bad as David Blaine - but not a lot