Infiling into classes with multiple variables

I'm Creating a program that I agreed to do and now I think I am in over my head. The code below is only a header file for a retail store register. This portion of the code will assign the employee's name last name, etc. from an text file. That's the first thing that I can't figure out. The other thing is how to verify that both the employee ID and Password match. if they do match then the employee will have access to the register if not i plan on using a "do while loop" until correct id and passwords are entered. I'm not expecting to get the code done for me, just to be pointed in the right direction. Anything is appreciated. Thanks.

class Employee
{
private:
string Name;
string Lname;
string position;
int Empid;
int Pword;

public:
void setName(string);
void setLname(string);
void setPosition(string);
void setEmpid(int);
void setPword(int);

void getName();
void getLname();
void getPosition();
void getEmpid();
void getPword();

};
Using your above class, you'd have to read from a file like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string Name, Lname, position;
int Empid, Pword;
vector<Employee> myEmployees;
Employee temp;

ifstream inFile("employeeDatabase.txt");
while (inFile.good()){
   inFile >> Name >> Lname >> position >> Empid >> Pword;
   temp.setName(Name);
   temp.setLname(Lname);
   temp.setPosition(position);
   temp.setEmpid(Empid);
   temp.setPword(Pword);
   myEmployees.push_back(temp);
}


Obviously, that's really rough and I just used the variable names you already had, but you can change it to whatever you want.

If you're stuck using that class, you don't really have many other options. However, I'd suggest making the member variables public so you can do something like this:
1
2
3
4
5
6
7
8
9
vector<Employee> myEmployees;
Employee temp;

ifstream inFile("employeeDatabase.txt");
while (inFile.good()){
   inFile >> temp. Name >> temp.Lname
          >> temp.position >> temp.Empid >> temp.Pword;
   myEmployees.push_back(temp);
}


Same thing, a lot less code. The getter/setter methods are fine for learning about classes, but not exactly practical in real world uses.
Thanks a lot. I actually figured out a different way to do it. The problem I've been facing is that I can only use the information taught in the class. I've picked up different ways of doing it but seldom from the actual class. Ironic. Below I will print what I've come up with. Again I do appreciate the help.
ifstream infile;
infile.open("empID.txt");
if(!infile)
{cout<<"Cannot Find empID.txt"<<endl;}

string Name;
string Lname;
string position;
int Empid;
int Pword;

Employee employee[9];
for(int x=0; x<9; x++)
{
infile>>Name>>Lname>>position>>Empid>>Pword;
employee[x].setAll(Name, Lname, position, Empid, Pword);
}

This will set the data in the text file to the appropriate variable. Now the next thing is:
how do i verify if the employee number and password entered by the user are valid in the int main()?
If this is a separate function, there is a few things to think about.

1st
Your employee database, I assume, should be able to accessed throughout your entire program. If you were taught how to pass parameters, I suggest using them, otherwise, you'll have to use global variables *cringe*

2nd
Each function should do exactly what it was designed to do and nothing else. For example, ReadEmployees() should read the employees from the file, nothing else. CheckPassword() should only check the password. GetUserPassword() should only ask the user for their password.

3rd
The problem with your above function is that if the file couldn't be opened, you still attempt to read from it. In your if statement, I suggest adding the line return; so the function will exit and go back to the previous function.
Can you add member functions to the class? If so, why not add a member function to it to read into the data members directly instead of using the setters? That way you can create an object and pass it (and the ifstream object) to the read function. You could do this as many time as you wanted in a row.

I'm not as sanguine about making the data members public however. The whole point of classes is hiding data and using the public interface to manipulate it. Sure, it'll work, and for such a small program data hiding isn't that important. But still... it feels more like a hack to me. :)
Last edited on
The program is actually gonna be a bit more complicated. I am supposed to design a POS system. I have about 10 different classes each in different header files. I also will have to ofstream each time a customer purchases something to a general text file. Each employee will have their own text file which will hold their personal sales. once the employee logs in successfully then it will tell the employee to choose from one of the categories Example Desktops which is a class. when the category is chosen then the employee will choose a item from the category Example Gamer series. this will then prompt the employee to ask the customer if he/she wants services or accessories, if yes then it will be added to the total.
I have to do all this by tomorrow. the program i had been designing for a month was an RPG but another student already presented it and the professor will give more points for creativity so this is where i'm stuck. I've already stocked up with redbull and energy shots so i can pull an all nighter. To make things worse i have a calculus exam in the morning. fml
Message me and I'll help you. Also, if you don't have it, get Skype.
//I can't figure out why my program won't read the text file. is it because i am //using express version of C++? I had to manually create a text file using //notepad then drag and drop into the resources folder in my program.

//This is my Main.

#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
//All my HeaderFiles
#include"empID.h"
#include"Camcorder.h"
#include"DCam.h"
#include"Desktop.h"
#include"DVD.h"
#include"Laptop.h"
#include"PSTree.h"
#include"Speakers.h"
#include"TV.h"
using namespace std;



int main()//Grading rubric includes Sorting, Searching
{
ifstream infile;
infile.open("empID.txt");
if(!infile)
{cout<<"Cannot Find empID.txt"<<endl;}

string Name;
string Lname;
string position;
int Empid;
int Pword;

Employee employee[9];
for(int x=0; x<9; x++)
{
infile>>Name>>Lname>>position>>Empid>>Pword;
employee[x].setAll(Name, Lname, position, Empid, Pword);
}


for(int x=0; x<9; x++)
{
employee[x].displayEmployee();

}





system("pause");
return 0;
}

//this is my Empid.h header file
#include<iostream>
#include<string>
using namespace std;

class Employee
{
private:
string Name;
string Lname;
string position;
int Empid;
int Pword;

public:
void setName(string n){Name=n;};
void setAll(string n, string l, string p, int i, int pw)
{
Name=n;
Lname=l;
position=p;
Empid=i;
Pword=pw;
}

void displayEmployee()
{
cout<<"Name: "<<Name<<" "<<Lname<<endl;
cout<<"position: "<<position<<endl;
cout<<"employee id: "<<Empid<<endl;
cout<<"password: "<<Pword<<endl;

}
string getName(){return Name;}
string getLname(){return Lname;}
string getPosition(){return position;}
int getEmpid(){return Empid;}
int getPword(){return Pword;}
};



//the data in the text file is

Rudy Hernandez Owner 1 11
Simon Says Manager 2 22
Little John Motivater 3 33
Homer Simpson SafetyMan 4 44
Peter Griffin CEO 5 55
Taz Devil Security 6 66
Chavo Ocho Greeter 7 77
Donny Darko 8 88
Aunt Jemima 9 99
When running applications with MSVC++, the text file must reside in the same location as main.cpp, or whatever you called the main application file. If it's not, either move it there, or change the file name to the absolute path of the file. The former is much easier to do.
This is a function that will compare the employee IDs stored in the class to the employee ID entered. I can't figure out how to get just the employee ID from the class. What I noticed is that my programming is going to be repetitive, so every time I figure out how to do something I can apply it through out my program. I'm going to copy and paste the solution to this problem to the function which will verify the Password. I will bother you as little as I can.

void VerifyEmpId()
{
int empid;//
cout<<"Please enter Employee ID: ";
cin>>empid;

Employee employee[9];

for(int x=0; x<9; x++)
{
if(empid==Empid)//
{
break;
}
else{}

}
}
I don't mind helping, that's why I told you to message me. However, I told you before, you need a way to access your employee database, the one read from the file. Your issue here is that you're creating a new database in each function. Essentially, you're just creating all new employees with each function so comparing them won't do you any good to begin with.

However, once you figure out how to pass the database to each function, it's simple, you can just change Empid to employee[i].getEmpid();.
THE PROBLEM I'M HAVING WITH THIS ONE:

"1>c:\users\owner\documents\visual studio 2010\projects\finalproject\finalproject\project.cpp(217): error C2512: 'Desktop' : no appropriate default constructor available


//THIS IS MY CLASS HEADER

#include<iostream>
#include<string>
using namespace std;

class Desktop
{
private:
string dType;
double dPrice;
double dAcc;
double dWarranty1;
double dWarranty2;
double dUpgrade;

public:
Desktop(string type){dType=type;}
void setAll(string dt, double dp, double da, double dw1, double dw2, double du)
{
dType=dt;
dPrice=dp;
dAcc=da;
dWarranty1=dw1;
dWarranty2=dw2;
dUpgrade=du;
}

void displayDesktop()
{
cout<<"Type: "<<dType;
cout<<"Price: $"<<dPrice<<endl;
cout<<"Accessory: $"<<dAcc<<endl;
cout<<"2 Year Warranty: $"<<dWarranty1<<endl;
cout<<"4 Year Warranty: $"<<dWarranty2<<endl;
cout<<"Would You Like to Upgrade your Camcorder for: $"<<dUpgrade<<endl;
}

string getdType(){return dType;}
double getdPrice(){return dPrice;}
double getdAcc(){return dAcc;}
double getdWarr1(){return dWarranty1;}
double getdWarr2(){return dWarranty2;}
double getdUpGrade(){return dUpgrade;}
};

//THIS IS MY FUNCTION **************************
void ZonyDesktops()
{
ifstream infile3;
infile3.open("ZonyDesktops.txt");

if(!infile3)
{cout<<"Cannot Find ZonyDesktops.txt"<<endl;}
else
{
string dType;
double dPrice;
double dAcc;
double dWarranty1;
double dWarranty2;
double dUpgrade1;

Desktop desktop[4];
for(int x=0; x<4; x++)
{
infile3>>dType>>dPrice>>dAcc>>dWarranty1>>dWarranty2>>dUpgrade1;
desktop[x].setAll(dType, dPrice, dAcc, dWarranty1, dWarranty2, dUpgrade1);

}
}}
Nevermind, I saw my error. Thanks
Topic archived. No new replies allowed.