Having trouble with classes

I can't get my code to compile, i need to read in lines from a file and store them in variables. Then i have to construct instances of my class for how many lines there are in the file and take those variables into them.

I'm getting this error :

"a2.cpp:40: error: cannot convert `Employee' to `Employee*' in assignment"

Please help.

#include<iostream>
#include<string>
#include<fstream>

void displayInfo();

using namespace std;

class Employee{
public:
Employee();
Employee(string n, string i, double s){
name=n;
id=i;
salary=s;
}

void print(double a, double b){
if((salary>a)&&(salary<b)){
cout<<name<<" "<<id<<" "<<salary<<endl;
}
}
private:
string name;
string id;
double salary;
};

int main(){

displayInfo();

string nameIn;
string idIn;
char salaryTest[80];
double salaryIn=0;
Employee *p;
p = Employee(nameIn,idIn,salaryIn);
int total=0, i=0;

ifstream inFile;
inFile.open("a2.txt");
if(!inFile){
cout << "File not found" << endl;
exit(1);
}

while(inFile){
getline(inFile,nameIn,' ');
getline(inFile,idIn,' ');
inFile.getline(salaryTest,' ');

salaryIn = atof(salaryTest);

p[i]=Employee(nameIn, idIn, salaryIn);
total++;
i++;
}
inFile.close();
cout<<total<<" Records Read"<<endl;

char repeat='y';
double low,high;

while(repeat!='n'){
cout<<"Enter low salary: ";
cin>>low;
cout<<endl;
cout<<"Enter high salary: ";
cin>>high;
cout<<endl;

p[i].print(low,high);

cout/*records found*/<<" matching employees found"<<endl;
cout<<endl;

cout<<"Repeat (y/n)? ";
cin>>repeat;
}
}

closed account (28poGNh0)
first : you declared a pointer to an object class(in your case P) P should take the address of another object like p = &object;
In you case you should use the new operator

p = new Employee(nameIn,idIn,salaryIn);

dont forget to delete what did you toke

also atof and exit requared the include of <cstdlib>

Finaly Welcom to this website
next time when you want to print some code use code tags or tags your code between
[code]code[/code]
Last edited on
Topic archived. No new replies allowed.