Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in C++ 1. Download the input file, employees.dat, from my public directory. Whil

ID: 3777769 • Letter: I

Question

in C++

1. Download the input file, employees.dat, from my public directory. While you are in your own cs111

directory, use the copy command (cp).

[smith001@empress cs111] cp /cs/slott/cs111/employees.dat .
Copying into the current directory

Assume this input file contains an unknown number of lines/employees. Each line contains the information about an employee - first name, last name, gender, hourly rate, employee id and age. Assume the number of lines/employees never exceeds 100.

Ada Agusta F 10.00 19569 28

Issac Asimov M 18.25 63948 58

:

:

2. Create a struct called employee.

3. In the main, create 2 arrays of the struct employee type called mAr and fAr. mAr will have all the male

employees and fAr will have all the female employees.

use const for the size(100) for each array.

Make a function called readData that will fill mAr and fAr from the input file. This function should return the number of male employees and also the number of female employees to the caller. Assume you don’t know how many employees you have (use the “while” loop). Declare the input file and open and close it within this function.

hint:

??? readData( ??? )

{

mi = 0; //the index to the male array

fi = 0; //the index to the female array

declare the input file
open the input file
if it doesn’t exist, display an error message and terminate the program
if it exists, read each employee from the file and store it into the appropriate array depending on the gender. Keep counting how many male and female employees you have.

employee temp; //make an employee called temp //fill temp with the first employee
fin >> temp.fName;
fin >> temp.lName;

fin >> temp.gender;

:

while(fin && there is still room for both female and male arrays)

{

if(temp.gender == ‘F’)
fAr[???] = temp; //copy all the fields from temp to the female array

:

read in the next employee into temp }

close the input file.

}

5. Call readData() in the main and display the numbers of male and female employees. Run your program now and make sure you get 11 for males and 5 for females.

Sample Run
There are 11 male and 5 female employees.

THESE IS IN employee.dat

Ada Agusta F 10.00 19569 28

Issac Asimov M 18.25 63948 58

Humphry Bogart M 20.00 48482 56

Albert Einstein M 11.10 47474 67

Emmylou Harris F 33.50 72647 38

James Kirk M 18.85 82828 46

Ted Kopple M 22.25 37376 48

David Letterman M 15.50 19338 47

Stevis Nicks F 18.85 23459 38

Monty Python M 33.35 80939 44

Roger Rabbit M 15.00 91343 24

Sally Ride F 25.50 91123 30

Rod Serling M 55.50 93939 56

Luke Skywalker M 19.95 12343 35

Kit Ross F 11.00 20000 21

Mike Smith M 23.35 10000 30

Explanation / Answer

SOURCE CODE:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <stdexcept>

using namespace std;

const int size = 100;

typedef struct employee
{
string firstname;
string lastname;
string gender;
string hourlyRate;
string employeeid;
string age;
};

struct employee *mAr = new employee[size];
struct employee *fAr = new employee[size];

int mi=0;
int fi=0;

void readData()
{


   string sLine = "";
   ifstream infile;
   struct employee temp;      
   string word="";
   int i=0,j=0,h=0;
   infile.open("employee.dat");

   while (!infile.eof())
   {
       getline(infile, sLine);
      
       string t="";
       int count = 0;
       for(i=0;i<sLine.length();i++)
       {
           if(sLine.substr(i,1)==" ")
           {
               count++;
           }
           else
           {
               if(count > 0)
                   t = t + " "+ sLine.substr(i,1);
               else
                   t = t + sLine.substr(i,1);
               count = 0;
           }
       }
       t = t + " "+ sLine.substr(i,sLine.length()-i);
      
       sLine = t;

       for(int i=0;i<sLine.length();i++)
       {

           if(sLine.substr(i,1)==" ")
           {
               switch(j)
               {
                   case 0:
                       temp.firstname = word;
                       j++;
                       break;
                   case 1:
                       temp.lastname = word;
                       j++;
                       break;
                   case 2:
                       temp.gender = word;
                       j++;
                       if(word=="M")
                           mi++;
                       else if(word=="F")
                           fi++;
                       break;
                   case 3:
                       temp.hourlyRate = word;
                       j++;
                       break;
                   case 4:
                       temp.employeeid = word;
                       j++;
                       break;
                   case 5:
                       temp.age = word;
                       j=0;
                       break;

               }
               word="";
           }
           else
           {
               word = word + sLine.substr(i,1);
           }
          
       }
      
       word = "";
          
   }

   infile.close();

}

  
int main()
{
   readData();
cout<<"There are "<< mi <<" male and "<< fi <<" female employees." ;
return 0;
}

OUTPUT:

There are 11 male and 5 female employees.