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.

Recommended Answers

All 3 Replies

Now, I am not a procrastinator by any means. I was sick in bed the whole past week and have missed out on quite a lot of lectures. Please, I just thought I would throw that in.

Why are you trying to execute those commands in a class definition and not in a function, or in main itself? You also did not declare an object of PasswordSystem.

What is

PasswordSystem.Shazam();

???

You have no static function called Shazam to execute.

Sorry, how would one write it so the code runs in said function or main rather than what I did? I thought the place where I wrote shazam was an arbitrary thing ( dont know the actual of it ) where you can name it anything you wish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.