User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,791 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,752 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1616 | Replies: 3
Reply
Join Date: Oct 2004
Posts: 18
Reputation: hill0ster is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hill0ster hill0ster is offline Offline
Newbie Poster

Help Need help displaying when user enters negative number?

  #1  
Nov 7th, 2004
I am having two problems with this code. #1) The user is to enter a three digit number for their employee ID. No problem there. But if the user enters a negative number, such as -1 the program is to end and display the total payroll for the month, total payroll is all the employees pay added together. This function also has to do error checking so the user cannot enter '1xv'. I got that part but need help with displaying when user enters a negative number.
#2) I can't seem to figure out Total Payroll to display properly, I get junk values.


#include <iostream> 
#include <iomanip>
using namespace std;
#include <string>
int getEmployeeData (string[], char[], const int, double[]);
float calculateGrossPay(string[], char[], double[], int, double[]);
void displayEmployeeInfo (string[], char[], int, double[], double[]);
bool tryAgain();
bool isValidStatus(char);
bool isValidID (string);


int main()
{
	const int numEmp = 4;
	string ID[numEmp];
	char pyrllSt[numEmp];
	double grossPay[numEmp];
	double netPay[numEmp];
	double totalPayroll = 0;
	getEmployeeData(ID, pyrllSt, numEmp, grossPay);
	calculateGrossPay(ID, pyrllSt, grossPay, numEmp, netPay);

	return 0;	
}


int getEmployeeData (string ID[], char pyrllSt[], const int numEmp, double grossPay[])
{
		for (int index = 0; index < numEmp; index++)
		{	
			do
			{
				cout << "Enter employee ID:" << endl;
				cin >> ID[index];
			} while(!isValidID(ID[index])); 
			
			do
			{
				cout << "Enter payroll status:" << endl;
				cin >> pyrllSt[index];
			}while (!isValidStatus(pyrllSt[index])); 
	
			if (pyrllSt[index] == 's' || pyrllSt[index] == 'S')
			{
				cout << "Enter the monthly salary:" << endl;
			}
			
			else if (pyrllSt[index] == 'h' || pyrllSt[index] == 'H')
			{
				cout << "Enter the number of hours worked this month:" << endl;
			}
			
			else if (pyrllSt[index] == 'c' || pyrllSt[index] == 'C')
			{
				cout << "Enter total sales for this month: " << endl;
			}
			cin >> grossPay[index];
		}
		return grossPay[index];
}

float calculateGrossPay (string ID[], char pyrllSt[], double grossPay[] , const int numEmp, double netPay[])
{
	for (int index = 0; index < numEmp; index++)
	{
		if (pyrllSt[index] == 's' || pyrllSt[index] == 'S')
		{
			netPay[index] = grossPay[index];
		}
		if (pyrllSt[index] == 'h' || pyrllSt[index] == 'H')
		{
			netPay[index] = grossPay[index] * 18.75;
		}
		if (pyrllSt[index] == 'c' || pyrllSt[index] == 'C')
		{
			netPay[index] = (grossPay[index] * 0.06) + 1000.00;
		}
	}
	displayEmployeeInfo (ID, pyrllSt, numEmp, netPay, grossPay);
	return netPay[index];
}

void displayEmployeeInfo (string ID[], char pyrllSt[], const int numEmp, double netPay[],double grossPay[])
{
	cout << "Employee ID    " << "Payroll Status" << "    GrossPay" << endl;
	cout << fixed << showpoint << setprecision(2);
	for (int index = 0; index < numEmp; index++)
	{
		if (pyrllSt[index] == 's' ||pyrllSt[index] == 'S')
		{
			cout << ID[index] << setw(20) << "Salaried" << setw(11) << "$" << netPay[index] << endl;
		}
		if (pyrllSt[index] == 'c' || pyrllSt[index] == 'C')
		{
			cout << ID[index] << setw(24) << "Commissioned" << setw(7) << "$" << netPay[index] << endl;
		}
		if (pyrllSt[index] == 'h' || pyrllSt[index] == 'H')
		{
			cout << ID[index] << setw(18) << "Hourly" << setw(13) << "$" << netPay[index] << endl;
		}
		
	}
	
	//float totalPayroll += grossPay[index];
	//cout << "Total Payroll for this month: " << "$" << totalPayroll << endl;
	
}

bool tryAgain()
{
	char quit;
	
	cout << "Do you want to try again or quit?" << endl;
	cout << "Type Q or q to quit, any other key to continue: " << endl;
	cin.get(quit);
	
	if (quit != '\n')
	{
		cin.ignore();
	}
	
	cin.get(quit);
	
	if (quit == 'q' || quit == 'Q')
	{
		cout << "*** Program Terminated ***" << endl;
		exit (0);
		return false;
		
	}
	
	else
	{
		return true;
	}
}
							
bool isValidStatus(char pyrllSt)
{
	if (pyrllSt == 's' || pyrllSt == 'S' || pyrllSt == 'h' || pyrllSt == 'H' || pyrllSt == 'c' || pyrllSt == 'C')
	{
		return true;
	}
	else 
	{
		cout << "*** Invalid payroll status ***" << endl;
		tryAgain();
		cout << "Re-enter 's' or 'S' for salaried, 'h' or 'H' for hourly," << endl;
		cout << "'c' or 'C' for commissioned." << endl;
		return false;
	}
}

bool isValidID(string ID)
{
	for (int index = 0; index < ID.length(); index++)
	{	
		if(ID[0] < 48 || ID[0] > 57)
		{
			cout << "*** Invalid employee ID ***" << endl;
			return tryAgain();
		}
	}

	if(ID.length() != 3)
	{
		cout << "*** Invalid employee ID ***" << endl;
		return false;
	}
	else
	{
		return true;
	}
} 
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,325
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Need help displaying when user enters negative number?

  #2  
Nov 7th, 2004
Stop making new threads for the same stupid question! I have a sound I'd like you to hear:

*plonk*
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Oct 2004
Posts: 44
Reputation: jigvesh is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jigvesh jigvesh is offline Offline
Light Poster

Re: Need help displaying when user enters negative number?

  #3  
Nov 7th, 2004
Hey dont get depressed by that comment. The solution is you can use exceptions. For example, you can throw an exception when the id<0 and on catching the exception display the details you need. Hope i am clear. If not tell me i will try to send you the code.
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 102
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Need help displaying when user enters negative number?

  #4  
Nov 7th, 2004
Seriously, it's called double posting, and it's not encouraged in this forum at all.

Closing this thread; please continue conversation in the thread already created.
Alex Cavnar, aka alc6379
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC