problem with c++ class implementation
Apr 24, 2008 at 2:58am UTC
i have this code and I am getting an error in main () at masterInfo x; that says "conversion from 'int' to non-scalar type 'masterInfo' requested" any suggestions on why this is happening would be appreciated. Thanks
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
//******************************************************************
// Assignment 5
// Programmer: Christy Sylvest
// Completed : 04/16/08
// Status : Complete
//
//******************************************************************
#include <iostream> // input/output declarations
#include <iomanip> // i/o manupulator declarations
#include <fstream> //output file declarations
#include <string>
using namespace std;
using std::ofstream;
using std::ios;
using std::string;
class masterInfo
{
private :
int id;
string name;
double payrate;
int numDep;
int empType;
public :
void getid ();
void getname ();
void getpayrate();
void getnumDep();
void getempType();
};
void masterInfo::getid()
{
cin >> id;
}
void masterInfo::getname()
{
cin >> name;
}
void masterInfo::getpayrate()
{
cin >> payrate;
}
void masterInfo::getnumDep()
{
cin >> numDep;
}
void masterInfo::getempType()
{
cin >> empType;
}
int main ()
{
int empNum = 1;
masterInfo x;
int first, second, third, fourth, fifth;
masterInfo timeSheet [5] = {first, second, third, fourth, fifth};
//open outputfile
ofstream fout("payroll_report.txt" );
if (!fout)
{
cout << "file not open" ;
}
for (int empNum = 0; empNum < 5; empNum++)
{
cout << "Enter information for employee " << empNum << endl;
cout <<"Employee id: " <<;
timesheet[empNum].getid();
cout <<endl;
cout <<"Employee name: " <<;
timesheet[empNum].getname ();
cout <<endl;
cout <<"Pay rate: " <<;
timesheet[empNum].getpayrate ();
cout <<endl;
cout <<"Dependents: " <<;
timesheet[empNum].getnumDep ();
cout <<endl;
cout <<"Type: " <<;
timesheet[empNum].getempType ();
cout <<endl;
}
system ("PAUSE" );
return 0;
}
Last edited on Apr 24, 2008 at 2:59am UTC
Apr 24, 2008 at 3:11am UTC
Your error is here, I think:
1 2
int first, second, third, fourth, fifth;
masterInfo timeSheet [5] = {first, second, third, fourth, fifth};
Notice the fact that 'first', 'second', etc. are of type int and you're declaring an array of type masterInfo? It is trying to convert values of type int into values of type masterInfo. You would need a constructor in the masterInfo class that takes an int as a parameter and stores it in whatever variable in masterInfo class it needs to be stored in.
Apr 24, 2008 at 4:42am UTC
ok if I change the int to masterInfo I now get an error that says timeSheet is undeclared...aren't i declaring it an array of the masterInfo class? or do i need to do array timesheet[5] = {first.....}?
1 2 3 4 5 6 7 8
int main ()
{
int empNum = 1;
masterInfo x;
masterInfo first, second, third, fourth, fifth;
masterInfo timeSheet [5] = {first, second, third, fourth, fifth};
Apr 24, 2008 at 4:56am UTC
How is this even compiling? I don't think it should be.
cout <<"Employee id: " <<;
You misspelled timeSheet here. That's why it is saying it's undeclared.
timesheet[empNum].getid();
Apr 24, 2008 at 5:25am UTC
well everything is compiling now, but when i run the program it is not accepting the spaces in my string...I know regularly i can do getline(cin, name); but I am not sure how to do that with the class and array stuff?
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
//******************************************************************
// Assignment 5
// Programmer: Christy Sylvest
// Completed : 04/16/08
// Status : Complete
//
//******************************************************************
#include <iostream> // input/output declarations
#include <iomanip> // i/o manupulator declarations
#include <fstream> //output file declarations
#include <string>
using namespace std;
using std::ofstream;
using std::ios;
using std::string;
class masterInfo
{
private :
int id;
string name;
double payrate;
int numDep;
int empType;
public :
void getid ();
void getname ();
void getpayrate();
void getnumDep();
void getempType();
};
void masterInfo::getid()
{
cin >> id;
}
void masterInfo::getname()
{
cin >> (cin, name);
}
void masterInfo::getpayrate()
{
cin >> payrate;
}
void masterInfo::getnumDep()
{
cin >> numDep;
}
void masterInfo::getempType()
{
cin >> empType;
}
int main ()
{
int empNum = 1;
masterInfo x;
masterInfo first, second, third, fourth, fifth;
masterInfo timeSheet [5] = {first, second, third, fourth, fifth};
//open outputfile
ofstream fout("payroll_report.txt" );
if (!fout)
{
cout << "file not open" ;
}
for (int empNum = 1; empNum < 5; empNum++)
{
cout << "Enter information for employee " << empNum << endl;
cout <<"Employee id: " ;
timeSheet[empNum].getid();
cout <<endl;
cout <<"Employee name: " ;
timeSheet[empNum].getname();
cout <<endl;
cout <<"Pay rate: " ;
timeSheet[empNum].getpayrate();
cout <<endl;
cout <<"Dependents: " ;
timeSheet[empNum].getnumDep();
cout <<endl;
cout <<"Type: " ;
timeSheet[empNum].getempType();
cout <<endl;
}
system ("PAUSE" );
return 0;
}
Apr 24, 2008 at 2:16pm UTC
You would just use
cin >> name;
rather than
cin >> (cin, name);
Last edited on Apr 24, 2008 at 2:17pm UTC
Topic archived. No new replies allowed.