C++ Data storage into array of Struct type

Employee Payroll using array of struct type- CODE WORKS

Page 1 of 1

2 Replies - 13504 Views - Last Post: 27 March 2008 - 12:58 PM Rate Topic: -----

#1 hollis092   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-February 08

C++ Data storage into array of Struct type

Posted 27 March 2008 - 09:49 AM

I have written this code to input from a text file the first name, last name, ID, payrate, and hours ans store
the data in corrsponding struct array members. The total pay is then calculated appropriately and the list is then
sorted using selection sort function. The code functions perfectly, but it has some type of problem executing, I get the program occured error windows is attempting to fix the problem error message. I dont know what is causing this and that is what i need to figure out and fix. But when compiled and ran the output file looks exactly as it should.

I have the code posted below and the input file attached. To run just create an empty .txt file (ie notepad) and save as payroll.txt in the same directory you save this code and the input file (emp_record.txt).

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;

struct empinfo 
{
	   string firstname;
	   string lastname;
	   int id;
	   float payrate;
	   float hours;
	   float pay;
	   
};

empinfo empArray[12];
//***************SELECTION SORT FUNCTION TO SORT BY LAST NAME*******************
void selectionSort( empinfo empArray[ ] , int length )
{
  int position; 
  empinfo tempArray[12];
	
  for ( int i = 0; i < length - 1; i++ )
   {
	  position = i;
	  for ( int j = i; j < length; j++ )
	  {
		 if ( empArray[ j ].lastname < empArray[ position ].lastname )
			position = j;
	  }
	  if ( empArray[ position ].lastname < empArray[ i ].lastname )
	  {
		 tempArray[ 0 ] = empArray[ i ];
		 empArray[ i ] = empArray[ position ];
		 empArray[ position ] = tempArray[ 0 ];
	  }
   }



};

//*********************END OF SELECTION SORT FUNCTION***************************



int main()
{
	ifstream inFile;
	ofstream outFile;
	
	int i = 0;
	inFile.open("emp_record.txt");//open input file
	inFile >> empArray[i].firstname;//prime loop
	inFile >> empArray[i].lastname;
	inFile >> empArray[i].id;
	inFile >> empArray[i].payrate;
	inFile >> empArray[i].hours;

	
	while(inFile)//(end of file) while loop
	{						 
		if(empArray[i].hours > 40)// if hours greater than 40, this is the calculation for OT
			 empArray[i].pay = ((empArray[i].payrate * 40) + (empArray[i].payrate * (empArray[i].hours - 40)* (1.5)));
		else//if regular time you get regular pay...
			 empArray[i].pay = (empArray[i].payrate * empArray[i].hours);

	 i++;
	 inFile >> empArray[i].firstname;//reprime loop
	 inFile >> empArray[i].lastname;
	 inFile >> empArray[i].id;
	 inFile >> empArray[i].payrate;
	 inFile >> empArray[i].hours;
				 
				 
				 
}


	 outFile.open("payroll.txt");//open output file
	 outFile<<"Weekly Payroll:"<<endl<<endl;
	 outFile<<"Name "<<setw(16)<<"ID"<<setw(13)<<"Pay"<<endl<<endl;
	 outFile<<"--------------------------------------"<<endl;
	 selectionSort(empArray,12);  //call to sort the names by last name 
	 for(int i = 0; i <= 12; i++){  //using for loop to output the info 
	 outFile<<setw(16)<<left<<empArray[i].lastname + ", " + empArray[i].firstname<<right<<setw(8)<<empArray[i].id<<setw(8)<<fixed<<setprecision(2)<<"$"<<empArray[i].pay<<endl;
 }
	
				 
	
	
  
	
	
	
	
	
	system("pause");//wait until any key is pressed and exit
	return 0;   
	
}

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: C++ Data storage into array of Struct type

#2 MikeRaines   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 78
  • Joined: 30-October 07

Re: C++ Data storage into array of Struct type

Posted 27 March 2008 - 12:15 PM

Took me a bit to track this down. Well written code by the way.

Your problem is you are iterating to a position in the array that doesnt exist.

Replace:
for(int i = 0; i <= 12; i++){  



With:
for(int i = 0; i < 12; i++){  



And it fixes the problem. Remember your iterator starts at 0 not 1 :)

Hope this is helpful,
-Mike

This post has been edited by MikeRaines: 27 March 2008 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 hollis092   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-February 08

Re: C++ Data storage into array of Struct type

Posted 27 March 2008 - 12:58 PM

Thanks mike, just needed an extra pair of eyes to help me out.
I just corrected it works perfectly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1