#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Location
    New York
    Posts
    18
    Rep Power
    0

    Output loop wont print


    it was printing before I modified it with a sort routine (input file not included, not sure how to upload that)
    and help appreciated. this modification took me all day. mostly just trying to figure out why it wont print
    much less sort

    Code:
    # include <iostream>
    # include <fstream>
    # include <string>
    # include <iomanip>
    
    void ReadData(void);	
    void CalculateGrosspay(void);
    void CalculateTaxRate(void);
    void AdjustforMaritalStatus(void);
    void CalculateNetPay(void);
    void sortnetpay(void);
    void OutputToConsole(void);
    
    using namespace std;
    
    float RegularHours[100],OverTimeHours[100];
    float TaxRate[100],TaxAmount[100],GrossPay[100],RegularPay[100],OverTimePay[100],NetPay[100];
    double TotalNetPay = 0.0 ;
    int EmployeeId[100],n;
    string FirstName[100],LastName[100],SSnumber[100];
    float HourlyRate[100],HoursWorked[100];
    char MaritalStatus[100];
    
    int main() 
    {
    	ReadData();
    	CalculateGrosspay();
    	CalculateTaxRate();
    	AdjustforMaritalStatus();
    	CalculateNetPay();;
    	OutputToConsole();
    	sortnetpay();
    	OutputToConsole();
    	return 0;
    }
    
    //Read Data------------------------------------------------------------------------------------------------------
    	
    	void ReadData (void)
    			{	
    				ifstream fin("employee.txt");
    				  int n=0;
    				while(fin>>EmployeeId[n]>>FirstName[n]>>LastName[n]>>SSnumber[n]>>HoursWorked[n]>>HourlyRate[n]>>MaritalStatus[n])
    					{
    					n++;
    					}
    				fin.close();
    
    			}
    //Calculate Gross pay------------------------------------------------------------------------------------------------------	
    
    	void CalculateGrosspay (void)
    			{
    				int i=0;
    				while(i<n)
    					{
    						if (HoursWorked[i] > 40)
    							{
     								OverTimeHours[i] = HoursWorked[i]-40;
     								RegularHours[i]=40;
    							}
    						else 
    							{
    								OverTimeHours[i]=0;
    								RegularHours[i]=HoursWorked[i];
    							}
    					GrossPay[i]=(RegularHours[i]*HourlyRate[i])+(OverTimeHours[i]*(HourlyRate[i]*1.5));
    		
    						i++;	
    					}
    		
    			}
    			
    //Calculate Tax Rate------------------------------------------------------------------------------------------------------
    				
    	void	CalculateTaxRate(void)
    		{
    	
    		int i=0;
    		while(i<n)// Decide on Tax Rate
    		{
    			if (GrossPay[i]>1000)
    				{ 
    				TaxRate[i]=.30;
    				}
    			else if (GrossPay[i] > 800 && GrossPay[i]<=1000)
    				{
    	 			TaxRate[i]=.20;
    				}
    			else if (GrossPay[i]> 500 && GrossPay[i]<= 800)
    				{
    				TaxRate[i]=.10;
    				}
    			else if (GrossPay[i]<= 500)
    				{
    				TaxRate[i]=0;
    				}	
    			i++;	
    		}
    	}
    //Adjust for Marital Status------------------------------------------------------------------------------------------------------		
    		
    void AdjustforMaritalStatus (void)	
    	{
    		int i=0;
    		while(i<n)//Adjust tax rate based on marital status
    		{
    			if (MaritalStatus[i] == 'S' | MaritalStatus[i] =='s')
    				{
    				TaxRate[i]=TaxRate[i]+.05;
    				}
    			else if (MaritalStatus[i] == 'M' | 'm')
    				{
    				TaxRate[i]=TaxRate[i];
    				}
    			else if (MaritalStatus[i] == 'H' | 'h')
    				{
    				if (TaxRate[i]>0)
    					{
    					TaxRate[i]=TaxRate[i]-.05;
    					}		
    					TaxRate[i]=TaxRate[i]-.05;
    				}
    			i++;
    		}
    	}
    //Calculate Net Pay------------------------------------------------------------------------------------------------------	
    			
    	void CalculateNetPay (void)
    		{
    			int i=0;
    			while(i<n)//Calculate Tax and Net pay
    				{				
    					// calculate TAX Amount		
    					TaxAmount[i]=TaxRate[i]*GrossPay[i];
    					// calculate NET pay
    					NetPay[i]=GrossPay[i]-TaxAmount[i];
    					// Calculate Overtime Pay
    					OverTimePay[i]=OverTimeHours[i]*(HourlyRate[i]*1.5);
    					i++;
    
    				}
    		}
    	
    
    void sortnetpay(void){
    			int scan=0,pass=0;
    			double temp=0;
    
    				for(pass=1;pass<n;pass++){
    
    					for(scan=0;scan<n;scan++){
    
    						if (NetPay[scan]>NetPay[scan+1]){
    
    					  	 	temp=NetPay[scan];
    							NetPay[scan]=NetPay[scan+1];
    							NetPay[scan+1]=temp;
    							
    					 	 }
    					}//end scan
    				}//end pass
    
    			}//end sort
    		
    
    //Output to Console------------------------------------------------------------------------------------------------------								
    
    void OutputToConsole (void){
    			cout.setf(ios::showpoint);
    			
    			//output header
    			std::cout << ios::left <<setw(50) << "COPYKATS PRINTING PAYROLL"<<endl<<endl;
    			std::cout << ios::left <<  setw(6) << "ID";
    			std::cout << ios::left <<  setw(10) << "First";
    			std::cout << ios::left <<  setw(13) << "Last";
    			std::cout << ios::left <<  setw(11) << "SS#";
    			std::cout << ios::left <<  setw(4) << "Hrs";
    			std::cout << ios::left <<  setw(5) << "rHrs";
    			std::cout << ios::left <<  setw(4) << "oT";
    			std::cout << ios::left <<  setw(6) << "hRate";
    			std::cout << ios::left <<  setw(4) << "MS";
    			std::cout << ios::left <<  setw(7) << "tRate";
    			std::cout << ios::left <<  setw(7) << "gPay";
    			std::cout << ios::left <<  setw(8) << "oTpay";
    			std::cout << ios::left <<  setw(7) << "tax";
    			std::cout << ios::left <<  setw(8) << "Netpay" << endl;
    		     std::cout<<"---------------------------------------------------------------------------------------------------"<<endl;	
    			//output data
    			int i=0;
    			while(i<n){
    				std::cout << ios::left << setw(6)<<EmployeeId[i];
    				std::cout << ios::left << setw(10)<<FirstName[i];
    				std::cout << ios::left << setw(9)<<LastName[i];
    				std::cout << ios::left << setw(15)<<SSnumber[i];
    				std::cout << ios::left << setw(4)<<HoursWorked[i];
    				std::cout << ios::left << setw(4)<<RegularHours[i];
    				std::cout << ios::right << setw(3)<<OverTimeHours[i];
    				std::cout << ios::right << setw(6)<<HourlyRate[i];
    				std::cout << ios::right << setw(4)<<MaritalStatus[i];
    				std::cout << ios::right << setw(6)<<TaxRate[i];
    				std::cout << ios::right << setw(8)<<GrossPay[i];
    				std::cout << ios::right << setw(7)<<OverTimePay[i];
    				std::cout << ios::right << setw(8)<<TaxAmount[i];
    				std::cout << ios::right << setw(8)<<NetPay[i]<<endl<<endl;	
    			i++;
    				}
    				
    		}
    input file
    Code:
    1111	Jackson	Brown	159-55-4972	40	13.00	M
    2222	Pink	Floyd	159-00-3657	35	9.00	s
    3333	Elvis	Presley	462-40-5469	45	8.50	H
    4444	Chris	Columbo	123-45-6789	70	10.50	m
    5555	Juan	Valdez	321-35-9731	70	7.50	h
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,182
    Rep Power
    2222
    Unreadable mess! You have been instructed to use code tags, but you refuse to do so. Why should we waste our time trying to make sense out of that unreadable mess.

    Format your code properly and post it using code tags.

    You also still have not corrected the warnings:
    C:\otros\dcw>g++ -Wall pop_02.cpp
    pop_02.cpp: In function `void OutputToConsole()':
    pop_02.cpp:171: `left' undeclared (first use this function)
    pop_02.cpp:171: (Each undeclared identifier is reported only once
    pop_02.cpp:171: for each function it appears in.)
    pop_02.cpp:196: `right' undeclared (first use this function)

    C:\otros\dcw>
    The compiler still thinks that left and right are variables, not ios:: directives. In your compiler's confusion it has to make guesses. Who knows what kind of output mess the compiler guessed into existence.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Location
    New York
    Posts
    18
    Rep Power
    0
    Originally Posted by dwise1_aol
    Unreadable mess! You have been instructed to use code tags, but you refuse to do so. Why should we waste our time trying to make sense out of that unreadable mess.

    Format your code properly and post it using code tags.

    You also still have not corrected the warnings:

    The compiler still thinks that left and right are variables, not ios:: directives. In your compiler's confusion it has to make guesses. Who knows what kind of output mess the compiler guessed into existence.
    OK thanks, working on that
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Location
    New York
    Posts
    18
    Rep Power
    0
    Originally Posted by dwise1_aol
    Unreadable mess! You have been instructed to use code tags, but you refuse to do so. Why should we waste our time trying to make sense out of that unreadable mess.

    Format your code properly and post it using code tags.

    You also still have not corrected the warnings:

    The compiler still thinks that left and right are variables, not ios:: directives. In your compiler's confusion it has to make guesses. Who knows what kind of output mess the compiler guessed into existence.
    Not sure how to format the ios flags
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,182
    Rep Power
    2222
    Originally Posted by popsimath
    Not sure how to format the ios flags
    ios::left
    ios::right
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Location
    New York
    Posts
    18
    Rep Power
    0
    Ok Thanks for your help and quick responses! I have fixed them but the loop still does not print.
    also the header texts are now printing with a "32" after each one, no idea where that came from and I have inserted cout.setf(ios::showpoint); sort of randomly, not being positive whether I need it to use the ios flags or if it matters where it goes???
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,182
    Rep Power
    2222
    Now that it compiles cleanly, we can address its behavior. Until we had reached that point, we first had to eliminate any possible problems related to the warnings.

    You could use code tags to display some of the data in your data file. Quote tags would also work, but you would lose the data file's formatting (all white space would be removed), so using code tags would present that data file to us as it is actually formatted.

    Without that data, we'd have to slog through 200 lines of code to figure out what it's supposed to be doing. Plus we'd be missing interaction with the data. I'm assuming that in previous versions of the program as you've been developing it (start small, add on incrementally is a good approach to design) it has been reading the data file and printing, and that the data file has not changed, and that it's still in the same place with respect to the executable as before.

    In the meantime, you could add some diagnostic cout's to ensure that the right things are happening. For example, in ReadData() when you open employee.txt, you are blindly assuming that it successfully opened that file. What if it didn't? What if it couldn't find that file? Whenever you open a file, you must always verify whether it did open and handle that failure properly, which includes reporting the failure and not trying to use it. And when you have finished reading in all the data, for which you have incremented a record counter, n, you should print that information out so that you can verify that it worked properly, something like,
    cout << "ReadData: record count = " << n <<endl;
    If n is set to a value other than what you expect, then there is a problem at that point.

    The general approach I'm invoking here is based on basic troubleshooting principles I was taught in tech school. We have a piece of equipment that accepts a input signals and generates output signals. Within it, the signal paths pass through several electronics modules. That piece of equipment has all the signals going in, but none coming out. So within that piece of equipment, we test at various points along the signal path to see whether the signal is present or not. We were taught a binary search, in which we first test the mid-point and then the mid-point of whichever half was indicated (eg, if the signal is present at mid-point, then the problem is in the second half of the signal path). And we continue narrowing it down until we determine the actual circuit that's malfunctioning. In fact, since that piece of equipment was part of an entire system, we had troubleshot the system in the same manner to narrow the problem down to that particular piece of equipment.

    You're not getting output from your program. You know the path of data flow, so you check each stage of that data flow to see where the data is still present and where it gets lost. A debugger is an excellent tool for doing that. If you're not using a debugger, then diagnostic print statements placed at strategic points in your program works almost as well.

    These suggestions should help you in your debugging.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,182
    Rep Power
    2222
    it was printing before I modified it with a sort routine
    I assume that you are referring to sortnetpay():
    Code:
    void sortnetpay(void)
    {
        int scan=0,pass=0;
        double temp=0;
    
        for(pass=1;pass<n;pass++)
        {
            for(scan=0;scan<n;scan++)
            {
                if (NetPay[scan]>NetPay[scan+1])
                {
                    temp=NetPay[scan];
                    NetPay[scan]=NetPay[scan+1];
                    NetPay[scan+1]=temp;
                }
            }//end scan
        }//end pass
    }//end sort
    Sorry for the reformatting; it was for my own benefit. Also, very good practice there of identifying what the close braces are associated with. That will especially come in handy when the size of the code within the braces becomes much greater.

    The logic doesn't look right. I think you are exceeding the bounds of the array. Also, I'm not sure what kind of sort algorithm you're using. Shell sort?

    Think through the logic. In the shell sort (working from memory here), you are comparing each array element with all the elements that come after it in order to find the lowest value (assuming ascending order sort). So the outer loop works from the first array position to the next-to-the-last position. Why not to the last position? Because when you reach the last position, there's nothing left to compare it to. Also, you don't compare the first element with the first element. And once you have found the lowest value for the first position, you're not going to include that position again in any subsequent tests.

    Also, in an array of n elements, those elements are NetPay[0] through NetPay[n-1]. In your code, when scan is n-1, then scan+1 would be n, which would exceed the bounds of the array. Beyond the end of the array lies nothing but garbage (zero in this case, because the array is a global, but random garbage if the array is local). So you end up comparing garbage to your data and, since that garbage is zero, then zero should rise up to the top as the lowest value. I do not know what effect that should have on your code, but it should succeed in zeroing out your data.

    Here is pseudo-code (so as to not give away the answer; you'll need to translate to C++) for what I remember the shell sort as being (haven't played with it that I know of since 1982, so here goes!):
    Code:
       int i; // outer loop index
        int j;  // inner loop index
        for i=0 to n-2    // do up to the next to the last, but not the last
            for j=i+1 to n-1  // start with the next element and go to the end
                compare a[i] to a[j] and swap if needed
            end for j
        end for i
    When you write a routine that works with an array, such as a sort, a good practice is to test it with pencil and paper. Create a short array of 3 to 5 elements and run them through your code. Be brutally honest with yourself and adhere strictly to your code, doing exactly and precisely what it tells you to do, step by step. Look for problems like exceeding the bounds of the array.

    I don't know how this will affect the output problem, except at least your data won't be all munged up. Plus you said that this was the only thing that you had changed.
    Last edited by dwise1_aol; January 18th, 2014 at 03:03 PM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Location
    New York
    Posts
    18
    Rep Power
    0
    OK thanks dwise1,
    I have tried placing couts all over the place and they work most places except the second half of the ouput module.

    I will try to learn my debugger, I watched some videos and learned a little about it. If that doesnt work i will try pencil and paper and look for the array bounds. I also posted the input data.
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,182
    Rep Power
    2222
    Originally Posted by popsimath
    OK thanks dwise1,
    I have tried placing couts all over the place and they work most places except the second half of the ouput module.
    You didn't tell us that it was only the second half of the output function that no longer worked. How are we supposed to help you when we only get incomplete information?

    What you describe now would be consistent with n being equal to zero. What did that cout tell you the value of n to be? And did you use cout in the middle of the output function, right before the while, to verify that nothing had changed the value of n? Without data of our own, we cannot run that test for you.

    Are you now testing whether the opening of the file succeeded?

    Did you correct the sorting function yet?

    There's an additional problem with that sorting function, I think. Is it true that your intent is not to just sort the netpay array? Is it true that your intent is to sort all of the arrays based on net pay? If the latter is your intent, then you would not get the output you wanted. You would do better to use a single array of struct.


    If you give us some data to play with, then we would be able to test your code ourselves.

    And do double-check the value of n just before you start using it in OutputToConsole().
  20. #11
  21. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,182
    Rep Power
    2222
    I just now saw that you had modified the opening post again. It is not a good practice to do that, since corrections you make to the code will render the original responses nonsensical. These messages are posted not just for your own benefit, but more for the benefit of others with similar types of problems who find these threads. Also, so much time is lost when you post request information back in Msg #1 instead of in a response to the message that made the request. I have no idea how long the test data has been sitting there, which means that all that time has been wasted.

    Please, from now on when you make corrections to your source code, do not replace the code in the original message, but rather post the corrected copy in a follow-up message. It helps all of us follow what's happening.
  22. #12
  23. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,182
    Rep Power
    2222
    I added cout statements in ReadData and in OutputToConsole to report the value of n. Here is what I get when I run it:
    Code:
    C:TEST>a
    Records read from file (n) = 5
    2                         COPYKATS PRINTING PAYROLL
    
    2    ID2     First2         Last2        SS#2 Hrs2 rHrs2  oT2 hRate2  MS2  tRate
    2   gPay2   oTpay2    tax2  Netpay
    --------------------------------------------------------------------------------
    -------------------
    n = 0
    2                         COPYKATS PRINTING PAYROLL
    
    2    ID2     First2         Last2        SS#2 Hrs2 rHrs2  oT2 hRate2  MS2  tRate
    2   gPay2   oTpay2    tax2  Netpay
    --------------------------------------------------------------------------------
    -------------------
    n = 0
    
    C:TEST>
    5 records in the file. 5 records read in. But n is zero when we try to output it.

    It turns out that the new sort function has nothing to do with it. Adding a cout to the sort I get:
    number of records to be sorted (n) = 0
    Instead, here is the problem:
    Code:
    int EmployeeId[100],n;   // as a global n is initialized to zero
    string FirstName[100],LastName[100],SSnumber[100];
    float HourlyRate[100],HoursWorked[100];
    char MaritalStatus[100];
    
    int main() 
    {
        ReadData();
        CalculateGrosspay();
        CalculateTaxRate();
        AdjustforMaritalStatus();
        CalculateNetPay();;
        OutputToConsole();
        sortnetpay();
        OutputToConsole();
        return 0;
    }
    
    //Read Data------------------------------------------------------------------------------------------------------
        
        void ReadData (void)
                {   
                    ifstream fin("employee.txt");
                      int n=0;  // this is a new variable unrelated to the global one
                          // this local n hides the global n, so that
                          //   all changes to n in this function is to the 
                          //   local n, leaving the global n unchanged
                    while(fin>>EmployeeId[n]>>FirstName[n]>>LastName[n]>>SSnumber[n]>>HoursWorked[n]>>HourlyRate[n]>>MaritalStatus[n])
                        {
                        n++;
                        }
                    fin.close();
    
            // the local n has been changed to 5, but the global n is still zero
                cout << "Records read from file (n) = " << n << endl;
                cout << "Global n = " << ::n << endl;
                }
    I had just added that cout for the global n ( ::n, review the section on scope) and this is what those two prints in ReadData output:
    Records read from file (n) = 5
    Global n = 0
    Do you understand what is happening there? When you declare a local variable of the same name, it creates a different and separate variable. Within that local scope all references to that variable name are made to the local one. One name for this is variable hiding, since the local variable "hides" the global variable.

    When I comment out that local declaration of n in ReadData, this is the output I get:
    Code:
    Records read from file (n) = 5
    Global n = 5
    2                         COPYKATS PRINTING PAYROLL
    
    2    ID2     First2         Last2        SS#2 Hrs2 rHrs2  oT2 hRate2  MS2  tRate2   gPay2   oTpay2    tax2  Netpay
    ---------------------------------------------------------------------------------------------------
    n = 5
    2  11112Jackson         2Brown        2159-55-4972              240.0000240.000040.00000413.00004M   40.1000004 520.00040.000004 52.00004 468.000
    
    2  22222Pink         2Floyd        2159-00-3657              235.0000235.000040.0000049.000004s   40.05000004 315.00040.000004 15.75004 299.250
    
    2  33332Elvis         2Presley        2462-40-5469              245.0000240.000045.0000048.500004H   40.000004 403.750463.75004 0.000004 403.750
    
    2  44442Chris         2Columbo        2123-45-6789              270.0000240.0000430.0000410.50004m   40.2000004 892.5004472.5004 178.5004 714.000
    
    2  55552Juan         2Valdez        2321-35-9731              270.0000240.0000430.000047.500004h   40.1000004 637.5004337.5004 63.75004 573.750
    
    number of records to be sorted (n) = 5
    2                         COPYKATS PRINTING PAYROLL
    
    2    ID2     First2         Last2        SS#2 Hrs2 rHrs2  oT2 hRate2  MS2  tRate2   gPay2   oTpay2    tax2  Netpay
    ---------------------------------------------------------------------------------------------------
    n = 5
    2  11112Jackson         2Brown        2159-55-4972              240.0000240.000040.00000413.00004M   40.1000004 520.00040.000004 52.00004 299.250
    
    2  22222Pink         2Floyd        2159-00-3657              235.0000235.000040.0000049.000004s   40.05000004 315.00040.000004 15.75004 0.00000
    
    2  33332Elvis         2Presley        2462-40-5469              245.0000240.000045.0000048.500004H   40.000004 403.750463.75004 0.000004 403.750
    
    2  44442Chris         2Columbo        2123-45-6789              270.0000240.0000430.0000410.50004m   40.2000004 892.5004472.5004 178.5004 468.000
    
    2  55552Juan         2Valdez        2321-35-9731              270.0000240.0000430.000047.500004h   40.1000004 637.5004337.5004 63.75004 573.750
    You still need to correct the sorting and to make it apply to entire records.
  24. #13
  25. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,182
    Rep Power
    2222
    So what's with all those extraneous "2"s? I don't know why, but it seems to be tied in with the ios::left and ios::right.

    For one thing, you don't need so many. Once you tell cout ios::left, then that's it from that point on or until you tell it otherwise.

    So I changed this code:
    Code:
    void OutputToConsole (void){
                cout.setf(ios::showpoint);
                
                //output header
                std::cout << ios::left <<setw(50) << "COPYKATS PRINTING PAYROLL"<<endl<<endl;
                std::cout << ios::left <<  setw(6) << "ID";
                std::cout << ios::left <<  setw(10) << "First";
                std::cout << ios::left <<  setw(13) << "Last";
                std::cout << ios::left <<  setw(11) << "SS#";
                std::cout << ios::left <<  setw(4) << "Hrs";
                std::cout << ios::left <<  setw(5) << "rHrs";
                std::cout << ios::left <<  setw(4) << "oT";
                std::cout << ios::left <<  setw(6) << "hRate";
                std::cout << ios::left <<  setw(4) << "MS";
                std::cout << ios::left <<  setw(7) << "tRate";
                std::cout << ios::left <<  setw(7) << "gPay";
                std::cout << ios::left <<  setw(8) << "oTpay";
                std::cout << ios::left <<  setw(7) << "tax";
                std::cout << ios::left <<  setw(8) << "Netpay" << endl;
                 std::cout<<"---------------------------------------------------------------------------------------------------"<<endl;    
                //output data
                int i=0;
                cout << "n = " << n << endl;
                while(i<n){
                    std::cout << ios::left << setw(6)<<EmployeeId[i];
                    std::cout << ios::left << setw(10)<<FirstName[i];
                    std::cout << ios::left << setw(9)<<LastName[i];
                    std::cout << ios::left << setw(15)<<SSnumber[i];
                    std::cout << ios::left << setw(4)<<HoursWorked[i];
                    std::cout << ios::left << setw(4)<<RegularHours[i];
                    std::cout << ios::right << setw(3)<<OverTimeHours[i];
                    std::cout << ios::right << setw(6)<<HourlyRate[i];
                    std::cout << ios::right << setw(4)<<MaritalStatus[i];
                    std::cout << ios::right << setw(6)<<TaxRate[i];
                    std::cout << ios::right << setw(8)<<GrossPay[i];
                    std::cout << ios::right << setw(7)<<OverTimePay[i];
                    std::cout << ios::right << setw(8)<<TaxAmount[i];
                    std::cout << ios::right << setw(8)<<NetPay[i]<<endl<<endl;  
                i++;
                    }
                    
            }
    to this:
    Code:
    void OutputToConsole (void){
                cout.setf(ios::showpoint);
                
                //output header
                std::cout << ios::left;
                std::cout <<setw(50) << "COPYKATS PRINTING PAYROLL"<<endl<<endl;
                std::cout <<  setw(6) << "ID";
                std::cout <<  setw(10) << "First";
                std::cout <<  setw(13) << "Last";
                std::cout <<  setw(11) << "SS#";
                std::cout <<  setw(4) << "Hrs";
                std::cout <<  setw(5) << "rHrs";
                std::cout <<  setw(4) << "oT";
                std::cout <<  setw(6) << "hRate";
                std::cout <<  setw(4) << "MS";
                std::cout <<  setw(7) << "tRate";
                std::cout <<  setw(7) << "gPay";
                std::cout <<  setw(8) << "oTpay";
                std::cout <<  setw(7) << "tax";
                std::cout <<  setw(8) << "Netpay" << endl;
                 std::cout<<"---------------------------------------------------------------------------------------------------"<<endl;    
                //output data
                int i=0;
                cout << "n = " << n << endl;
                while(i<n){
                    std::cout << ios::left;
                    std::cout << setw(6)<<EmployeeId[i];
                    std::cout << setw(10)<<FirstName[i];
                    std::cout << setw(9)<<LastName[i];
                    std::cout << setw(15)<<SSnumber[i];
                    std::cout << setw(4)<<HoursWorked[i];
                    std::cout << setw(4)<<RegularHours[i];
                    std::cout << ios::right;
                    std::cout << setw(3)<<OverTimeHours[i];
                    std::cout << setw(6)<<HourlyRate[i];
                    std::cout << setw(4)<<MaritalStatus[i];
                    std::cout << setw(6)<<TaxRate[i];
                    std::cout << setw(8)<<GrossPay[i];
                    std::cout << setw(7)<<OverTimePay[i];
                    std::cout << setw(8)<<TaxAmount[i];
                    std::cout << setw(8)<<NetPay[i]<<endl<<endl;  
                i++;
                    }
                    
            }
    Do you remember the mess you got with the original code, which I posted in Message #12? Well, after making this change, here is what I got:
    Code:
    Records read from file (n) = 5
    Global n = 5
    2                         COPYKATS PRINTING PAYROLL
    
        ID     First         Last        SS# Hrs rHrs  oT hRate  MS  tRate   gPay   oTpay    tax  Netpay
    ---------------------------------------------------------------------------------------------------
    n = 5
    2  1111JacksonBrown159-55-497240.000040.000040.0000013.0000M0.100000 520.0000.00000 52.0000 468.000
    
    2  2222PinkFloyd159-00-365735.000035.000040.000009.00000s0.0500000 315.0000.00000 15.7500 299.250
    
    2  3333ElvisPresley462-40-546945.000040.000045.000008.50000H0.00000 403.75063.7500 0.00000 403.750
    
    2  4444ChrisColumbo123-45-678970.000040.0000430.000010.5000m0.200000 892.500472.500 178.500 714.000
    
    2  5555JuanValdez321-35-973170.000040.0000430.00007.50000h0.100000 637.500337.500 63.7500 573.750
    
    number of records to be sorted (n) = 5
    2                         COPYKATS PRINTING PAYROLL
    
        ID     First         Last        SS# Hrs rHrs  oT hRate  MS  tRate   gPay   oTpay    tax  Netpay
    ---------------------------------------------------------------------------------------------------
    n = 5
    2  1111JacksonBrown159-55-497240.000040.000040.0000013.0000M0.100000 520.0000.00000 52.0000 299.250
    
    2  2222PinkFloyd159-00-365735.000035.000040.000009.00000s0.0500000 315.0000.00000 15.7500 0.00000
    
    2  3333ElvisPresley462-40-546945.000040.000045.000008.50000H0.00000 403.75063.7500 0.00000 403.750
    
    2  4444ChrisColumbo123-45-678970.000040.0000430.000010.5000m0.200000 892.500472.500 178.500 468.000
    
    2  5555JuanValdez321-35-973170.000040.0000430.00007.50000h0.100000 637.500337.500 63.7500 573.750
    Why does treating left and right differently make such a difference? I don't know. I learned C++ in 1991 and from the beginning I couldn't find any practical use for iostreams so I never used it. I don't know all the little tricks to using it. Maybe somebody else can explain it.

    Comments on this post

    • popsimath agrees : Thanks for the help!
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Location
    New York
    Posts
    18
    Rep Power
    0
    OK Thanks for your help dwise1, I learned a lot on this one.
    Once I learned to put Couts all over the place and fixed the variable hiding problem I started outputing the loop variables so I could see if was getting through. If the loop variable is set one digit off you get junk which is what was appearing as a 32 or 2 after my intended data printed. I also printed the entire array after read in, before sorting and after sorting to see what was going on then commented out all but what was need for the class. that was huge for me.

    Code:
    # include <iostream>
    # include <fstream>
    # include <string>
    # include <iomanip>
    
    void ReadData(void);	
    void CalculateGrosspay(void);
    void CalculateTaxRate(void);
    void AdjustforMaritalStatus(void);
    void CalculateNetPay(void);
    void sortnetpay(void);
    void OutputToConsole(void);
    //void	OutputDataAsRead(void);
    
    using namespace std;
    
    float RegularHours[100],OverTimeHours[100];
    float TaxRate[100],TaxAmount[100],GrossPay[100],RegularPay[100],OverTimePay[100],NetPay[100];
    int EmployeeId[100],n;
    string FirstName[100],LastName[100],SSnumber[100];
    float HourlyRate[100],HoursWorked[100];
    char MaritalStatus[100];
    
    int main() 
    {
    	ReadData();
    //	OutputDataAsRead();
    	CalculateGrosspay();
    	CalculateTaxRate();
    	AdjustforMaritalStatus();
    	CalculateNetPay();
    //cout  << "Before Sort ";
    //	OutputToConsole();
    	sortnetpay();
    cout << "Sorted by Net Pay " << endl;
    	OutputToConsole();
    	return 0;
    }
    
    //Read Data------------------------------------------------------------------------------------------------------
    	
    	void ReadData (void)
    			{	
    				ifstream fin("employeesort.txt");
    				  int N=0;
    				while(fin>>EmployeeId[N]>>FirstName[N]>>LastName[N]>>SSnumber[N]>>HoursWorked[N]>>HourlyRate[N]>>MaritalStatus[N])
    					{
    					N++;
    					}
    				n=N;
    				fin.close();
    				
    
    			}
    //Calculate Gross pay------------------------------------------------------------------------------------------------------	
    
    	void CalculateGrosspay (void)
    			{
    				int i=0;
    				while(i<n)
    					{
    						if (HoursWorked[i] > 40)
    							{
     								OverTimeHours[i] = HoursWorked[i]-40;
     								RegularHours[i]=40;
    							}
    						else 
    							{
    								OverTimeHours[i]=0;
    								RegularHours[i]=HoursWorked[i];
    							}
    					GrossPay[i]=(RegularHours[i]*HourlyRate[i])+(OverTimeHours[i]*(HourlyRate[i]*1.5));
    		
    						i++;	
    					}
    		
    			}
    			
    //Calculate Tax Rate------------------------------------------------------------------------------------------------------
    				
    	void	CalculateTaxRate(void)
    		{
    	
    		int i=0;
    		while(i<n)// Decide on Tax Rate
    		{
    			if (GrossPay[i]>1000)
    				{ 
    				TaxRate[i]=.30;
    				}
    			else if (GrossPay[i] > 800 && GrossPay[i]<=1000)
    				{
    	 			TaxRate[i]=.20;
    				}
    			else if (GrossPay[i]> 500 && GrossPay[i]<= 800)
    				{
    				TaxRate[i]=.10;
    				}
    			else if (GrossPay[i]<= 500)
    				{
    				TaxRate[i]=0;
    				}	
    			i++;	
    		}
    	}
    //Adjust for Marital Status------------------------------------------------------------------------------------------------------		
    		
    void AdjustforMaritalStatus (void)	
    	{
    		int i=0;
    		while(i<n)//Adjust tax rate based on marital status
    		{
    			if (MaritalStatus[i] == 'S' | MaritalStatus[i] =='s')
    				{
    				TaxRate[i]=TaxRate[i]+.05;
    				}
    			else if (MaritalStatus[i] == 'M' | 'm')
    				{
    				TaxRate[i]=TaxRate[i];
    				}
    			else if (MaritalStatus[i] == 'H' | 'h')
    				{
    				if (TaxRate[i]>0)
    					{
    					TaxRate[i]=TaxRate[i]-.05;
    					}		
    					TaxRate[i]=TaxRate[i]-.05;
    				}
    			i++;
    		}
    	}
    //Calculate Net Pay------------------------------------------------------------------------------------------------------	
    			
    	void CalculateNetPay (void)
    		{
    			int i=0;
    			while(i<n)//Calculate Tax and Net pay
    				{				
    					// calculate TAX Amount		
    					TaxAmount[i]=TaxRate[i]*GrossPay[i];
    					// calculate NET pay
    					NetPay[i]=GrossPay[i]-TaxAmount[i];
    					// Calculate Overtime Pay
    					OverTimePay[i]=OverTimeHours[i]*(HourlyRate[i]*1.5);
    					i++;
    
    				}
    		}
    	
    
    void sortnetpay(void){
    			char chartemp;
    			double temp=0;
    			float floatTemp=0;
    			int intTemp;
    			string stringTemp;
    				for(int pass=0;pass<(n);pass++){
    			//	cout << "pass " << pass << endl;
    
    					for(int scan=0;scan<(n-1);scan++){
    				//	cout << "scan " << scan << endl;
    
    						if (NetPay[scan]>NetPay[(scan+1)]){
    					//	cout << "before NetPay[scan] " << NetPay[scan] << "NetPay[scan+1] " << NetPay[scan+1] << endl;
    
    					  	 	temp=NetPay[scan+1];
    							NetPay[scan+1]=NetPay[scan];
    							NetPay[scan]=temp;
    
    							intTemp=EmployeeId[scan+1];
    							EmployeeId[scan+1]=EmployeeId[scan];
    							EmployeeId[scan]=intTemp;
    
    							stringTemp=FirstName[scan+1];
    							FirstName[scan+1]=FirstName[scan];
    							FirstName[scan]=stringTemp;
    
    							stringTemp=LastName[scan+1];
    							LastName[scan+1]=LastName[scan];
    							LastName[scan]=stringTemp;
    
    							stringTemp=SSnumber[scan+1];
    							SSnumber[scan+1]=SSnumber[scan];
    							SSnumber[scan]=stringTemp;
    
    							floatTemp=HoursWorked[scan+1];
    							HoursWorked[scan+1]=HoursWorked[scan];
    							HoursWorked[scan]=floatTemp;
    
    							floatTemp=HourlyRate[scan+1];
    							HourlyRate[scan+1]=HourlyRate[scan];
    							HourlyRate[scan]=floatTemp;
    
    							floatTemp=RegularHours[scan+1];
    							RegularHours[scan+1]=RegularHours[scan];
    							RegularHours[scan]=floatTemp;
    
    							floatTemp=OverTimeHours[scan+1];
    							OverTimeHours[scan+1]=OverTimeHours[scan];
    							OverTimeHours[scan]=floatTemp;
    
    							chartemp=MaritalStatus[scan+1];
    							MaritalStatus[scan+1]=MaritalStatus[scan];
    							MaritalStatus[scan]=chartemp;
    
    							floatTemp=TaxRate[scan+1];
    							TaxRate[scan+1]=TaxRate[scan];
    							TaxRate[scan]=floatTemp;
    
    							floatTemp=GrossPay[scan+1];
    							GrossPay[scan+1]=GrossPay[scan];
    							GrossPay[scan]=floatTemp;
    
    							floatTemp=OverTimePay[scan+1];
    							OverTimePay[scan+1]=OverTimePay[scan];
    							OverTimePay[scan]=floatTemp;
    
    							floatTemp=TaxAmount[scan+1];
    							TaxAmount[scan+1]=TaxAmount[scan];
    							TaxAmount[scan]=floatTemp;
    
    
    						//	cout << "after NetPay[scan] " << NetPay[scan] << "NetPay[scan+1] " << NetPay[scan+1] << endl;	
    					 	 }
    					}//end scan
    				}//end pass
    
    			}//end sort
    		
    
    //Output to Console------------------------------------------------------------------------------------------------------								
    
    void OutputToConsole (void){
    		
    			cout << endl << endl;
    			//output header
    			cout << left <<setw(30) << "COPYKATS PRINTING PAYROLL"<<endl<<endl;
    			cout << left <<  setw(6) << "ID";
    			cout << left <<  setw(10) << "First";
    			cout << left <<  setw(13) << "Last";
    			cout << left <<  setw(11) << "SS#";
    			cout << left <<  setw(4) << "Hrs";
    			cout << left <<  setw(5) << "rHrs";
    			cout << left <<  setw(4) << "oT";
    			cout << left <<  setw(6) << "hRate";
    			cout << left <<  setw(4) << "MS";
    			cout << left <<  setw(7) << "tRate";
    			cout << left <<  setw(7) << "gPay";
    			cout << left <<  setw(8) << "oTpay";
    			cout << left <<  setw(7) << "tax";
    			cout << left <<  setw(8) << "Netpay" << endl;
    		     cout<<"---------------------------------------------------------------------------------------------------"<<endl;	
    			//output data
    			int i=0;
    			while(i<n){
    
    				cout << left << setw(6)<<EmployeeId[i];
    				cout << left << setw(10)<<FirstName[i];
    				cout << left << setw(9)<<LastName[i];
    				cout << left << setw(15)<<SSnumber[i];
    				cout << left << setw(4)<<HoursWorked[i];
    				cout << left << setw(4)<<RegularHours[i];
    				cout << right << setw(3)<<OverTimeHours[i];
    				cout << right << setw(6)<<HourlyRate[i];
    				cout << right << setw(4)<<MaritalStatus[i];
    				cout << right << setw(6)<<TaxRate[i];
    				cout << right << setw(8)<<GrossPay[i];
    				cout << right << setw(7)<<OverTimePay[i];
    				cout << right << setw(8)<<TaxAmount[i];
    				cout << right << setw(8)<<NetPay[i]<<endl<<endl;	
    			i++;
    				}
    						cout << endl << endl << endl << endl;
    
    		}
    /*		
    void	OutputDataAsRead(void){
    
    			cout << endl << endl;
    			//output header
    			cout << left <<setw(30) << "COPYKATS PRINTING PAYROLL DATA READ IN"<<endl<<endl;
    			cout << left <<  setw(6) << "ID";
    			cout << left <<  setw(10) << "First";
    			cout << left <<  setw(13) << "Last";
    			cout << left <<  setw(11) << "SS#";
    			cout << left <<  setw(4) << "Hrs";
    			cout << left <<  setw(6) << "hRate";
    			cout << left <<  setw(4) << "MS" << endl;
    		     cout<<"--------------------------------------------------------"<<endl;	
    			//output data
    			int i=0;
    			while(i<n){
    
    				cout << left << setw(6)<<EmployeeId[i];
    				cout << left << setw(10)<<FirstName[i];
    				cout << left << setw(9)<<LastName[i];
    				cout << left << setw(15)<<SSnumber[i];
    				cout << left << setw(4)<<HoursWorked[i];
    				cout << right << setw(6)<<HourlyRate[i];
    				cout << right << setw(4)<<MaritalStatus[i]<< endl;
    			i++;
    
    				}
    
    }
    */

IMN logo majestic logo threadwatch logo seochat tools logo