After a HDD wipeout, I lost my completed .cpp files for program 2 which I already have done creating a function out of the following code. The only problem is, for the last few days I have been failing to recreate or replicate what I previously have done a few weeks ago. Daniwebbers, how can I create a function out of this?
// This program will accept user ID and password from user and match
// with one on file lab2.data If pass and user match then program
// will display a success message.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main ()
{
string user;
string password;
// Read username and pass from data file
ifstream dataFile("lab2.data");
dataFile >> user >> password; //hopefully this will parse white
dataFile.close();
// Get username and pass from user
string user1;
string password1;
cout << "Please enter your username" << endl;
getline (cin, user1);
cout << "Please enter your password" << "\n";
getline (cin, password1);
// Now we compare user/password with one from file
if ((user1 == user) && (password1 == password))
cout << "Success, you are signed in!" << endl;
else
cout << "Username and/or password incorrect. Try again." <<
"\n";
return 0;
}
here is what I have come up with so far, scratching my brains out trying to remember what i did before.
// This program will accept user ID and password from user and match
// with one on file lab2.data If pass and user match then program
// will display a success message.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class PasswordSystem
{
private:
string user;
string password;
// Read username and pass from data file
ifstream dataFile("lab2.data");
dataFile >> user >> password; //hopefully this will parse white
dataFile.close();
// Get username and pass from user
string user1;
string password1;
cout << "Please enter your username" << endl;
getline (cin, user1);
cout << "Please enter your password" << "\n";
getline (cin, password1);
// Now we compare user/password with one from file
if ((user1 == user) && (password1 == password))
cout << "Success, you are signed in!" << endl;
else
cout << "Username and/or password incorrect. Try again." <<
"\n";
};
int main ()
{
PasswordSystem.Shazam();
return 0;
}
I know I am missing a lot of lines here and there but I cannot remember how to do it again. Please help, I am really in a hurry and the assignment is due in a few hours. Because once I get how to do classes, I can add constructors in which I was hoping you all can also help me out with.