Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys, I have been working on my university assignment that was to write a program to get the input of no of employees working in a company from user and then get the input of their salaries from user. The objective was to calculate the net salaries (subtracting the tax amount, explain in the coding below) and then finding the unlucky people who had higher salary but lower net salary, below is the coding I have written. Now, the problem is when I run the program, it runs fine and ask the no of employees from user, after I give number, it runs fine again and ask their salaries, after I have given salaries then it does not do anything and just freezes and the dash keep blinking. Please help me out, what is the problem? This is the coding:

C++
#include <iostream>
using namespace std;

void getInput(double [][2], int);
void calcSal(double [][2], int);
void locUnLucky (double [][2], int [], int);
void displayOutput (double [][2], int [], int);

int main()
{
	const int arraySize= 100;
	int totalEmps;
	int lucky[] = {0};
	double sal[arraySize][2];
	
	cout << "Please enter the total number of employees working in your company: ";
	cin >> totalEmps;

	getInput(sal, totalEmps);
	
	calcSal(sal, totalEmps);
	
	locUnLucky(sal, lucky, totalEmps);
	
	displayOutput(sal, lucky, totalEmps);
	
}

void getInput(double sal[][2], int totalEmps)
{
    for (int i = 0; i < totalEmps; i++)
    {
        cout << "Please enter the gross salary in dollars for employee #" << i << ": ";
        cin >> sal[i][0];
    }
}

void calcSal(double sal[][2], int totalEmps)
{
	for (int i = 0; i < totalEmps; i++)
	{
		if (sal[i][0] <= 5000)
		{
			// no tax deduction
			sal[i][1] = sal[i][0];
		}
		
		else
		{
			if(sal[i][0] <= 10000)
			{
				// 5% tax deduction
				sal[i][1] = sal[i][0] - (0.05 * sal[i][0]);
			}
			
			else
			{
				if(sal[i][0] <= 20000)
				{
					// 10% tax deduction
					sal[i][1] = sal[i][0] - (0.1 * sal[i][0]);
				}
				
				else
				{
					// 15% tax deduction
					sal[i][1] = sal[i][0] - (0.15 * sal[i][0]);
				}
			}
		}
	}
}

void locUnLucky (double sal[][2], int lucky[], int totalEmps)
{
	int i, j, grossSalary, netSalary;
	
	for (i = 0; i < totalEmps; i++)
	{
		grossSalary = sal[i][0];
		netSalary = sal[i][1];
		
		for (j = 0; i < totalEmps; j++)
		{
			if(grossSalary > sal[i][0] && netSalary < sal[i][1])
			{
				lucky[i] = 1;
			}
		}
	}
}

void displayOutput (double sal[][2], int lucky[], int totalEmps)
{
	for (int i = 0; i < totalEmps; i++)
	{
		if (lucky[i] == 1)
		{
			cout << "Enmployee #" << i+1 << " is unlucky with $" << sal[i][0] << " as gross salary whereas $" << sal[i][1] << "as net salary" << endl;
		}
	}
}


What I have tried:

As I can't understand that what the problem could be, I have not tried anything
Posted
Updated 3-Dec-16 10:23am
Comments
RedDk 3-Dec-16 14:52pm    
The program looks very logical but just glancing at the basic nature would be a lot easier if there were some breakpoints set (pick a couple) and the whole thing was run through the debugger.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
CPallini 3-Dec-16 16:11pm    
Indeed. 5.
Patrice T 3-Dec-16 16:21pm    
Thank you
Khubaib Khawar 3-Dec-16 16:25pm    
Hey, thanks for the guideline. I am using DevC++ as IDE. Can you suggest what would be the good debugger for me? Or does DevC++ already have a built-in Debugger?
Patrice T 3-Dec-16 16:41pm    
I don't know DevC++, but it looks like there is a debugger integrated.
Just "Google DevC++ debugger"
Khubaib Khawar 3-Dec-16 17:02pm    
Thanks :)
Using the debugger, as suggested by ppolymorphe I was able to spot the bug, your are messing with loop indices (i, j) in the locUnLucky function. It should be:

C++
void locUnLucky (double sal[][2], int lucky[], int totalEmps)
{
	int i, j;
	double grossSalary, netSalary;
	
	for (i = 0; i < totalEmps; i++)
	{
		grossSalary = sal[i][0];
		netSalary = sal[i][1];
		
		for (j = 0; j < totalEmps; j++)
		{
			if(grossSalary < sal[j][0] && netSalary > sal[i][1])
			{
				lucky[i] = 1;
			}
		}
	}
}


However, even with such a fix, your code has a logical flaw: you should compare each employee salary with the average salary, in order to get meaningful info.
 
Share this answer
 
Comments
Khubaib Khawar 3-Dec-16 16:34pm    
By average, do you mean the average salaries of total no. of employees entered by the user?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900