2

Jason Chimera SE 7039990101

Mike Knuble SSE 7039990102

Karl Alzner JSE 7039990202

Tom Poti SE 7039990203

Alex Ovechkin EGM 7039990001

I am trying to read in the information above from a file and sort it by last name. How can I fix this error:

Error:expected a ':'

for the strings I declared in the EmployeeInformation class?

 class EmployeeInformation {
  public string firstName;
  public string lastName;
  public string title;
  public string phonenumber;

EmployeeInformation(&firstName, &lastName, &title, &phonenumber)
{
    this->firstName = firstName;
    this->lastName = lastName;
    this->initials = title;
    this->id = phoneumber;
}
};


 #include <vector>
 #include <algorithm>
 #include <iostream>
 #include <fstream>

 using namespace std;

int main(int argc, char**argv) {

ifstream file;
file.open("employees.txt");

if (!file.is_open())
    cout << "Error opening file";

vector<EmployeeInformation> employees;
while (!file.eof())
{

    string firstName, lastName, title, phonenumber;
    file >> firstName >> lastName >> title >> phonenumber;
    EmployeeInformation person(firstName, lastName, title, phonenumber);
    employees.push_back(person);
    // end file
}
sort(employees.begin(), employees.end(), [](EmployeeInformation a, EmployeeInformation b) { return a.lastName < b.lastName });

for (EmployeeInformation i : employees)
    // print person info in sorted order

return 0;

}

Noor Chaudhry
  • 73
  • 1
  • 3
  • 7

4 Answers4

7

Lot of minor errors.

Public section.

In C++ public is followed by ':' to mark a section. Also you need to include the string definition. Which lives in the std:: namespace.

  public: std::string firstName;
  public: std::string lastName;
  public: std::string title;
  public: std::string phonenumber;

C++ is a very pedantic on types.

You need to specify the types of all parameters:

EmployeeInformation(string const& firstName, string const & lastName, string const& title, string const& phonenumber)

unknown names:

this->initials = title;
  //  ^^^^^^^^   Not a member of this class. Did you mean title.
this->id = phoneumber;
  //  ^^         Not a member of this class. Did you mean phoneumber

Statements are terminated by ';'

return a.lastName < b.lastName
                       //      ^^^^^   missing here.

Once you have those fixed it compiles. But I would take the fixed version to code review for a more detailed break down.

When all complete main looks like this:

int main(int argc, char**argv)
{
    std::ifstream file("employees.txt");

    // Read data into vector
    std::vector<EmployeeInformation> employees(std::istream_iterator<EmployeeInformation>(file),
                                               (std::istream_iterator<EmployeeInformation>()));

    // Sort container.
    std::sort(std::begin(employees), std::end(employees),
              [](EmployeeInformation const& lhs, EmployeeInformation const& rhs) { return lhs.lastName < rhs.lastName;});

    // Copy container to stream.
    std::copy(std::begin(employees), std::end(employees),
              std::ostream_iterator<EmployeeInformation>(std::cout));
}
Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562
4

Your class is not declared correctly. For instance, you cannot omit data types from the constructor's parameters.

You are also not reading the file correctly.

Try something more like this:

#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

class EmployeeInformation
{
public:
    string firstName;
    string lastName;
    string title;
    string phoneNumber;

    EmployeeInformation(const string &_firstName, const string &_lastName, const string &_title, const string &_phonenumber) :
        firstName(_firstName),
        lastName(_lastName),
        title(_title),
        phoneNumber(_phoneNumber)
    {
    }
};

int main(int argc, char**argv)
{
    ifstream file;

    file.open("employees.txt");
    if (!file.is_open())
    {
        cout << "Error opening file";
        return 0;
    }

    vector<EmployeeInformation> employees;
    string line;

    while (getline(file, line))
    {
        string firstName, lastName, title, phonenumber;
        istringstream iss(line);
        iss >> firstName >> lastName >> title >> phonenumber;
        EmployeeInformation person(firstName, lastName, title, phonenumber);
        employees.push_back(person);
    }

    sort(employees.begin(), employees.end(), [](const EmployeeInformation &a, const EmployeeInformation &b) { return a.lastName < b.lastName; });

    for (EmployeeInformation i : employees)
    {
        // print person info in sorted order
        cout << i.lastName << " " << i.firstName << " " << i.title << " " i.phoneNumber << endl;
    }

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • The same way you print out anything else - pass the desired values to `std::cout` as needed. I updated my answer to show that. – Remy Lebeau May 13 '15 at 21:59
3

Your syntax for access modifiers is incorrect (you're using Java style rather than C++). In C++ you would declare this fully public class like this:

 class EmployeeInformation {
 public:
   string firstName;
   string lastName;
   string title;
   string phonenumber;
   //...
};

If you really do want to make all the data public, you could also use a struct instead of a class, since the members of structs are public by default.

atkins
  • 1,963
  • 14
  • 27
2

First of all your variable declaration in class is wrong.Then in the constructor,you are not telling the data types.See this

 class EmployeeInformation {
public:
string firstName;
string lastName;
string title;
string phonenumber;

EmployeeInformation(string &firstName,string &lastName,string &title,string &phonenumber)
{
this->firstName = firstName;
this->lastName = lastName;
this->title = title;            //there is no variable named initial in class
this->phonenumber = phonenumber; //there is no variable named id in class
}
};

Then you are missing a semicolon.

sort(employees.begin(), employees.end(), [](EmployeeInformation a, EmployeeInformation b) { return a.lastName < b.lastName; });  //there should be a semicolon after return statement
Gaurav Sehgal
  • 7,422
  • 2
  • 18
  • 34