Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Using a loop to create an object and initialize object with file...

$
0
0
I have a pre-lab assignment and I just want to confirm whether I've done this right or not and if I haven't I would appreciate tips and advice on how to fix it here is all we were given for the pre-lab:
#include <iostream>
#include <fstream>
using namespace std;
#include "Worker.h"

int main()
{
   ifstream infile;
   int records;
 // declare required variables here.
   Worker wrkr;
   int index;
   const int MAX_EMPLOYEES = 10;
   Worker employees[MAX_EMPLOYEES];

	
   // Open data file       
   infile.open("employee.txt");
   if (infile.fail())
   {
      cout<<"The file failed to open.\n\n";
      exit(1);
   }
   // Pre-lab instructions:
   // Using a loop, create a worker object, read data from the file,
   // initialize object with the data, and store the object in the
   // array.

   // Using another loop, traverse through the array objects created
   // and display each object.
   while(infile)
   {
		infile >> wrkr; 
		employees[index] = wrkr;
		index++;
   }

   int numEmployees = index;
   for(index = 0; index < numEmployees; index++)
   {
		cout << employees[index] << endl;
   }
   


We were not given the Worker.h file so I can't test it which is why I'm asking on here. Thanks in advance for any help!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>