Homework Help: Else/If With Math Logic

A thank you first off to anyone willing to read this and help. I am taking programming class online as part of my degree plan, and it has been a nightmare for me. I've been working with my professor on this, but he's actually written the textbook we're working out of and I'm not sure he can really explain things any differently. Anyway..

My code follows. The problem I'm facing is that the FINDTAXRATE doesn't appear to be calculating, which is throwing off everything else. The code itself was largely provided by the profesor, I've made some changes to get it to run (mostly) on Dev C++.

FWIW, I don't think I'm capable of coding for a living... just trying to get through this course :)

The result can be found here: https://docs.google.com/document/d/1KGxqrD3XV6c_7EhI92EA7QkdIbdXXInjlO7xCRVCwn8/edit?usp=sharing

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
125
126
127
128
129
130
131
132
133
134
///////Payroll Program Part 1 

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
#include <cstdlib>
#include <ctype.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>


using namespace std;

//prototypes
int readalldata (long int[], int[], float[], const int);
void findovertimehours (int[], int[], int);
void findovertimepay (int[], float[], float[], int);
void findregularhours (int[], int[], int);
void findregularpay (int[], float[], float[], int);
void findgrosspay (float[], float[], float[], int);
void findtaxrate (float[], float[], int);
void findtaxamount (float[], float[], float[], int);
void findnetpay (float[], float[], float[], int);
void printalldata (long int[], int[], float[], float[], float[], float[], float[], int);
///prototypes

int main(){
	const int MAXSIZE=100;
	long int id[MAXSIZE];
	int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
	int regularhours[MAXSIZE];
	float hourlyrate[MAXSIZE]; float regularpay[MAXSIZE];
	float overtimepay[MAXSIZE]; float grosspay[MAXSIZE];
	float taxrate[MAXSIZE]; float taxamount[MAXSIZE]; float netpay[MAXSIZE];
	
	int n = readalldata(id, hoursworked, hourlyrate, MAXSIZE);
	findovertimehours(hoursworked, overtimehours, n);
	findovertimepay(overtimehours, hourlyrate, overtimepay, n);
	findregularhours(hoursworked, regularhours, n);
	findregularpay(regularhours, regularpay, hourlyrate, n);
	findgrosspay(regularpay, overtimepay, grosspay, n);
	findtaxrate(grosspay, taxrate, n);
	findtaxamount(grosspay, netpay, taxamount, n);
	printalldata(id, hoursworked, hourlyrate, overtimepay, grosspay, taxamount, netpay, n);	
}//MAIN

///////Payroll Program Part 2

int readalldata(long int id[], int hoursworked[], float hourlyrate[], int n){
	fstream fin;
	fin.open("employee.txt");
	n=0;
	
	while(fin>> id[n] >> hoursworked[n]>> hourlyrate[n]) n++;
	fin.close();
	return n;
}

void findovertimehours ( int hoursworked[], int overtimehours[], int n){
	for( int i=0; i<n; i++){
		if(hoursworked[i]>40)overtimehours[i]=hoursworked[i] - 40;
		else overtimehours[i] = 0;
	}//FOR
}

void findovertimepay(int overtimehours[], float hourlyrate[],
	float overtimepay[], int n){
		for(int i=0; i<n; i++){
			overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;
	}//FOR
}

void findregularhours(int hoursworked[], int regularhours[], int n){
	for(int i=0; i<n; i++){
		if(hoursworked[i] > 40) regularhours[i]=40;
		else regularhours[i] = hoursworked[i];
	}//FOR
}	

void findregularpay(int regularhours[], float regularpay[],
					float hourlyrate[], int n){
	for(int i=0; i<n; i++){
		regularpay[i]=regularhours[i]*hourlyrate[i];
	}//FOR
}

void findgrosspay(float regularpay[], float overtimepay[],
					float grosspay[], int n){
	for(int i=0; i<n; i++){
		grosspay[i]=regularpay[i]+overtimepay[i];
	}//FOR
}

void findtaxrate(float grosspay[], float taxrate[], int n){
	for(int i=0; i<n; i++){
		if(grosspay[i] >=4000.00) 
		{
		taxrate[i] = 0.40;
		}
		else if(grosspay[i] >=3000.00 && grosspay[i] <=1000)
			{
			taxrate[i]=0.30;
			}
		else if(grosspay[i] >=1000.00)
			{
			taxrate[i]=0.20;
		}
		else {
		taxrate[i]=0.10;}
	}//FOR
}

void findtaxamount (float grosspay[], float taxamount[], float taxrate[], int n){
	for(int i=0; i<n; i++){
		taxamount[i]=grosspay[i]*taxrate[i];
	}//FOR
}

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n){
	for(int i=0; i<n; i++){
		netpay[i] = grosspay[i] - taxamount[i];
	}//FOR
}

///////Payroll Program Part 3

void printalldata(long int id[], int hoursworked[], float hourlyrate[], float overtimepay[], float grosspay[], float taxamount[], float netpay[], int n){
	cout<<std::setw(7)<<std::left<<"EMP ID"<<"\t"<<std::setw(5)<<std::left<<"HOURS"<<"\t"<<std::setw(5)<<std::left<<"RATE"<<"\t"<<std::setw(12)<<std::left<<"OVERPAY"<<"\t"<<std::setw(12)<<std::left<<"GROSSPAY"<<"\t"<<std::setw(12)<<std::left<<"TAX"<<"\t"<<std::setw(5)<<std::left<<"NETPAY"<<endl;
	for (int i=0; i<n; i++){
		cout<<""<<id[i]<<"\t"<<hoursworked[i]<<"\t"<<hourlyrate[i]<<"\t"<<overtimepay[i]<<"\t\t"<<grosspay[i]<<"\t\t"<<taxamount[i]<<"\t"<<netpay[i]<<endl;
	}//FOR
}


Last edited on
grosspay[i] >=3000.00 && grosspay[i] <=1000
When is a number at once greater than or equal to 3000 and less than or equal to 1000?

If after reading the above you're thinking of flipping the relational operators, be careful: you're still missing the 3000-4000 range.
Helios - Thank you so much for the reply, I wish I scrubbed that better before submitting the code here for review.

I made changes to the code based on your observation, but unfortunately the result is exactly the same. I commented in my thoughts for each range on the chance I'm missing something as basic as the greater than/less than.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void findtaxrate(float grosspay[], float taxrate[], int n){
	for(int i=0; i<n; i++){
		if(grosspay[i] >=4000) // Grosspay greater than or equal to 4000
		{
		taxrate[i] = 0.40;
		}
		else if(grosspay[i] =3000 && grosspay[i] <=3999) // Grosspay greater than or equal to 3000 but less than 4000
			{
			taxrate[i]=0.30;
			}
		else if(grosspay[i] >=1000 && grosspay[i] <=2999) // Grosspay greater than or equal to 1000 but less than 3000
			{
			taxrate[i]=0.20;
		}
		else {
		taxrate[i]=0.10;}   // 0 to 999
	}//FOR
}
Last edited on
Look carefully at what you're writing. Specifically line 7.
Same with the >=3000 in line 7 (was playing with spacing after getting the failed results and must have clipped it, I'm grasping at straws here :) )
Turns out i wasn't calling the netpay function in MAIN, and some other misc issues... corrected code here:

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
125
126
127
128
129
130
131
132
133
134
135
136
137
///////Payroll Program Part 1 

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
#include <cstdlib>
#include <ctype.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>


using namespace std;

//prototypes
int readalldata(long int[], int[], float[], const int);
void findovertimehours(int[], int[], int);
void findovertimepay(int[], float[], float[], int);
void findregularhours(int[], int[], int);
void findregularpay(int[], float[], float[], int);
void findgrosspay(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[], int);
void printalldata(long int[], int[], float[], float[], float[], float[], float[], int);
///prototypes

int main() {
	const int MAXSIZE = 100;
	long int id[MAXSIZE];
	int hoursworked[MAXSIZE], overtimehours[MAXSIZE];
	int regularhours[MAXSIZE];
	float hourlyrate[MAXSIZE]; float regularpay[MAXSIZE];
	float overtimepay[MAXSIZE]; float grosspay[MAXSIZE];
	float taxrate[MAXSIZE]; float taxamount[MAXSIZE]; float netpay[MAXSIZE];

	int n = readalldata(id, hoursworked, hourlyrate, MAXSIZE);
	findovertimehours(hoursworked, overtimehours, n);
	findovertimepay(overtimehours, hourlyrate, overtimepay, n);
	findregularhours(hoursworked, regularhours, n);
	findregularpay(regularhours, regularpay, hourlyrate, n);
	findgrosspay(regularpay, overtimepay, grosspay, n);
	findtaxrate(grosspay, taxrate, n);
	findtaxamount(grosspay, taxamount, taxrate, n);
	findnetpay(grosspay, netpay, taxamount, n);
	printalldata(id, hoursworked, hourlyrate, overtimepay, grosspay, taxamount, netpay, n);
}//MAIN

 ///////Payroll Program Part 2

int readalldata(long int id[], int hoursworked[], float hourlyrate[], int n) {
	fstream fin;
	fin.open("employee.txt");
	n = 0;

	while (fin >> id[n] >> hoursworked[n] >> hourlyrate[n]) n++;
	fin.close();
	return n;
}

void findovertimehours(int hoursworked[], int overtimehours[], int n) {
	for (int i = 0; i<n; i++) {
		if (hoursworked[i]>40)overtimehours[i] = hoursworked[i] - 40;
		else overtimehours[i] = 0;
	}//FOR
}

void findovertimepay(int overtimehours[], float hourlyrate[],
	float overtimepay[], int n) {
	for (int i = 0; i<n; i++) {
		overtimepay[i] = static_cast<float>(overtimehours[i] * hourlyrate[i] * 1.5);
	}//FOR
}

void findregularhours(int hoursworked[], int regularhours[], int n) {
	for (int i = 0; i<n; i++) {
		if (hoursworked[i] > 40) regularhours[i] = 40;
		else regularhours[i] = hoursworked[i];
	}//FOR
}

void findregularpay(int regularhours[], float regularpay[],
	float hourlyrate[], int n) {
	for (int i = 0; i<n; i++) {
		regularpay[i] = regularhours[i] * hourlyrate[i];
	}//FOR
}

void findgrosspay(float regularpay[], float overtimepay[],
	float grosspay[], int n) {
	for (int i = 0; i<n; i++) {
		grosspay[i] = regularpay[i] + overtimepay[i];
	}//FOR
}

void findtaxrate(float grosspay[], float taxrate[], int n) {
	for (int i = 0; i<n; i++) {
		if (grosspay[i] >= 4000) // Grosspay greater than or equal to 4000
		{
			taxrate[i] = 0.40f;
		}
		else if (grosspay[i] >= 3000 && grosspay[i] <= 3999) // Grosspay greater than or equal to 3000 but less than 4000
		{
			taxrate[i] = 0.30f;
		}
		else if (grosspay[i] >= 1000 && grosspay[i] <= 2999) // Grosspay greater than or equal to 1000 but less than 3000
		{
			taxrate[i] = 0.20f;
		}
		else {
			taxrate[i] = 0.10f;
		}   // 0 to 999
	}//FOR
}

void findtaxamount(float grosspay[], float taxamount[], float taxrate[], int n) {
	for (int i = 0; i<n; i++) {
		taxamount[i] = grosspay[i] * taxrate[i];
	}//FOR
}

void findnetpay(float grosspay[], float netpay[], float taxamount[], int n) {
	for (int i = 0; i<n; i++) {
		netpay[i] = grosspay[i] - taxamount[i];
		cout << "netpay: " << netpay[i] << endl;
	}//FOR
}

///////Payroll Program Part 3

void printalldata(long int id[], int hoursworked[], float hourlyrate[], float overtimepay[], float grosspay[], float taxamount[], float netpay[], int n) {
	cout << std::setw(7) << std::left << "EMP ID" << "\t" << std::setw(5) << std::left << "HOURS" << "\t" << std::setw(5) << std::left << "RATE" << "\t" << std::setw(12) << std::left << "OVERPAY" << "\t" << std::setw(12) << std::left << "GROSSPAY" << "\t" << std::setw(12) << std::left << "TAX" << "\t" << std::setw(5) << std::left << "NETPAY" << endl;
	for (int i = 0; i<n; i++) {
		cout << "" << id[i] << "\t" << hoursworked[i] << "\t" << hourlyrate[i] << "\t" << overtimepay[i] << "\t\t" << grosspay[i] << "\t\t" << taxamount[i] << "\t\t" << netpay[i] << endl;
	}//FOR
}


Helios - Thank you again for jumping in and helping me fix my calculations!!
Topic archived. No new replies allowed.