Finding the sum and average

Hello everyone! I am once again trying to figure out some issues I am having in trying to understand C++. I am trying to write a Program that will find the Sum of Netpay data from a datasheet. From there, I am trying to find the average of that Sum. For instance, I have 6 names in the datasheet, all with a Netpay calculated within the code. What I am trying to do is get those numbers added up and then averaged. Here is what I have thus far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

void printalldata(char[][15],char[][15],float[],float,float, int);

float findsum(float net[], int n){
    float sum=0.0;
        for(int i=0;i<n;i++){
        sum=sum+net[i];
        }//FOR
        return sum;
}//FINDSUM

float findaverage(float sum, int n){
    float average;
        average=sum/n;
        return average;
}//FINDAVERAGE

int main(int argc, char *argv[]){

    char stat[100];

    char fn[100][14], ln[100][15];

    float ssn[100], hw[100], hr[100], oth[100], otp[100], regp[100], gross[100], taxamnt[100];

    float taxrate[100];

    float net[100];

    float findsum(float[],int);

    float sum;

    float findaverage(float[], int);

    float average;

    int i=0;

            //cout<<"PLEASE ENTER THE EMPLOYEE'S FIRST NAME:";
            //cin>> fn[100];
            //cout<<"PLEASE ENTER THE EMPLOYEE'S LAST NAME:";
            //cin>> ln[100];
            //cout<<endl;

    cout<<"__________________________________________________________________________"<<endl;
    cout<<"DR. EBRAHIMI'S PAYROLL INSTITUTE"<<endl;
    cout<<"=========================================================================="<<endl;

    ifstream fin("employeeM4.txt");

    cout<<setw(2)<<"FIRST"<<setw(8)<<"LAST"<<setw(8)<<"STAT"<<setw(5)<<"SSN"<<setw(5)

<<"HW"<<setw(4)<<"HR"<<setw(4)<<"OTH"<<setw(5)<<"OTP"<<setw(5)<<"REGP"<<setw(6)

<<"GROSS"<<setw(6)<<"TAX"<<setw(6)<<"NET"<<endl;

    cout<<"=========================================================================="<<endl;

    while(fin>>fn[i]>>ln[i]>>stat[i]>>ssn[i]>>hw[i]>>hr[i]){

        if(hw[i]>40){

                     oth[i]=hw[i]-40;

                     otp[i]=oth[i]*hr[i]*1.5;

                     regp[i]=40*hr[i];

                     }

        else{

             oth[i]=0;

             otp[i]=0;

             regp[i]=hw[i]*hr[i];

             }

        gross[i] = regp[i]+otp[i];

            if(gross[i]>1000) taxrate[i] = .30;

            else if(gross[i]>800) taxrate[i] = .20;

            else if(gross[i]>500) taxrate[i] = .10;

            else taxrate[i] = 0;

        taxamnt[i] = gross[i]*taxrate[i];

        net[i] = gross[i]-taxamnt[i];

cout<<fn[i]<<setw(10)<<ln[i]<<setw(5)<<stat[i]<<setw(7)<<ssn[i]<<setw(5)<<hw[i]<<setw(4)<<hr[i]<<setw(4)<<oth[i]

<<setw(4)<<otp[i]<<setw(5)<<regp[i]<<setw(6)<<gross[i]<<setw(6)<<taxamnt[i]<<setw(6)<<setprecision( 4 )<<net[i]<<endl<<endl;

        }//WHILE

        cout<<endl<<"The sum of the Employees' Netpay is: "<<sum<<endl<<endl;

        cout<<"The average Employee Netpay is: "<<average<<endl;
    cin.get();
    return 0;
}//MAIN 

"employeeM4.txt" is my datasheet. It looks like this:
1
2
3
4
5
6
Annie Apple   M 1111 40 20
Brian Brown   H 2222 42 15
Candy Cane    S 3333 47 45
Danny Day     S 4444 45 10
Erin Edward   H 5555 30 90
Frank Fella   M 6666 50 30

Basically, the program is functioning correctly and finding the Netpay of each person. But, the Sum and Average are printing a "0" for the answer.

Any help would be most appreciated! Thanks in advance :)
Last edited on
You never call findaverage and findsum in main. The sum and average variables in main is still uninitialized when you print them.
Main does not call either function at any point?
You never call findaverage and findsum in main. The sum and average variables in main is still uninitialized when you print them.

I thought this would call them:
1
2
3
        cout<<endl<<"The sum of the Employees' Netpay is: "<<sum<<endl<<endl;

        cout<<"The average Employee Netpay is: "<<average<<endl;

Since that is not the case, what is it that I am missing? Since "findsum" and "findaverage" are functions, I had to place them outside of the main function. I thought by calling "sum" and "average" in my cout would print the calculations.
1
2
3
4
5
6
7
8
9
10
void function()
{
//stuff the function does
}

int main()
{
    function(); //Function call
    return 0; 
}


Further reading:

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
^^Thanks, Mats. I am going to read the info in the links you provided and hopefully, I will get a better understanding of what I am doing wrong and how to fix it. I appreciate it!
Come back and ask if something doesn't make sense or still will not work. :)
Last edited on
Ok, I made some edits, but it still isn't working :( Not sure what I am doing wrong. I have been working on this after reading those links over and over, but I am still missing something. Here is what I have now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

void findnet(float gross[100], float taxamnt[100], float net[100]);

void findsum(float sum);

void findaverage(float sum, float average);

int main(int argc, char *argv[]){

    char stat[100];

    char fn[100][14], ln[100][15];

    float ssn[100], hw[100], hr[100], oth[100], otp[100], regp[100], gross[100], taxamnt[100];

    float taxrate[100];

    float net[100];

    float findsum(float[],int);

    float sum;

    float findaverage(float[], int);

    float average;

    int i=0;

            //cout<<"PLEASE ENTER THE EMPLOYEE'S FIRST NAME:";
            //cin>> fn[100];
            //cout<<"PLEASE ENTER THE EMPLOYEE'S LAST NAME:";
            //cin>> ln[100];
            //cout<<endl;

    cout<<"__________________________________________________________________________"<<endl;
    cout<<"DR. EBRAHIMI'S PAYROLL INSTITUTE"<<endl;
    cout<<"=========================================================================="<<endl;

    ifstream fin("employeeM4.txt");

    cout<<setw(2)<<"FIRST"<<setw(8)<<"LAST"<<setw(8)<<"STAT"<<setw(5)<<"SSN"<<setw(5)

<<"HW"<<setw(4)<<"HR"<<setw(4)<<"OTH"<<setw(5)<<"OTP"<<setw(5)<<"REGP"<<setw(6)

<<"GROSS"<<setw(6)<<"TAX"<<setw(6)<<"NET"<<endl;

    cout<<"=========================================================================="<<endl;

    while(fin>>fn[i]>>ln[i]>>stat[i]>>ssn[i]>>hw[i]>>hr[i]){

        if(hw[i]>40){

                     oth[i]=hw[i]-40;

                     otp[i]=oth[i]*hr[i]*1.5;

                     regp[i]=40*hr[i];

                     }

        else{

             oth[i]=0;

             otp[i]=0;

             regp[i]=hw[i]*hr[i];

             }

        gross[i] = regp[i]+otp[i];

            if(gross[i]>1000) taxrate[i] = .30;

            else if(gross[i]>800) taxrate[i] = .20;

            else if(gross[i]>500) taxrate[i] = .10;

            else taxrate[i] = 0;

        taxamnt[i] = gross[i]*taxrate[i];

        findnet();
        return 0;

cout<<fn[i]<<setw(10)<<ln[i]<<setw(5)<<stat[i]<<setw(7)<<ssn[i]<<setw(5)<<hw[i]<<setw(4)<<hr[i]<<setw(4)<<oth[i]

<<setw(4)<<otp[i]<<setw(5)<<regp[i]<<setw(6)<<gross[i]<<setw(6)<<taxamnt[i]<<setw(6)<<setprecision( 4 )<<net[i]<<endl<<endl;

        }//WHILE

        cout<<endl<<"The sum of the Employees' Netpay is: "<<sum<<endl<<endl;

        cout<<"The average Employee Netpay is: "<<average<<endl;
    cin.get();
    return 0;
}//MAIN

void findnet(float gross[100], float taxamnt[100], float net[100]){

    net[100] = gross[100]-taxamnt[100];
        return 0;
}//FINDNET

void findsum(float sum){

        findsum();
        int sum<=6, sum++;
        sum=sum+net[100];
        return 0;
}//FINDSUM

void findaverage(float sum, float average){

        findaverage();
        average=sum/6;
        return 0;
}//FINDAVERAGE 

Any ideas on what I am doing wrong? Thanks in advance!
line 90: return 0; that will terminate your program.

1
2
3
4
5
6
7
void findsum(float sum){

        findsum();
        int sum<=6, sum++;
        sum=sum+net[100];
        return 0; 
}//FINDSUM 
Please explain what the hell do you think you are doing there. (line by line)
Last edited on
Line 1: he declares a function which returns no value and takes a float parameter named sum. Then for some reason he places the opening curly brace on the same line and leaves line 2 blank.
Line 2: he put the curly brace that would have gone on this line at the end of line 1 instead.
Line 3: he recursively calls findsum for no reason, and he also neglects to give it the required float parameter.
Line 4: he declares an integer named sum, even though his float parameter already took this name, and then instead of using = to assign a default value, he uses <=. He then uses the comma syntax making the 6 get evaluated but become useless, and what is actually taken is the value of sum++, but it is unknown at this point which sum variable this refers to.
Line 5: instead of using the += operator, he decides to set sum (which one?) to the value of itself (which one?) plus the value at the 101st element in the array net, which does not even exist in this scope.
Line 6: he uses a useless return statement, but instead of ending there he tries to return a value of 0, even though his findsum function has been declared to return no value.
Line 7: he ends off his function definition with the ending curly brace, and then leaves a comment in ALL CAPS delcaring that this curly brace is the end to the extremely short findsum function.

Sorry for being silly, ne555
Last edited on
Dear LB, I want emets31's opinion.
Sorry for not being clear enough.
I was being silly, I knew what you meant. I was giving him big hints ;)
Well, honestly, I don't know. That came partly from an Instructor's suggestion, and partly from my apparent error. My original looked like this:
1
2
3
4
5
6
7
void findsum(int net){

        float sum;
        sum<=6, sum++;
        sum=sum+net[100];
        return sum; 
}//FINDSUM  

But, this wasn't working, so I tried something else. Basically, what I think the above code would do is:

Define the findsum function, which is supposed to find the total net pay amount for 6 people (adding up all 6). But, as you can see, I am lost at that point. I have been trying to figure out a way for the "net" values to be added up, but to no avail.
Ok, I decided to start over, since I was getting so many errors. Here is what I have now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

float findsum(float net, float sum, int n);

float findaverage(float average, float sum, int n);

int main(int argc, char *argv[]){

    char stat[100];

    char fn[100][14], ln[100][15];

    float ssn[100], hw[100], hr[100], oth[100], otp[100], regp[100], gross[100], taxamnt[100], net[100];

    float taxrate[100];

    float sum;

    float average;

    int i=0;

    cout<<"__________________________________________________________________________"<<endl;
    cout<<"DR. EBRAHIMI'S PAYROLL INSTITUTE"<<endl;
    cout<<"=========================================================================="<<endl;

    ifstream fin("employeeM4.txt");

    cout<<setw(2)<<"FIRST"<<setw(8)<<"LAST"<<setw(8)<<"STAT"<<setw(5)<<"SSN"<<setw(5)<<"HW"<<setw(4)<<"HR"<<setw(4)
<<"OTH"<<setw(5)<<"OTP"<<setw(5)<<"REGP"<<setw(6)<<"GROSS"<<setw(6)<<"TAX"<<setw(6)<<"NET"<<endl;

    cout<<"=========================================================================="<<endl;

    while(fin>>fn[i]>>ln[i]>>stat[i]>>ssn[i]>>hw[i]>>hr[i]){

        if(hw[i]>40){

                     oth[i]=hw[i]-40;

                     otp[i]=oth[i]*hr[i]*1.5;

                     regp[i]=40*hr[i];

                     }

        else{

             oth[i]=0;

             otp[i]=0;

             regp[i]=hw[i]*hr[i];

             }

        gross[i] = regp[i]+otp[i];

            if(gross[i]>1000) taxrate[i] = .30;

            else if(gross[i]>800) taxrate[i] = .20;

            else if(gross[i]>500) taxrate[i] = .10;

            else taxrate[i] = 0;

            //if(stat[i]==S) taxrate[i] = taxrate[i]+.05;

            //else if(stat[i]==H) taxrate[i] = taxrate[i]-.05;

            //else if(stat[i]==M) taxrate[i] = taxrate[i]+0;

        taxamnt[i] = gross[i]*taxrate[i];

        net[i] = gross[i]-taxamnt[i];

cout<<fn[i]<<setw(10)<<ln[i]<<setw(5)<<stat[i]<<setw(7)<<ssn[i]<<setw(5)<<hw[i]<<setw(4)<<hr[i]<<setw(4)<<oth[i]<<setw(4)
<<otp[i]<<setw(5)<<regp[i]<<setw(6)<<gross[i]<<setw(6)<<taxamnt[i]<<setw(6)<<setprecision( 4 )<<net[i]<<endl<<endl;

        }//WHILE

        sum = findsum();

        average = findaverage();

        cout<<endl<<"The sum of the Employees' Netpay is: "<<sum<<endl<<endl;

        cout<<"The average Employee Netpay is: "<<average<<endl;

    cin.get();
    return 0;

    system("PAUSE");

    return EXIT_SUCCESS;

}//MAIN

float findsum(float net[], int n){
float sum=0.0;
    for(int i=0;i<n;i++){
    sum=sum+net[i];
    }//FOR
return sum;
}//FINDSUM

float findaverage(float sum, int n){
float average;
average=sum/n;
return average;
}//FINDAVERAGE 

And I am getting these errors, which I cannot seem to resolve:
1
2
3
4
5
6
C:\Users\Eric\...\main.cpp||In function `int main(int, char**)':|
C:\Users\Eric\...\main.cpp|7|error: too few arguments to function `float findsum(float, float, int)'|
C:\Users\Eric\...\main.cpp|83|error: at this point in file|
C:\Users\Eric\...\main.cpp|9|error: too few arguments to function `float findaverage(float, float, int)'|
C:\Users\Eric\...\main.cpp|85|error: at this point in file|
||=== Build finished: 4 errors, 0 warnings ===| 

Any suggestions on how to fix? Thanks in advance!
Last edited on
If you declare a function with arguments:
1
2
3
4
int function(int x, float y, float z)
{
    return x * y;
}


You must then mention all these arguments in future:
1
2
3
4
5
6
7
8
9
10
int func(int x, float y, float z)
{
    return x * y;
}

int main()
{
    func(6,3.95,5.4); //Fine
    func(5,6); //Will give me a 'too few arguments...' on this line. 
}


You will surely find you get an error for multiple declarations of 'sum' too, since you declare it in the function arguments and then again as a float within the function (line 103).
Last edited on
Thank you, Mats, for your continued efforts. Much appreciated! Here is what I have now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <fstream>
#include <iomanip>
#include <iostream>

using namespace std;

float findsum(float net, float sum, int);

float findaverage(float average, float sum, int);

int main(int argc, char *argv[]){

    char stat[100];

    char fn[100][14], ln[100][15];

    int ssn[100], hw[100], hr[100];

    float oth[100], otp[100], regp[100], gross[100], taxamnt[100];

    float taxrate[100];

    float net;

    float sum;     //Is the problem here? the error says 'declaration of 'float sum' shadows a parameter', but that is the code my Instructor suggested.

    float average;    //Or here?

    int i;

    int n;

    cout<<"__________________________________________________________________________"<<endl;
    cout<<"DR. EBRAHIMI'S PAYROLL INSTITUTE"<<endl;
    cout<<"=========================================================================="<<endl;

    ifstream fin("employeeM4.txt");

    cout<<setw(2)<<"FIRST"<<setw(8)<<"LAST"<<setw(8)<<"STAT"<<setw(5)<<"SSN"<<setw(5)<<"HW"<<setw(4)<<"HR"<<setw(4)<<"OTH"
<<setw(5)<<"OTP"<<setw(5)<<"REGP"<<setw(6)<<"GROSS"<<setw(6)<<"TAX"<<setw(6)<<"NET"<<endl;

    cout<<"=========================================================================="<<endl;

    while(fin>>fn[i]>>ln[i]>>stat[i]>>ssn[i]>>hw[i]>>hr[i]){

        if(hw[i]>40){

                     oth[i]=hw[i]-40;

                     otp[i]=oth[i]*hr[i]*1.5;

                     regp[i]=40*hr[i];

                     }

        else{

             oth[i]=0;

             otp[i]=0;

             regp[i]=hw[i]*hr[i];

             }

        gross[i] = regp[i]+otp[i];

            if(gross[i]>1000) taxrate[i] = .30;

            else if(gross[i]>800) taxrate[i] = .20;

            else if(gross[i]>500) taxrate[i] = .10;

            else taxrate[i] = 0;

            //if(stat[i]==S) taxrate[i] = taxrate[i]+.05;

            //else if(stat[i]==H) taxrate[i] = taxrate[i]-.05;

            //else if(stat[i]==M) taxrate[i] = taxrate[i]+0;

        taxamnt[i] = gross[i]*taxrate[i];

        net = gross[i]-taxamnt[i];

cout<<fn[i]<<setw(10)<<ln[i]<<setw(5)<<stat[i]<<setw(7)<<ssn[i]<<setw(5)<<hw[i]<<setw(4)<<hr[i]<<setw(4)<<oth[i]<<setw(4)
<<otp[i]<<setw(5)<<regp[i]<<setw(6)<<gross[i]<<setw(6)<<taxamnt[i]<<setw(6)<<setprecision( 4 )<<net<<endl<<endl;

i++;
        }//WHILE

        n=i;

        sum = findsum(net, sum, n);

        average = findaverage(average, sum, n);

        cout<<endl<<"The sum of the Employees' Netpay is: "<<sum<<endl<<endl;

        cout<<"The average Employee Netpay is: "<<average<<endl;

    cin.get();
    return 0;

    system("PAUSE");

    return EXIT_SUCCESS;

}//MAIN

float findsum(float net[], float sum[], int n){
    float sum=0.0;      //It says this is shadowing a parameter                             
    for(int i=0;i<n;i++){
    sum=sum+net[i];
    }//FOR
    return sum;
}//FINDSUM

float findaverage(float average[], float sum[], int n){
    float average;     //It says this is shadowing a parameter as well
    average=sum/n;     //|119|error: invalid operands of types `float*' and `int' to binary `operator/'| I am not sure what this means
    return average;
}//FINDAVERAGE 

This is giving me the following errors:
1
2
3
4
5
6
C:\Users\Eric\...\main.cpp||In function `float findsum(float*, float*, int)':|
C:\Users\Eric\...\main.cpp|110|error: declaration of 'float sum' shadows a parameter|
C:\Users\Eric\...\main.cpp||In function `float findaverage(float*, float*, int)':|
C:\Users\Eric\...\main.cpp|118|error: declaration of 'float average' shadows a parameter|
C:\Users\Eric\...\main.cpp|119|error: invalid operands of types `float*' and `int' to binary `operator/'|
||=== Build finished: 3 errors, 0 warnings ===| 
Last edited on
Ok, I actually got that program fixed and it is up and running. I am working on the 2nd part now, and I will reply when I (no doubt) run into any problems. Thanks to those that helped!
Topic archived. No new replies allowed.