Hi there, I have my code that I have been working on for a week straight and all the research I've done has lead me here to see if I can get some much needed help on this.
Assignment is:
1. Create a program to verify a user name and password given by a user
2. Create a text file named "correctData.txt" (You will NOT upload this file during submission). Place a username on line 1 in the txt file and a password on line 2 in the file. This will be used for testing.
3. Create the following functions and call them starting from main:
void Login () - This function is responsible for displaying the prompts asking for user name and password. It will only allow a user to attempt a password 3 times for security measures before returning back to main. You will need to use a loop to accomplish this task.
bool CheckCredentials (string username, string password)- In order to validate the username and password. This is a boolean return and will return true or false from this function if the data verified does not match the file.
You will need to use fstream to create an instance of a file using ifstream. Compare the strings to the data in the text file. Remember, the username will be on line 1 in the text file and the password will be on line 2.
Remember:
Close the fstream file before returning 0
Prototype functions.
Don't use global variables
Here is what I have:
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
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void Login(string, string);
bool CheckCredentials(string userName, string passWord);
int main()
{
ifstream inFile;
string userName;
string passWord;
string u;
string p;
inFile.open("CorrectData.txt");
Login(userName, passWord);
CheckCredentials(userName, passWord);
inFile.close();
return 0;
}
void Login(string u,string p)
{
ifstream inFile;
inFile.open("CorrectData.txt");
cout << "Login" << endl;
cout << "\nUsername: ";
cin >> u;
cout << "Password: ";
cin >> p;
for (int count = 0; count <= 3; count++)
if (count ==3)
{
cout << "\nYou have " << count << " remaining.\n";
}
else
{
cout << "\nAttempts exceeded.";
}
}
bool CheckCredentials(string userName, string passWord)
{
bool status;
string u, p;
ifstream inFile;
inFile.open("CorrectData.txt");
if (userName == u && passWord == p)
{
status = true;
cout << "\nLogin sucessful!" << endl;
}
else (!status);
{
cout << "\nInvalid username or password. " << endl;
Login(userName, passWord);
inFile.close();
return status;
}
}
|
my txt file is:
mynava
1234abc
I get the same result (below) regardless if the username and password are correct or not.
Login
Username: wrong
Password: kljadlskjf
Attempts exceeded.
Attempts exceeded.
Attempts exceeded.
You have 3 remaining.
Login sucessful!
Invalid username or password.
Login
Username: kaljsd
Password: jdjdj
Attempts exceeded.
Attempts exceeded.
Attempts exceeded.
You have 3 remaining.
Press any key to continue . . .
|