Hello!! I am a beginner c++ user and need help!
I need to write a program where you can are given a temporary password(1234) and a username(123456)
you have 3 tries to get this correct if you don't then the program terminates.
if you get it right at any time you should be able to "log in" and reset your password to a 4 digit number and different from the temporary password.
so far I am here but I am stuck... any help would be greatly appreciated
I set my code so that when the person uses the temporary password it goes through
if(username == USERNAME && password == saved_password)...
and when the user goes through the program and successfully changes the password to the new password it goes through
if(username == USERNAME && password == new_password)...
However there seems to be a problem on how I am writing it, I want it to be if it is using the saved password to follow these rules and if the password has been reset to use these rules but its not working how I thought it would
Thank you
the code I am talking about will be in line 38 through 64
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <iostream> // library that contains basic input output functions
#include <cstdio> // library for file read write functions
#include <string> // library for c++ strings
using namespace std;
int main()
{
bool finished = false;
bool login_finished = false;
int fail = 3;
//strings in which user will enter username and password
string username, password, saved_password, new_password, un, pw;
// hard-coded saved password as 1234
saved_password = "1234";
while (!login_finished)
{
// ask the user to login
while( !finished && fail > 0)
{
//Username to validate credentials
const string USERNAME = "123456";
// Prompting user to input username
cout << "Enter Username: ";
cin >> username;
//prompting user for password
cout << "Enter Password : ";
cin >> password;
//Checking if user's entered credentials are equal to actual USERNAME and PASSWORD
if(username == USERNAME && password == new_password)
{
cout << "Access is Granted! :-) :-) :-)\nPress any key to continue..." << endl;
}
else
{
cout<<"Invalid credentials, programs terminates after 3 valid attemps,"<<'\n';
cout << "You have " << fail-1 << " tries left.\n" << endl;
fail--;
login_finished = true; // don't let them try again
}
if(username == USERNAME && password == saved_password)
{
cout << "User credentials are correct!!!" << endl;
cout << "Successful first login! Please change your password!!!\n";
}
else
{
cout<<"Invalid credentials, programs terminates after 3 valid attemps,"<<'\n';
cout << "You have " << fail-1 << " tries left.\n" << endl;
fail--;
login_finished = true; // don't let them try again
}
//while !finished && fail > 0
// if the fail count is > 0 then the user successfully entered a good password
if (fail > 0)
{
// if the password they used is the default, we need them to create a new one
if (password == "1234")
{
finished = false;
while (!finished) {
cout << "New password must be 4-digit and different from your temporary password: ";
cin >> password;
if (password != "1234" && password.length() == 4) { // they entered something new
cout << "Please confirm your password again: ";
cin >> new_password;
if (password == new_password) { // they entered the same one twice- good
finished = true;
saved_password = password;
cout << "You have changed your password successfully!!\nPlease re-login\n";
}
else {
cout << "Please try again\n";
}
} // if new password
} // while !finished
} // if default password
else {
login_finished = true; // no need to re-enter password
}
} // if fail > 0
finished = false;
}
} // while !login_finished
return 0;
}
|