- #1
Nusc
- 760
- 2
C++
I'm supposed to read in the file:
Joe Bloggs 10 June 2002 21 Sept 2004 48524
Jane Doe 6 Feb 2004 0 nomonth 0 52595
#include <string>
#include <fstream>
#include <vector>
#include <iostream>
using namespace std;
//user-defined types
struct datetype
{
int day;
string month;
int year;
};
struct Employee
{
string first;
string last;
datetype join;
datetype left;
int salary;
};
// prototypes
void GetData(vector <Employee>&a, string filename);
void AddEmployee(vector<Employee>&a);
void ListCurrent(const vector <Employee>&a);
int main();
//I get the following compiler error
//emp.cc:30: error: expected unqualified-id before '{' token
//emp.cc:30: error: expected `,' or `;' before '{' token
{ // <------------ on this line
string filename;
vector <Employee> a;
cout <<"Please enter your filename:"<< endl;
cin >> filename;
GetData(a,filename);
char choice;
do
{
cout <<"What do you wish to do with the database?"<<endl;
cout <<"Input the relevant letter: Options are:"<< endl;
cout <<"(a) Add an employee to the file" << endl;
cout <<"(b) List the current employees"<< endl;
cout <<"(c) Quit the program"<< endl;
cin >> choice;
cout <<"You have entered:"<< choice << endl;
switch(choice)
{
case('a'):
AddEmployee(a);
break;
case('b'):
ListCurrent(a);
break;
case('c'):
{
cout <<"Program ending"<< endl;
}
}
}while(choice != 'c');
return 0;
}
//I'm supposed to read in a file but I'm not sure whether I use struct or a vector after the while loop.
void GetData(vector <Employee>&a,string filename)
{
ifstream infile(filename.c_str());
if(infile.fail())
{
cout << "Could not find existing file."<< endl;
exit(0);
}
while(!infile.eof());
infile >>
infile >>
infile >>
infile >>
infile >>
infile >>
}
a.push_back( ? )
infile.close();
}
}
void AddEmployee(vector<Employee>&a)
{
Employee b;
cout <<"Please enter your employee's first and last name:" << endl;
cin >> b.first >> b.last;
cout <<"Please enter your employee's joining date and leaving date (day month year)" << endl;
cin >> b.join.day >> b.join.month >> b.join.year >> b.left.day >> b.left.month >> b.left.year;
cout <<"Please enter your employee's salary:" << endl;
cin >> b.salary;
a.push_back(b);
cout <<"New employee successfully added." << endl;
}
//This function is supposed to list the current employees rather than the list of employees but I don't know how to do that.
void ListCurrent(const vector <Employee>&a)
{
cout <<"Here is a list of names of your employees" << endl;
for (int i = 0; i<a.size();i++)
{
cout<<a.at(i).last<<", "<<a.at(i).first<< endl;
}
}
Can anyone please help me?
I'm supposed to read in the file:
Joe Bloggs 10 June 2002 21 Sept 2004 48524
Jane Doe 6 Feb 2004 0 nomonth 0 52595
#include <string>
#include <fstream>
#include <vector>
#include <iostream>
using namespace std;
//user-defined types
struct datetype
{
int day;
string month;
int year;
};
struct Employee
{
string first;
string last;
datetype join;
datetype left;
int salary;
};
// prototypes
void GetData(vector <Employee>&a, string filename);
void AddEmployee(vector<Employee>&a);
void ListCurrent(const vector <Employee>&a);
int main();
//I get the following compiler error
//emp.cc:30: error: expected unqualified-id before '{' token
//emp.cc:30: error: expected `,' or `;' before '{' token
{ // <------------ on this line
string filename;
vector <Employee> a;
cout <<"Please enter your filename:"<< endl;
cin >> filename;
GetData(a,filename);
char choice;
do
{
cout <<"What do you wish to do with the database?"<<endl;
cout <<"Input the relevant letter: Options are:"<< endl;
cout <<"(a) Add an employee to the file" << endl;
cout <<"(b) List the current employees"<< endl;
cout <<"(c) Quit the program"<< endl;
cin >> choice;
cout <<"You have entered:"<< choice << endl;
switch(choice)
{
case('a'):
AddEmployee(a);
break;
case('b'):
ListCurrent(a);
break;
case('c'):
{
cout <<"Program ending"<< endl;
}
}
}while(choice != 'c');
return 0;
}
//I'm supposed to read in a file but I'm not sure whether I use struct or a vector after the while loop.
void GetData(vector <Employee>&a,string filename)
{
ifstream infile(filename.c_str());
if(infile.fail())
{
cout << "Could not find existing file."<< endl;
exit(0);
}
while(!infile.eof());
infile >>
infile >>
infile >>
infile >>
infile >>
infile >>
}
a.push_back( ? )
infile.close();
}
}
void AddEmployee(vector<Employee>&a)
{
Employee b;
cout <<"Please enter your employee's first and last name:" << endl;
cin >> b.first >> b.last;
cout <<"Please enter your employee's joining date and leaving date (day month year)" << endl;
cin >> b.join.day >> b.join.month >> b.join.year >> b.left.day >> b.left.month >> b.left.year;
cout <<"Please enter your employee's salary:" << endl;
cin >> b.salary;
a.push_back(b);
cout <<"New employee successfully added." << endl;
}
//This function is supposed to list the current employees rather than the list of employees but I don't know how to do that.
void ListCurrent(const vector <Employee>&a)
{
cout <<"Here is a list of names of your employees" << endl;
for (int i = 0; i<a.size();i++)
{
cout<<a.at(i).last<<", "<<a.at(i).first<< endl;
}
}
Can anyone please help me?
Last edited: