Help with "cout" [2-dim arrays]

This is a discussion on Help with "cout" [2-dim arrays] within the C++ Programming forums, part of the General Programming Boards category; Help with CODE can't seem to make the arrays i made and stuff to appear on screen.. (noobie) Code: #include ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Help with "cout" [2-dim arrays]

    Help with CODE can't seem to make the arrays i made and stuff to appear on screen.. (noobie)
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    const double HEALTH_PLAN=18.75;
    const double FED_TAX=0.22;
    const double DEPENDENT_COST= 7.35;
    
    
    
    void employeeinfo(char name[][30],unsigned int hours[7], float &rate,int &dependents, char &healthplan);
    int calculatehours(unsigned int hours[7]);
    float calculategross(unsigned int hours,float &rate);
    float calculatedfedtax(float grosspay);
    float calculatehealth(char healthplan,int dependents);
    float calculatefica(float grosspay);
    void bubblename(char name[][30], int counter);
    float calculateNetpay(float fedtax,float fica,char healthplan,float grosspay);
    void output(float totaldata[][6],char name [][30]);
    
    int main()
    {
    	char name[20][30],choice,answer;
    	unsigned int hours[7];
    	float rate,totaldata[20][6];
    	int dependents,count = 0;
    	char healthplan;
    	int hoursworked,totalhours =0;
    	float gross,fedtax,fica,netpay;
    
    	
    
    
    	cout<<"Hello and Welcome to 4DaLulz inc. Payroll computation system."<<endl;
    	cout<<"You will need employee information such as name,hours, and payrate"<<endl;
    	cout<<"\nWould you like to continue? ( Y or N ) \n";
    	cin>>choice;
    	cin.ignore(1);
    	switch(choice)
    	{
    	case 'Y':
    	case 'y':
    		do
    		{
    			employeeinfo(name,hours,rate,dependents,healthplan);
    			hoursworked=calculatehours(hours);
    			totalhours+=hoursworked;
    			gross=calculategross(hoursworked,rate);
    			fedtax=calculatedfedtax(gross);
    			fica=calculatefica(gross);
    			netpay=calculateNetpay(gross,fedtax,fica,healthplan);
    
    			totaldata[count][0]=hoursworked;
    			totaldata[count][1]=totalhours;
    			totaldata[count][2]=gross;
    			totaldata[count][3]=fedtax;
    			totaldata[count][4]=fica;
    			totaldata[count][5]=netpay;
    		
    			cout<<"Are there more employees? (Y or N) \n";
    			cin>>answer;
    			cin.ignore(1);
    			if(toupper(answer) != 'Y')
    				break;
    		}while(choice=='Y'||choice=='y');
    		break;
    	}
    	output(totaldata,name);
    
    	return 0;
    }
    void output(float totaldata[][6],char name [][30])
    {
    // i can't seem to make the functions, and name data appear on the screen
    // supposed to look something like
                        Total          FICA
    // Name       Hours          Tax    // etc etc...// can't get the cout right...=(
       [names]   [hours]        [fica]
    
    }
    
    void swapstrings(char a[], char b[], int length)
    {
    	char c;
    	for (int i = 0; i < length; i++)
    	{
    		c = a[i];
    		a[i] = b[i];
    		b[i] = c;
    	}
    }
    
    //void bubblehours(unsigned int hours[7],int counter)
    //{
    //	for(int i=1;i<counter;i++)
    //		for(int j=0;j<counter;j++)
    //		if(hours[i]>hours[j])
    //			swap(hours[i],hours[j]);
    //}
    void bubblename(char name[][30], int counter, float totaldata[][6])
    {
    	for(int i=1;i<counter;i++)
    	{	
    		for(int j=0;j<counter-i;j++)
    		{	
    			if(strcmp(name[i], name[j]) > 0)
    			{	swapstrings(name[i], name[j],30);
    		
    				for(int u=0;u<7;u++)
    				{
    					swap(totaldata[i][u],totaldata[j][u]);
    				}
    				//	
    			}
    		}
    	}
    }				
    void employeeinfo(char name[][30],unsigned int hours[7], float &rate,int &dependents, char &healthplan)
    {
    	int counter = 0;
    	cout<<"\nEmployee's Name: \n";
    	cin.getline(name[counter],30,'\n');
    	cin.clear();
    
    	cout<<"Hours: \n";
    	for(int h=0;h<7;h++)
    	cin>>hours[h];
    
    	cout<<"Rate: \n";
    	cin>>rate;
    
    	cout<<"Dependents: \n";
    	cin>>dependents;
    	if(dependents<=0)
    	{
    		cout<<"You count as a dependent minimum is 1. \n";
    		cin>>dependents;
    	}
    	cout<<"Health Plan (Y or N)? \n";
    	cin>>healthplan;
    
    }
    
    float calculategross(unsigned int hours,float &rate)
    {
    	if(hours>40)
    		return(hours-40)*1.5f*rate+40*rate;
    	return hours * rate;
    }
    int calculatehours(unsigned int hours[])
    {
    	int hourstotal=0;
    	for(int h=0;h<7;h++)
    		hourstotal+=hours[h];
    	return hourstotal;
    }
    float calculatedfedtax(float grosspay)
    {
    	if(grosspay>300)
    		return(grosspay-300)*FED_TAX;
    	return 0;
    }
    
    float calculatehealth(char healthplan,int dependents)
    {
    	if(toupper(healthplan)!='Y')
    		return 0;
    	else
    		return HEALTH_PLAN+DEPENDENT_COST*(dependents-1);
    }
    float calculatefica(float grosspay)
    {
    	float fica;
    	fica=grosspay*.085f;
    	if(fica<45)
    		return 45;
    return fica;
    }
    float calculateNetpay(float fedtax,float fica,char healthplan,float grosspay)
    {
    	double netPay;
    	if(healthplan=='Y'||healthplan=='y')
    	{
    		netPay=grosspay-fedtax-fica-HEALTH_PLAN;
    	}
    	else
    	{
    		netPay=grosspay-fedtax-fica;
    	}
    	return netPay;
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Show us what you have so far and we'll correct you.

    This might come in handy:
    http://www.cplusplus.com/reference/i...tors/setw.html

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Code:
    void output(char name[][30],float totaldata[][6], int counter)
    {
    for(int i=0;i<counter;i++)
         for(int j=0;j<counter;j++)
    cout<<left<<setw(31)<<"Name"
    <<setw(5)<<"Hours"<<setw(5)<<"Pay"<<setw(8)<<"Gross"<<setw(5)<<"Fed"<<setw(5)<<" "<<setw(5)<<"Health"<<setw(5)<<"Net \n";
    cout<<setw(36)<<"Worked"<<setw(5)<<"Rate"<<setw(8)<<"Pay"<<setw(5)<<"Tax"<<setw(5)<<"FICA"<<setw(5)<<"Plan"<<setw(5)<<"Pay \n";
    cout<<setw(31)<<name[i][j]<<setw(5)<<totaldata[i][j]; // have a good feeling this last cout is wrong the one i can't get...i can't check i'm at school right now but yeah still can't figure this thing out...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Looking at the output should have given you a good idea what was wrong: seeing the headers printed for every number, rather than for every person, indicates that you have your headers one for-loop too far.

    So loop on counter:
    * print headers
    * print name (which is name[i], by the way)
    * print each of the six data elements in totaldata[i] (you could use another for-loop here, maybe, but it looks like your setw()s are different? so you'd have to deal with that)
    * print new line

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help optimizing loops....
    By ninjacookies in forum C Programming
    Replies: 4
    Last Post: 02-24-2005, 03:54 PM
  2. Bit a tough one
    By crag2804 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 12-10-2002, 07:40 PM

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