4 Replies - 1583 Views - Last Post: 19 June 2007 - 09:31 PM Rate Topic: -----

#1 mdmac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-June 07

text file into arrays of structs

Posted 15 June 2007 - 05:06 PM

Ok I'm having a really hard time figuring this out. What I'm attempting to do is to read a text file and put the information into 4 arrays. One of the arrays contains the "Personnel" structure and the remaining three contain the "Time" structure. I need to store them, calculate the number of hours worked for each person and who has been the latest.
Then I'll have to print it on the screen as well as to outdata.txt. :crazy:

I'm not asking anyone to spell anything out for me. I just really need to be ale to load the data (GetData) and (PrintData) and I have no clue on how to proceed.

The indata.txt:
Mark Twain 9 45 18 55
Carlos Perez 9 30 18 45
Edgar Cantu 8 00 19 05
Tom Hanks 8 15 18 00
Mary Carillo 8 05 19 10

Any help will be appreciated!!

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

struct time				// data type
{
	int hr;			// define members
	int min;
};

struct personnel
{
	string fname;
	string lname;
};

void GetData(inFile, personnel employee[], time cardI[], time cardO[], int index);					//declare prototypes
void Hoursworked(const time cardI[], const time cardO[], time hoursw[], int index);
int Latest(const time cardO[], int index);
void PrintData(ofstream outFile, personnel employee[], const time cardI[], const time cardO[], time hoursw[], int index, int mlate);

int main() {
	
	personnel employee[20];
	time cardI[20], cardO[20], hoursw[20];
	int index, mlate;
	int x=0;
	
	ifstream inFile;
	ofstream outFile;

	inFile.open("indata.txt");
	outFile.open("outdata.txt");

	if (!inFile)
	{
		cout << "Couldn't open the input file" << endl;
		return 1;
	}
	
	outFile.open("outdata.txt");
	if (!outFile)
	{
		cout << "Couldn't open the output file" << endl;
		return 1;
	}
	
	GetData(inFile, personnel employee[], time cardI[], time cardO[], int index){	//read from text file and insert into arrays 
	index = 0;
	
		while(!inFile.eof()){
		inFile>>employee[index].fname>>employee[index].lname>>cardI[index]>>cardO[index];
		index++;
		
	
	}
	}


			 /*Hoursworked(const time cardI[], const time cardO[], time hoursw[], int index);{		//hours worked function
		int x=0;
		for (x=0, x<index, x++);
			(cardO[x].hr * 60 + cardI[x].min) - (cardI[x].hr * 60 + cardO[x].min);

		hoursw[x].hr = mlate/60;
		hoursw[x].min = mlate%60;
	};
	
		int Latest(cardO[], int index){															//Latest employee function
		lateindex=0;
		int minw;
		for(x=1, x<index, x++)
			(cardO[x].hr*60+cardO[x].min)=minw;
		if(minw>cardO[lateindex].hr*60+cardO[lateindex].min)
			lateindex=x;
	};
	
	PrintData(outFile, personnel employee[], const time cardI[], const time cardO[], const time hoursw[], int index, int mlate){ // print data function
	
	
	
	
};
*/
	
	return 0;
};

This post has been edited by mdmac: 15 June 2007 - 08:43 PM


Is This A Good Question/Topic? 0
  • +

Replies To: text file into arrays of structs

#2 rameshg87  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 34
  • Joined: 06-June 07

Re: text file into arrays of structs

Posted 15 June 2007 - 10:08 PM

I think you had problems with compilation of the program. Here is it solved, now work on it....


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

struct times				
{
	int hr;			
	int min;
};

struct personnel
{
	string fname;
	string lname;
};

int main() 
{
	
	int index, mlate;
	int i,x=0;
	personnel employee[20];
	times cardI[20], cardO[20], hoursw[20];
	
	ifstream inFile;
	ofstream outFile;

	inFile.open("indata.txt");
	outFile.open("outdata.txt");

	if (!inFile)
	{
		cout << "Couldn't open the input file" << endl;
		cin.get();
		return 1;
	}
	
	outFile.open("outdata.txt");
	
	index=0;   
	while(!inFile.eof())
	{
		inFile>>employee[index].fname>>employee[index].lname;
		inFile>>cardI[index].hr>>cardI[index].min;
		inFile>>cardO[index].hr>>cardO[index].min;
		index++;
	}
	
	
  

	for (i=0;i<index;i++)
	{
		cout<<"name is "<<employee[i].fname<<" "<<employee[i].lname;  
		cout<<"\ntime is "<<cardI[i].hr<<cardI[i].min;
		cout<<"\ntime is "<<cardO[i].hr<<cardO[i].min<<"\n\n";
	}
  
	inFile.close();
	outFile.close();
	cin.get();
	return 0;
};



Was This Post Helpful? 0
  • +
  • -

#3 mdmac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-June 07

Re: text file into arrays of structs

Posted 16 June 2007 - 12:09 PM

View Postrameshg87, on 15 Jun, 2007 - 10:08 PM, said:

I think you had problems with compilation of the program. Here is it solved, now work on it....


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

struct times				
{
	int hr;			
	int min;
};

struct personnel
{
	string fname;
	string lname;
};

int main() 
{
	
	int index, mlate;
	int i,x=0;
	personnel employee[20];
	times cardI[20], cardO[20], hoursw[20];
	
	ifstream inFile;
	ofstream outFile;

	inFile.open("indata.txt");
	outFile.open("outdata.txt");

	if (!inFile)
	{
		cout << "Couldn't open the input file" << endl;
		cin.get();
		return 1;
	}
	
	outFile.open("outdata.txt");
	
	index=0;   
	while(!inFile.eof())
	{
		inFile>>employee[index].fname>>employee[index].lname;
		inFile>>cardI[index].hr>>cardI[index].min;
		inFile>>cardO[index].hr>>cardO[index].min;
		index++;
	}
	
	
  

	for (i=0;i<index;i++)
	{
		cout<<"name is "<<employee[i].fname<<" "<<employee[i].lname;  
		cout<<"\ntime is "<<cardI[i].hr<<cardI[i].min;
		cout<<"\ntime is "<<cardO[i].hr<<cardO[i].min<<"\n\n";
	}
  
	inFile.close();
	outFile.close();
	cin.get();
	return 0;
};




Thanks! I pretty much have it all worked out now.
Was This Post Helpful? 0
  • +
  • -

#4 mdmac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-June 07

Re: text file into arrays of structs

Posted 16 June 2007 - 02:04 PM

Ok, I pretty much have this all done but I have one minor problem.

The output is supposed to look similar to this:

Joe Doe worked 8 hrs and 45 mins from 9:45 until 18:30
Jane Dane worked 9 hrs and 15 mins from 9:30 until 18:45
Jack Pack worked 10 hrs and 05 mins from 8:00 until 18:05
Tom Dumb 4 worked 9 hrs and 45 mins from 8:15 until 18:00

Jane Dane was the last one to leave at: 18:45


I pretty much got that down except my output excludes some zeros. I get this:

Mark Twain worked 9 hrs and 10 mins from 9:45 until 18:55
Carlos Perez worked 9 hrs and 15 mins from 9:30 until 18:45
Edgar Cantu worked 11 hrs and 5 mins from 8:0 until 19:5
Tom Hanks worked 9 hrs and 45 mins from 8:15 until 18:0
Mary Carillo worked 11 hrs and 5 mins from 8:5 until 19:10

Mary Carillo was the last one to leave at 19:10


Is there any way to get around this? Thanks in advance.

for (i=0;i<index;i++)												//ouput to outdata.txt
	{
		outFile<<employee[i].fname<<" "<<employee[i].lname<<" worked "<<hoursw[i].hr<<" hrs and "<<
			hoursw[i].min<<" mins from "<<cardI[i].hr<<":"<<cardI[i].min<<" until "<<cardO[i].hr<<":"<<
			cardO[i].min;
		outFile<<endl;
		
	}
		outFile<<endl;
		outFile<<employee[mlate].fname<<" "<<employee[mlate].lname<<" was the last one to leave at "<<cardO[mlate].hr<<":"<<cardO[mlate].min;
		
  
	inFile.close();														//close input and output files
	outFile.close();
	cin.get();
	return 0;
};



EDIT: I figured it out. I just used setw and setfill.

This post has been edited by mdmac: 16 June 2007 - 03:49 PM

Was This Post Helpful? 0
  • +
  • -

#5 mdmac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-June 07

Re: text file into arrays of structs

Posted 19 June 2007 - 09:31 PM

I apologize for making multiple posts like this. For some reason I'm not able to edit my previous post. Also, I didn't want to make a completely new thread for this. Well, I was able to get the program working perfectly but my professor will not give me a passing grade due to the fact that I did not use the specified functions. I have rewritten the program with the required prototypes and function definitions. I just have no idea what to put in MAIN. How do I pass the info from the text file into the functions without having to create all these arguments that pretty much do exactly what the function definition does. I'm really confused. Sorry!

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;




//Declare structures
struct time	
{
	int hr;			
	int min;
};

struct personnel
{
	string fname;
	string lname;
};

//Declare prototypes
void GetData(ifstream&, personnel, time, time, int);
void Hoursworked(const time, const time, time, int);
int Latest(const time, int);
void PrintData(ofstream&, personnel, const time, const time, time, int, int);



int main()
{
	//Declare variables and arrays of structures
	int index;
	int mlate=0;
	personnel employee[20];
	time cardI[20], cardO[20], hoursw[20];
	
	//Define ifstream and ofstream variables
	ifstream inFile;													
	ofstream outFile;

	//Open input and output files
	inFile.open("indata.txt");											
	outFile.open("outdata.txt");
	
	//Test inFile
	if (!inFile)													
	{
		cout << "Couldn't open the input file" << endl;
		cin.get();
		return 1;
	}
	
	//Test outFile
	if (!outFile)													
	{
		cout << "Couldn't open the output file" << endl;
		cin.get();
		return 1;
	}
	




	//Close input and output files
	inFile.close();														
	outFile.close();
	cin.get();
	return 0;
};

//*******************************//
//FUNCTION DEFINITION FOR GETDATA//
//*******************************//`
 void GetData(ifstream& inFile, personnel employee[], time cardI[], time cardO[], int& index)
	{
	index=0;  
	while(!inFile.eof())												
	{
		inFile>>employee[index].fname>>employee[index].lname;
		inFile>>cardI[index].hr>>cardI[index].min;
		inFile>>cardO[index].hr>>cardO[index].min;
		index++;
	}
	}

 //***********************************//
 //FUNCTION DEFINITION FOR HOURSWORKED//
 //***********************************//
void Hoursworked(time cardI[], time cardO[], time hoursw[], int index)
{
	int minw;
		for(int x=0;x<index;x++)												
		{
		minw = (cardO[x].hr*60 + cardO[x].min) - 
		(cardI[x].hr*60 + cardI[x].min);
		
		hoursw[x].hr = minw/60;
		hoursw[x].min = minw%60;
		}
}

//******************************//
//FUNCTION DEFINITION FOR LATEST//
//******************************//
	int Latest(time cardO[], int index)
	{
		int mlate;
		int totmin;
		for(int x=1;x<index;x++)												
		{
		totmin =(cardO[x].hr*60+cardO[x].min);
			if(totmin>cardO[mlate].hr*60+cardO[mlate].min)
				mlate=x;
	}
		return mlate;
	}
	
//*********************************//
//FUNCTION DEFINITION FOR PRINTDATA//
//*********************************//
	void PrintData(ofstream& outFile, personnel employee[], time cardI[], time cardO[], time hoursw[], int index, int mlate)
	{
		for (int i=0;i<index;i++)												//ouput to outdata.txt
	{
		outFile<<setprecision(5)<<employee[i].fname<<" "<<employee[i].lname<<" worked "<<hoursw[i].hr<<" hrs and "<<
			hoursw[i].min<<" mins from "<<cardI[i].hr<<":"<<setw(2)<< setfill('0')<<cardI[i].min<<" until "<<cardO[i].hr<<":"<<setw(2)<< setfill('0')<<
			cardO[i].min;
		outFile<<endl;
		
	}
		outFile<<endl;
		outFile<<employee[mlate].fname<<" "<<employee[mlate].lname<<" was the last one to leave at "<<cardO[mlate].hr<<":"<<cardO[mlate].min;
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1