Structure Array Looping Problem

This is a discussion on Structure Array Looping Problem within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I really need some of your help here because I don't know what to do now. Basically, this ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    11

    Structure Array Looping Problem

    Hi guys, I really need some of your help here because I don't know what to do now. Basically, this program will loop as many as the user inputs number of employee (numEmployee1). The program does the first loop fine, it stores and display the value whatever the user enters. After the first loop, the program does not work...it stores and displays some unexpected numbers (garbage numbers). I think there is something wrong with my loop but after 2 days I still cannot find the problem. Would you guys please help me here?

    Here is a full version of my code:
    Code:
    #include <iostream>
    #include <cstring>
    #include <iomanip>
    #include <cstdlib>
    using namespace std;
    
    //STRUCTURE, ENUM,AND UNION//////////////////////////////////////////////////////////////////////
    
    enum payStatus{ Salaries1, Hourly1, Commissioned1};
    
    union payRate
    {
    	long salaries;
    	float hourly;
    	int commissioned;
    };
    
    struct Employee
    {
    	char ID[4];
    	char name[36];
    	payStatus type;
    	payRate rate;
    	float grossPay;
    };
    ////////////////////////////////////////////////////////////////////////////////////////////
    //FUNCTIONS/////////////////////////////////////////////////////////////////////////////////
    
    int getEmployeeData(Employee*, int);
    float calculateGrossPay(Employee[], int);
    void displayEmployeeInfo(Employee[], int, float);
    ////////////////////////////////////////////////////////////////////////////////////////////
    
    
    //MAIN (STARTING POINT)/////////////////////////////////////////////////////////////////////
    
    int main()
    {	
    
    	int numEmployee1;
    	Employee *emp;
    	float total;
    	
    	cout<<fixed<<showpoint<<setprecision(2);
    
    	cout<<"Enter maximum number of employees to enter: ";
    	cin>>numEmployee1;
    
    	emp = new Employee[numEmployee1];
    
    	getEmployeeData(emp, numEmployee1);
    
    	total=calculateGrossPay(emp, numEmployee1);
    
    	displayEmployeeInfo(emp, numEmployee1, total);
    
    	delete [] emp;
    
    	return 0;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////
    
    //ALL THE FUNCTION DEFINITIONS//////////////////////////////////////////////////////////////
    
    int getEmployeeData(Employee* e, int numEmployee2)
    {
    	char inString[80];
    
    	for(int i=0; i<numEmployee2; i++/*, e++*/)
    	{
    		cout<<"Enter employee id (Enter to quit): ";
    		cin.ignore();
    		cin.getline(e->ID,4);
    
    		cout<<"Enter name (Last, First): ";
    		cin.getline(e->name,36);
    
    		cout<<"Enter S or s for Salaried, H or h for Hourly, and C or c for Commissioned"<<endl;
    		cout<<"Enter payroll status: ";
    		cin.getline(inString, 80);
    
    		if(*inString=='S' || *inString=='s')
    		{
    			(e+i)->type=Salaries1;
    			cout<<"Enter monthly salary: ";
    			cin>>e->rate.salaries;
    		}
    		else if(*inString=='H' || *inString=='h')
    		{
    			(e+i)->type=Hourly1;
    			cout<<"Enter number of hours worked this month: ";
    			cin>>e->rate.hourly;
    		}
    		else
    		{
    			(e+i)->type=Commissioned1;
    			cout<<"Enter total sales for this month: ";
    			cin>>e->rate.commissioned;
    		}
    
    		//^^Error is at above here...it doesn't work from the second loop
    	}
    
    	return 0;
    }
    
    
    float calculateGrossPay(Employee e[], int numEmployee4)
    {
    	float CalTotal=0.0;
    
    	for(int xx=0; xx<numEmployee4; xx++)
    	{
    		if(e[xx].type==Salaries1)
    		{
    			e[xx].grossPay = e[xx].rate.salaries;
    		}
    		else if(e[xx].type==Hourly1)
    		{
    			e[xx].grossPay = 18.75 * e[xx].rate.hourly;
    		}
    		else
    		{
    			e[xx].grossPay = 1000+(e[xx].rate.commissioned*0.06);
    		}
    
    		CalTotal+=e[xx].grossPay;
    	}
    
    	return CalTotal;
    }
    
    
    void displayEmployeeInfo(Employee e[], int numEmployee3, float finalTotal)
    {
    	cout<<fixed<<showpoint<<setprecision(2);
    	cout<<endl;
    	cout<<endl;
    
    	cout<<"ID       Name                    Payroll Status          Gross Pay"<<endl;
    	cout<<endl;
    
    	for(int x=0; x<numEmployee3; x++)
    	{
    		cout<<left<<setw(9)<<e[x].ID<<setw(24)<<e[x].name;
    		
    		if(e[x].type==Salaries1)
    		{
    			cout<<left<<setw(24)<<"Salaries"<<setw(5)<<e[x].grossPay<<endl;
    		}
    		else if(e[x].type==Hourly1)
    		{
    			cout<<left<<setw(24)<<"Hourly"<<setw(5)<<e[x].grossPay<<endl;
    		}
    		else
    		{
    			cout<<left<<setw(24)<<"Commissioned"<<setw(5)<<e[x].grossPay<<endl;
    		}
    	}
    	
    	cout<<endl;
    
    	cout<<"Total Payroll for this month: $"<<finalTotal<<endl;
    	cout<<"Employee with Maximum gross pay: "<<endl;
    	cout<<"Employee with Minimum gross pay: "<<endl;
    	cout<<"Average gross pay: $"<<endl;
    
    	cout<<endl;
    }
    And here is the loop that I have problem with:
    Code:
    for(int i=0; i<numEmployee2; i++/*, e++*/)
    	{
    		cout<<"Enter employee id (Enter to quit): ";
    		cin.ignore();
    		cin.getline(e->ID,4);
    
    		cout<<"Enter name (Last, First): ";
    		cin.getline(e->name,36);
    
    		cout<<"Enter S or s for Salaried, H or h for Hourly, and C or c for Commissioned"<<endl;
    		cout<<"Enter payroll status: ";
    		cin.getline(inString, 80);
    
    		if(*inString=='S' || *inString=='s')
    		{
    			(e+i)->type=Salaries1;
    			cout<<"Enter monthly salary: ";
    			cin>>e->rate.salaries;
    		}
    		else if(*inString=='H' || *inString=='h')
    		{
    			(e+i)->type=Hourly1;
    			cout<<"Enter number of hours worked this month: ";
    			cin>>e->rate.hourly;
    		}
    		else
    		{
    			(e+i)->type=Commissioned1;
    			cout<<"Enter total sales for this month: ";
    			cin>>e->rate.commissioned;
    		}
    
    		//^^Error is at above here...it doesn't work from the second loop
    	}
    Thank you very much for all of your inputs.
    Vitamin_C

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Well you're not being consistent in your loops
    > cin.getline(e->ID,4);
    > (e+i)->type=Salaries1;
    Which kinda follows from your commented out e++ in your for loops.

    Just replace all your e-> things with array notation, eg
    Code:
    cin.getline( e[i].ID, sizeof(e[i].ID) );  // use the compiler to work out the size
    e[i].type=Salaries1;
    >for(int xx=0; xx<numEmployee4; xx++)
    >for(int x=0; x<numEmployee3; x++)
    There's no need to invent new names for things.
    i and numEmployee is all you need in all your functions, and it aids readability.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    ^^ Thank you very much for your help. The program works as I wish now

    Vitamin_C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  2. Multidimensional Array in a Structure
    By Night_Blade in forum C Programming
    Replies: 3
    Last Post: 04-04-2005, 08:14 PM
  3. array in structure help
    By bobnet in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2003, 07:51 AM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Filling an array in a structure
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21