Thread: Newbie - i need a total of list of numbers

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    17

    Question Newbie - i need a total of list of numbers (I posted code)

    I am working on a project that calculates gross pay and net pay from an imported file that contains 4 employees and their rate per hour and their number of hours worked. I can import the file and do the calculations perfectly. The part I can't do.

    I am supposed to sum up all of the employees gross pay and net pay to get the total for the company. So the file you are looking at looks something like this for example:
    employee gross pay
    1 50
    2 100
    3 75

    I need to sum the 50+100+75 to get a total but my program needs to sum a thousand numbers if need be. My problem is that I am just using a standard; totgross=totgross +totgross but that is just adding 75 and 75.
    Last edited by sugie; 02-25-2005 at 02:33 PM. Reason: adfdfasf

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    It would help if you posted the code that you've written. That will give us an idea of what you've already tried.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    How are you storing the gross and net pay? Are you saving them in a container of some sort (array/vector etc...) or are you simply trying to keep running totals as you read in your employee data. Show some code!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    17
    yeah, that's on a different computer. can't post it right now.

    I can tell you this....I import a file that has the employee information and do calculations to it to get gross, net and all taxes. that spits out everything perfectly. after the calculations i run a PrintSummary program that messes up.

    if this is still no help i'll post my code later.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    17
    trying to keep running.

  6. #6
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88
    don't worry m8 , i can;t tell you , 50 + 100 + 75 = 225 , problem solved :P

    but seriuos , if you do totgros = totgros + totgros and and the awnser is 150 it will be the same as 75 + 75 . what i wouyld suggest ( i am still a noob too but ) just make 3 variables so

    Code:
    int a ;
    int b ;
    int c ;
    
    totgros = a + b + c
    or if it already holds a value ( for example c = 75 ) just let it go like :

    Code:
    totgros = a + b
    as simple as it is . if this didn;t solve the problem i must have understood your question wrong , it would help if you post the code you have so far so we Xactly see what you do mean.

    PLay MystWind beta , within two years

  7. #7
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88
    hmm don't think you ment that after all .


    medic ! I NEED CODE ! CODE !
    PLay MystWind beta , within two years

  8. #8
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Something along the lines of an integer storing how many employees, a loop, and an array?
    Code:
    const int employees = 10; //ten employees
    int array[10] //this stores salary of each employee, simply read in the values
    int total;
    for( int I = 0; I < employees; I++ )
    {
      Total += array[I];
    }
    Um...?
    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    17

    here is my code.

    at the bottom where i keep messing up i had to insert the "//" b/c i could not get past them.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    #include <fstream>
    
    void ProcessPayroll(float&, float&, float&, float&, float&, ifstream&, ofstream&);
    int ProcessEmployee(int&, float&, int&, ifstream&);
    void PrintSummary(float, float, float, float, float, ofstream&);
    int main()
    {
    	ifstream infile;
    	infile.open ("lab1.txt");
    	if(infile.fail())
    	{
    		cout << "failing to open input file\n";
    		getchar();
    		exit(1);
    	}
    
    	ofstream outfile;
    	outfile.open ("out1.txt");
    	if(outfile.fail())
    	{
    		cout << "failing to open output file\n";
    		getchar();
    		exit(1);
    	}
    	float totgross, totss, totfed, totstate, totnet;
    	ProcessPayroll(totgross, totss, totfed, totstate, totnet, infile, outfile);
    	getchar();
    	return 0;
    }
    
    
    int ProcessEmployee(int& EmpNum, float& Rate, int& Hours, ifstream& input)
    {
    	if(input >> EmpNum >> Rate >> Hours)
    	     return 1;
    	else
    	     return 0;
    }
    
    
    void ProcessPayroll(float& totgross, float& totss, float& totfed, float& totstate, float& totnet, ifstream& input, ofstream& outfile)
    {
    	float Rate;	
    	int EmpNum, Hours;
    	char flag = ' ';
    
    	cout << "Employee" << "  " << "Hours" << "  " << " Rate" << "  " << " Gross" << "  " 
    	     << "   Net" << "  " << "   Fed" << "  " << "State" << "  " << "Soc Sec\n";
    	outfile << "Employee" << "  " << "Hours" << "  " << " Rate" << "  " << " Gross" << "  " 
    	     << "   Net" << "  " << "   Fed" << "  " << "State" << "  " << "Soc Sec\n";
    	
    
    	while(ProcessEmployee(EmpNum, Rate, Hours, input))
    	{
    	if(Hours > 40)
    	{
    		totgross=((Rate * 1.5)*(Hours - 40)) + (Rate * 40);
    	    flag = '$';
    	}
    	else if(Hours > 35)
    	{
    		totgross=Rate * Hours;
    	    flag = ' ';
    	}
    	else if(Hours <= 35)
    	{	
    		totgross=(Rate + .15) * Hours;
    	    flag = '*';
    	}
    
    	totss = totgross*.07;
    	totfed = totgross*.16;
    	totstate = totgross*.0542;
    	totnet = totgross - (totss + totfed + totstate);
    	
    	cout << setw(8) << EmpNum << "  " 
    		 << setw(5) << Hours  << flag << " "
    		 << setw(5) << setiosflags(ios::fixed) << setprecision(2) << Rate << "  " 
    	     << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << "  "
    		 << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << "  "
    		 << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << "  "
    		 << setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << "    "
    		 << setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";
    	
    	
    	outfile << setw(8) << EmpNum << "  " 
    		 << setw(5) << Hours  << flag << " "
    		 << setw(5) << setiosflags(ios::fixed) << setprecision(2) << Rate << "  " 
    	     << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << "  "
    		 << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << "  "
    		 << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << "  "
    		 << setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << "    "
    		 << setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";
    	}
    	PrintSummary(totgross, totss, totfed, totstate, totnet, outfile);
    
    	return;
    }
    
    void PrintSummary(float totgross, float totss, float totfed, float totstate, float totnet, ofstream& outfile)
    {
    	//float totgross, totss, totfed, totstate, totnet;
        
    	cout << "\n\n\nSummary - Totals For All Employees" << "\n\n  " 
    		 << "Gross Pay" << "   " << "Net Pay" << "   " << "Federal Tax" << "   " 
    	     << "State Tax" << "   " << "Soc Security\n";
    	
    	outfile << "\n\n\nSummary - Totals For All Employees" << "\n\n  " 
    		    << "Gross Pay" << "   " << "Net Pay" << "   " << "Federal Tax" << "   " 
    	        << "State Tax" << "   " << "Soc Security\n";
    
    	//while(ProcessPayroll(totgross, totss, totfed, totstate, totnet, input, outfile))
    	{
    	totgross=totgross+totgross;
    	totnet=totnet+totnet;
    	totfed=totfed+totfed;
    	totstate=totstate+totstate;
    	totss=totss+totss;
    
    	cout << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << "  "
    		 << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << "  "
    		 << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << "  "
    		 << setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << "    "
    		 << setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";
    
    	outfile << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << "  "
    		    << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << "  "
    		    << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << "  "
    		    << setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << "    "
    		    << setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";
    
    	}
    	outfile.close();		
    	return;
    }
    Last edited by sugie; 02-25-2005 at 01:53 PM.

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>  // Moved this from two lines below
    using namespace std;
    
    void ProcessPayroll(float&, float&, float&, float&, float&, ifstream&, ofstream&);
    int ProcessEmployee(int&, float&, int&, ifstream&);
    void PrintSummary(float, float, float, float, float, ofstream&);
    int main()
    {
        ifstream infile;
        infile.open ("lab1.txt");
        if(infile.fail())
        {
            cout << "failing to open input file\n";
            getchar();
            exit(1);
        }
    
        ofstream outfile;
        outfile.open ("out1.txt");
        if(outfile.fail())
        {
            cout << "failing to open output file\n";
            getchar();
            exit(1);
        }
        float totgross, totss, totfed, totstate, totnet;
        // Added line below, should initialize values before you use them
        totgross = totss = totfed = totstate = totnet = 0.0f;
        ProcessPayroll(totgross, totss, totfed, totstate, totnet, infile, outfile);
        getchar();
        return 0;
    }
    
    
    int ProcessEmployee(int& EmpNum, float& Rate, int& Hours, ifstream& input)
    {
        if(input >> EmpNum >> Rate >> Hours)
            return 1;
        else
            return 0;
    }
    
    
    void ProcessPayroll(float& totgross, float& totss, float& totfed, float& totstate,
                        float& totnet, ifstream& input, ofstream& outfile)
    {
        float Rate;	
        int EmpNum, Hours;
        char flag = ' ';
        // New variables added
        float gross, net, ss, state, fed;
    
        cout << "Employee" << "  " << "Hours" << "  " << " Rate" << "  " << " Gross" << "  " 
             << "   Net" << "  " << "   Fed" << "  " << "State" << "  " << "Soc Sec\n";
        outfile << "Employee" << "  " << "Hours" << "  " << " Rate" << "  " << " Gross" << "  " 
                << "   Net" << "  " << "   Fed" << "  " << "State" << "  " << "Soc Sec\n";
    	
    
        while(ProcessEmployee(EmpNum, Rate, Hours, input))
        {
            if(Hours > 40)
            {
                gross = ((Rate * 1.5)*(Hours - 40)) + (Rate * 40);
                flag = '$';
            }
            else if(Hours > 35)
            {
                gross = Rate * Hours;
                flag = ' ';
            }
            else if(Hours <= 35)
            {	
                gross = (Rate + .15) * Hours;
                flag = '*';
            }
    
            ss = gross * 0.07;
            fed = gross * 0.16;
            state = gross * 0.0542;
            net = gross - state - fed - state;
    
            totss += ss;
            totfed += fed;
            totstate += state;
            totnet += net;
            totgross += gross;
    	
            cout << setw(8) << EmpNum << "  " 
                 << setw(5) << Hours  << flag << " "
                 << setw(5) << setiosflags(ios::fixed) << setprecision(2) << Rate << "  " 
                 << setw(6) << setiosflags(ios::fixed) << setprecision(2) << gross << "  "
                 << setw(6) << setiosflags(ios::fixed) << setprecision(2) << net << "  "
                 << setw(6) << setiosflags(ios::fixed) << setprecision(2) << fed << "  "
                 << setw(5) << setiosflags(ios::fixed) << setprecision(2) << state << "    "
                 << setw(5) << setiosflags(ios::fixed) << setprecision(2) << ss << "\n";
    	
            outfile << setw(8) << EmpNum << "  " 
                    << setw(5) << Hours  << flag << " "
                    << setw(5) << setiosflags(ios::fixed) << setprecision(2) << Rate << "  " 
                    << setw(6) << setiosflags(ios::fixed) << setprecision(2) << gross << "  "
                    << setw(6) << setiosflags(ios::fixed) << setprecision(2) << net << "  "
                    << setw(6) << setiosflags(ios::fixed) << setprecision(2) << fed << "  "
                    << setw(5) << setiosflags(ios::fixed) << setprecision(2) << state << "    "
                    << setw(5) << setiosflags(ios::fixed) << setprecision(2) << ss << "\n";
        }
    
        PrintSummary(totgross, totss, totfed, totstate, totnet, outfile);
    
        return;
    }
    
    void PrintSummary(float totgross, float totss, float totfed, float totstate, float totnet,
                      ofstream& outfile)
    {
        //float totgross, totss, totfed, totstate, totnet;
        
        cout << "\n\n\nSummary - Totals For All Employees" << "\n\n  " 
             << "Gross Pay" << "   " << "Net Pay" << "   " << "Federal Tax" << "   " 
             << "State Tax" << "   " << "Soc Security\n";
    	
        outfile << "\n\n\nSummary - Totals For All Employees" << "\n\n  " 
                << "Gross Pay" << "   " << "Net Pay" << "   " << "Federal Tax" << "   " 
                << "State Tax" << "   " << "Soc Security\n";
    
        // Get rid of all this below in blue
        //while(ProcessPayroll(totgross, totss, totfed, totstate, totnet, input, outfile))
        {
        totgross=totgross+totgross;
        totnet=totnet+totnet;
        totfed=totfed+totfed;
        totstate=totstate+totstate;
        totss=totss+totss;
    
        cout << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << "  "
             << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << "  "
             << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << "  "
             << setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << "    "
             << setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";
    
        outfile << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totgross << "  "
                << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totnet << "  "
                << setw(6) << setiosflags(ios::fixed) << setprecision(2) << totfed << "  "
                << setw(5) << setiosflags(ios::fixed) << setprecision(2) << totstate << "    "
                << setw(5) << setiosflags(ios::fixed) << setprecision(2) << totss << "\n";
    
        // Get rid of the following in blue
        }
    
        outfile.close();		
        return;
    }
    Note, your main function has no need to keep the totfed, totss, totstate, totgross, and totnet variables local to it. main does not use them anywhere so they should be moved into the ProcessPayroll function. This means that the function prototype should be changed to only have the infile and outfile file stream objects.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM