11 Replies - 39087 Views - Last Post: 15 August 2019 - 05:53 AM Rate Topic: -----

#1 qwert12345   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 26-October 08

Write a program to compute the weekly pay.

Post icon  Posted 26 October 2008 - 04:12 PM

Hi, I'm very new to c++, and I have to write a program that will compute the weekly pay for each
employee.

A company pays its employees as managers (who receive a fixed weekly salary), hourly
workers (who receive a fixed hourly wage for up to the first 40 hours they work and
time-and-a-half, i.e. 1.5 times their hourly wage, for overtime hours worked), commission
workers (who receive $250 plus 5% of their gross weekly sales), or pieceworkers (who
receive a fixed amount of money for each item they produce-each pieceworker in the
company works only on one type of item).

Write a program to compute the weekly pay for each
employee. You do not know the number of employees in advance. Each type of employee has its
own pay code: Managers have code 1, hourly workers have code 2, commission workers 3 and
pieceworkers 4. Use a switch to compute each employee's pay according to his/her paycode.
Within the switch, prompt the user to enter the appropriate facts your program needs
to calculate each employee's pay according to the paycode.

Problem: when i run the program, and enter one of the values for the paycode it does nothing.


here's my code:



#include <iostream>
#include <iomanip>																							 

using namespace std;

int main ()
{
 int paycode;
 int WeeklySalary;
 double pay;
 int HourlySalary;
 int TotalHours;
 int GrossWeeklySales;
 int pieces;
 int PieceWage;

cout << "Enter paycode (-1 to end): ";
cin  >> paycode;






 while (1); {
		
		 switch (paycode) {
				case '1':
				 cout << "Manager selected." << endl;
				 cout << "Enter weekly salary: ";
				 cin  >> WeeklySalary;
				 
				 cout << endl;
				 
				 pay = WeeklySalary;
				 cout << "The manager's pay is $ " << pay;
				 cout << endl;
				 
				 
				 break;
				
				 case '2':
				 cout << "Hourly worker selected." << endl;
				 cout << "Enter the hourly salary: ";
				 cin  >> HourlySalary;
				 
				 cout << endl;
				 cout << "Enter the total hours worked: " << endl;
				 cin  >> TotalHours;
				 
					  if ( TotalHours <= 40) 
						   pay = HourlySalary * TotalHours;
						   
					  else 
						   pay = (40.0 * HourlySalary) + (TotalHours - 40) * (HourlySalary * 1.5);
						   
				  
				 cout << endl;
				 cout << "Worker's pay is $ " << pay; 
				 
				 cout << endl;
				 break;
				 
				 case '3':
				 cout << "Commission worker selected." << endl;
				 cout << "Enter gross weekly sales: ";
				 cin  >> GrossWeeklySales;
				 
				 cout << endl;
				 pay = (GrossWeeklySales *.57) + 250;
				  
				 cout << " Commission worker's pay is $ " << pay;
				  break;   
				 
				 case '4':
				 cout << "Pieceworker selected." << endl;
				 cout << "Enter number of pieces: ";
				 cin  >> pieces;
				 
				 cout << "Enter wage per piece: ";
				 cin >> PieceWage;
				 
				 pay = pieces * PieceWage;
				 
				 cout << "Pieceworker's pay is $ " << pay;
				 break;
				 

				 
			   
} 
				
}



system ("pause");

return 0;

}
		  


Is This A Good Question/Topic? 0
  • +

Replies To: Write a program to compute the weekly pay.

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6257
  • View blog
  • Posts: 24,025
  • Joined: 23-August 08

Re: Write a program to compute the weekly pay.

Posted 26 October 2008 - 05:03 PM

while (1); 


That's an infinite loop...get rid of the semicolon.
Was This Post Helpful? 0
  • +
  • -

#3 qwert12345   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 26-October 08

Re: Write a program to compute the weekly pay.

Posted 26 October 2008 - 05:13 PM

View PostJackOfAllTrades, on 26 Oct, 2008 - 05:03 PM, said:

while (1); 


That's an infinite loop...get rid of the semicolon.



ok, i deleted the semi-colon by the while condition, do i need to change the while condition from
while(1)



to
while (paycode = -1)




when the program runs, and i input one of the paycode, it does nothing...still.
Was This Post Helpful? 0
  • +
  • -

#4 brianlb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 05-October 08

Re: Write a program to compute the weekly pay.

Posted 26 October 2008 - 05:26 PM

I didn't run this, but by looking at it, it appears it would work. If it doesn't it should give you an idea of where to go from here.

#include <iostream>
#include <iomanip>																							

using namespace std;

int main ()
{
	int paycode,
		TotalHours,
		pieces;
	double pay,
		   HourlySalary,
		   PieceWage,
		   WeeklySalary,
		   GrossWeeklySales;

	cout << "Enter paycode\n"
		 << "Enter 1 for Manager, 2 for Hourly Worker \n"
		 << "3 for Commission Worker, and 4 for Piece Work. (0 to end): ";
	cin  >> paycode;


	while (paycode < 0 || paycode >=5)
	{
		cout << "You must enter 0 through 4\n";
		cout << "Enter 1 for Manager, 2 for Hourly Worker \n"
			 << "3 for Commission Worker, and 4 for Piece Work. (0 to end): ";
		cin  >> paycode;
	}

	
	
	cout << fixed << showpoint << setprecision(2);
		
		 switch (paycode) 
		 {
				 case 1:
				 cout << "Manager selected." << endl;
				 cout << "Enter weekly salary: ";
				 cin  >> WeeklySalary;
				 cout << endl;

				 pay = WeeklySalary;
				 cout << "The manager's pay is $ " << pay;
				 cout << endl;
				 break;	
								 
				 case 2:
				 cout << "Hourly worker selected." << endl;
				 cout << "Enter the hourly salary: ";
				 cin  >> HourlySalary;
				
				 cout << endl;
				 cout << "Enter the total hours worked: " << endl;
				 cin  >> TotalHours;
				
					  if ( TotalHours <= 40)
						   pay = HourlySalary * TotalHours;
						  
					  else
						   pay = (40.0 * HourlySalary) + (TotalHours - 40) * (HourlySalary * 1.5);
						  
				  
				 cout << endl;
				 cout << "Worker's pay is $ " << pay;
				
				 cout << endl;
				 break;
				
				 case 3:
				 cout << "Commission worker selected." << endl;
				 cout << "Enter gross weekly sales: ";
				 cin  >> GrossWeeklySales;
				
				 cout << endl;
				 pay = (GrossWeeklySales *.57) + 250;
				  
				 cout << "Commission worker's pay is $ " << pay << endl;
				 break;  
				
				 case 4:
				 cout << "Pieceworker selected." << endl;
				 cout << "Enter number of pieces: ";
				 cin  >> pieces;
				
				 cout << "Enter wage per piece: ";
				 cin >> PieceWage;
				
				 pay = pieces * PieceWage;
				
				 cout << "Pieceworker's pay is $ " << pay << endl;
				 break;
				
				 					
		  }	 

return 0;

}


Was This Post Helpful? 0
  • +
  • -

#5 qwert12345   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 26-October 08

Re: Write a program to compute the weekly pay.

Posted 26 October 2008 - 05:50 PM

View Postbrianlb, on 26 Oct, 2008 - 05:26 PM, said:

I didn't run this, but by looking at it, it appears it would work. If it doesn't it should give you an idea of where to go from here.

#include <iostream>
#include <iomanip>																							

using namespace std;

int main ()
{
	int paycode,
		TotalHours,
		pieces;
	double pay,
		   HourlySalary,
		   PieceWage,
		   WeeklySalary,
		   GrossWeeklySales;

	cout << "Enter paycode\n"
		 << "Enter 1 for Manager, 2 for Hourly Worker \n"
		 << "3 for Commission Worker, and 4 for Piece Work. (0 to end): ";
	cin  >> paycode;


	while (paycode < 0 || paycode >=5)
	{
		cout << "You must enter 0 through 4\n";
		cout << "Enter 1 for Manager, 2 for Hourly Worker \n"
			 << "3 for Commission Worker, and 4 for Piece Work. (0 to end): ";
		cin  >> paycode;
	}

	
	
	cout << fixed << showpoint << setprecision(2);
		
		 switch (paycode) 
		 {
				 case 1:
				 cout << "Manager selected." << endl;
				 cout << "Enter weekly salary: ";
				 cin  >> WeeklySalary;
				 cout << endl;

				 pay = WeeklySalary;
				 cout << "The manager's pay is $ " << pay;
				 cout << endl;
				 break;	
								 
				 case 2:
				 cout << "Hourly worker selected." << endl;
				 cout << "Enter the hourly salary: ";
				 cin  >> HourlySalary;
				
				 cout << endl;
				 cout << "Enter the total hours worked: " << endl;
				 cin  >> TotalHours;
				
					  if ( TotalHours <= 40)
						   pay = HourlySalary * TotalHours;
						  
					  else
						   pay = (40.0 * HourlySalary) + (TotalHours - 40) * (HourlySalary * 1.5);
						  
				  
				 cout << endl;
				 cout << "Worker's pay is $ " << pay;
				
				 cout << endl;
				 break;
				
				 case 3:
				 cout << "Commission worker selected." << endl;
				 cout << "Enter gross weekly sales: ";
				 cin  >> GrossWeeklySales;
				
				 cout << endl;
				 pay = (GrossWeeklySales *.57) + 250;
				  
				 cout << "Commission worker's pay is $ " << pay << endl;
				 break;  
				
				 case 4:
				 cout << "Pieceworker selected." << endl;
				 cout << "Enter number of pieces: ";
				 cin  >> pieces;
				
				 cout << "Enter wage per piece: ";
				 cin >> PieceWage;
				
				 pay = pieces * PieceWage;
				
				 cout << "Pieceworker's pay is $ " << pay << endl;
				 break;
				
				 					
		  }	 

return 0;

}






Thanks, I had to add a system("pause") at the end of your code, because the program was cut short when i ran it. Other than that, its fine.

so, in my program, i put switch(paycode) in the while loop, and I think that was the problem.
Was This Post Helpful? 0
  • +
  • -

#6 brianlb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 05-October 08

Re: Write a program to compute the weekly pay.

Posted 26 October 2008 - 05:58 PM

you had a few problems, one you didn't declare any arguments for your while loop. On your switch , you delcared paycode as an int, but when you put case '1': you basically were thinking in terms of a char, you don't need the single quotes when declaring an int, you would have had to declared char paycode;
It didn't look like you had the understanding of how the while loop works, basically all i did was use the while loop to make sure the user inputs either 0,1,2,3, or 4, anything other than that they stay in the loop until they enter what you want them to enter.
Was This Post Helpful? 0
  • +
  • -

#7 codemax88   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 07-February 07

Re: Write a program to compute the weekly pay.

Posted 26 October 2008 - 07:21 PM

View Postqwert12345, on 26 Oct, 2008 - 05:50 PM, said:

View Postbrianlb, on 26 Oct, 2008 - 05:26 PM, said:

I didn't run this, but by looking at it, it appears it would work. If it doesn't it should give you an idea of where to go from here.

#include <iostream>
#include <iomanip>																							

using namespace std;

int main ()
{
	int paycode,
		TotalHours,
		pieces;
	double pay,
		   HourlySalary,
		   PieceWage,
		   WeeklySalary,
		   GrossWeeklySales;

	cout << "Enter paycode\n"
		 << "Enter 1 for Manager, 2 for Hourly Worker \n"
		 << "3 for Commission Worker, and 4 for Piece Work. (0 to end): ";
	cin  >> paycode;


	while (paycode < 0 || paycode >=5)
	{
		cout << "You must enter 0 through 4\n";
		cout << "Enter 1 for Manager, 2 for Hourly Worker \n"
			 << "3 for Commission Worker, and 4 for Piece Work. (0 to end): ";
		cin  >> paycode;
	}

	
	
	cout << fixed << showpoint << setprecision(2);
		
		 switch (paycode) 
		 {
				 case 1:
				 cout << "Manager selected." << endl;
				 cout << "Enter weekly salary: ";
				 cin  >> WeeklySalary;
				 cout << endl;

				 pay = WeeklySalary;
				 cout << "The manager's pay is $ " << pay;
				 cout << endl;
				 break;	
								 
				 case 2:
				 cout << "Hourly worker selected." << endl;
				 cout << "Enter the hourly salary: ";
				 cin  >> HourlySalary;
				
				 cout << endl;
				 cout << "Enter the total hours worked: " << endl;
				 cin  >> TotalHours;
				
					  if ( TotalHours <= 40)
						   pay = HourlySalary * TotalHours;
						  
					  else
						   pay = (40.0 * HourlySalary) + (TotalHours - 40) * (HourlySalary * 1.5);
						  
				  
				 cout << endl;
				 cout << "Worker's pay is $ " << pay;
				
				 cout << endl;
				 break;
				
				 case 3:
				 cout << "Commission worker selected." << endl;
				 cout << "Enter gross weekly sales: ";
				 cin  >> GrossWeeklySales;
				
				 cout << endl;
				 pay = (GrossWeeklySales *.57) + 250;
				  
				 cout << "Commission worker's pay is $ " << pay << endl;
				 break;  
				
				 case 4:
				 cout << "Pieceworker selected." << endl;
				 cout << "Enter number of pieces: ";
				 cin  >> pieces;
				
				 cout << "Enter wage per piece: ";
				 cin >> PieceWage;
				
				 pay = pieces * PieceWage;
				
				 cout << "Pieceworker's pay is $ " << pay << endl;
				 break;
				
				 					
		  }	 

return 0;

}






Thanks, I had to add a system("pause") at the end of your code, because the program was cut short when i ran it. Other than that, its fine.

so, in my program, i put switch(paycode) in the while loop, and I think that was the problem.

Your while loop conditions are incorrect. Negative numbers and numbers greater than 5 would make your program still run.
Also your while loop does not surround the switch block correctly. If you want to keep inputing options until terminating with zero you need to fix it . Also, your input prompts are redundant.

View Postcodemax88, on 26 Oct, 2008 - 07:16 PM, said:

View Postqwert12345, on 26 Oct, 2008 - 05:50 PM, said:

View Postbrianlb, on 26 Oct, 2008 - 05:26 PM, said:

I didn't run this, but by looking at it, it appears it would work. If it doesn't it should give you an idea of where to go from here.

#include <iostream>
#include <iomanip>																							

using namespace std;

int main ()
{
	int paycode,
		TotalHours,
		pieces;
	double pay,
		   HourlySalary,
		   PieceWage,
		   WeeklySalary,
		   GrossWeeklySales;

	cout << "Enter paycode\n"
		 << "Enter 1 for Manager, 2 for Hourly Worker \n"
		 << "3 for Commission Worker, and 4 for Piece Work. (0 to end): ";
	cin  >> paycode;


	while (paycode < 0 || paycode >=5)
	{
		cout << "You must enter 0 through 4\n";
		cout << "Enter 1 for Manager, 2 for Hourly Worker \n"
			 << "3 for Commission Worker, and 4 for Piece Work. (0 to end): ";
		cin  >> paycode;
	}

	
	
	cout << fixed << showpoint << setprecision(2);
		
		 switch (paycode) 
		 {
				 case 1:
				 cout << "Manager selected." << endl;
				 cout << "Enter weekly salary: ";
				 cin  >> WeeklySalary;
				 cout << endl;

				 pay = WeeklySalary;
				 cout << "The manager's pay is $ " << pay;
				 cout << endl;
				 break;	
								 
				 case 2:
				 cout << "Hourly worker selected." << endl;
				 cout << "Enter the hourly salary: ";
				 cin  >> HourlySalary;
				
				 cout << endl;
				 cout << "Enter the total hours worked: " << endl;
				 cin  >> TotalHours;
				
					  if ( TotalHours <= 40)
						   pay = HourlySalary * TotalHours;
						  
					  else
						   pay = (40.0 * HourlySalary) + (TotalHours - 40) * (HourlySalary * 1.5);
						  
				  
				 cout << endl;
				 cout << "Worker's pay is $ " << pay;
				
				 cout << endl;
				 break;
				
				 case 3:
				 cout << "Commission worker selected." << endl;
				 cout << "Enter gross weekly sales: ";
				 cin  >> GrossWeeklySales;
				
				 cout << endl;
				 pay = (GrossWeeklySales *.57) + 250;
				  
				 cout << "Commission worker's pay is $ " << pay << endl;
				 break;  
				
				 case 4:
				 cout << "Pieceworker selected." << endl;
				 cout << "Enter number of pieces: ";
				 cin  >> pieces;
				
				 cout << "Enter wage per piece: ";
				 cin >> PieceWage;
				
				 pay = pieces * PieceWage;
				
				 cout << "Pieceworker's pay is $ " << pay << endl;
				 break;
				
				 					
		  }	 

return 0;

}






Thanks, I had to add a system("pause") at the end of your code, because the program was cut short when i ran it. Other than that, its fine.

so, in my program, i put switch(paycode) in the while loop, and I think that was the problem.

Your while loop conditions are incorrect. Negative numbers and numbers greater than 5 would make your program still run.
Also your while loop does not surround the switch block correctly. If you want to keep inputing options until terminating with zero you need to fix it . Also, your input prompts are redundant.

Let me correct myself. Your while loop does not surround your switch block at all.
Was This Post Helpful? 0
  • +
  • -

#8 brianlb   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 05-October 08

Re: Write a program to compute the weekly pay.

Posted 26 October 2008 - 08:37 PM

Runs fine on VS 2008 and runs fine for him, the while loop conditions are correct, enter a negative number or a number 5 or more and it asks you to re enter, the switch never gets executed if anything other than 1,2,3, or 4 is entered. For this program surrounding it in the switch block doesn't matter.
Was This Post Helpful? 0
  • +
  • -

#9 codemax88   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 07-February 07

Re: Write a program to compute the weekly pay.

Posted 26 October 2008 - 10:55 PM

View Postbrianlb, on 26 Oct, 2008 - 08:37 PM, said:

Runs fine on VS 2008 and runs fine for him, the while loop conditions are correct, enter a negative number or a number 5 or more and it asks you to re enter, the switch never gets executed if anything other than 1,2,3, or 4 is entered. For this program surrounding it in the switch block doesn't matter.

My final thoughts: I see qwert12345 is using the while loop as a prompt exception ( informing users they made a wrong entry). An "if" statement would do the same thing since in reality the while loop is not really doing anything. The only logic I see that is strange is why would a user enter a zero to terminate while loop before it begins? Since the while statement does not enclose the switch statment, the zero is not needed since a single selection is made then the program terminates. In essence, multiple choices are not made during excution of the program. Every switch choice entails a new execution of the program. The only reasonable purpose is to terminate the initial prompt exception. I gather that is what qwet12345 wants.
Was This Post Helpful? 0
  • +
  • -

#10 qwert12345   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 26-October 08

Re: Write a program to compute the weekly pay.

Posted 27 October 2008 - 05:52 PM

View Postcodemax88, on 26 Oct, 2008 - 10:55 PM, said:

View Postbrianlb, on 26 Oct, 2008 - 08:37 PM, said:

Runs fine on VS 2008 and runs fine for him, the while loop conditions are correct, enter a negative number or a number 5 or more and it asks you to re enter, the switch never gets executed if anything other than 1,2,3, or 4 is entered. For this program surrounding it in the switch block doesn't matter.

My final thoughts: I see qwert12345 is using the while loop as a prompt exception ( informing users they made a wrong entry). An "if" statement would do the same thing since in reality the while loop is not really doing anything. The only logic I see that is strange is why would a user enter a zero to terminate while loop before it begins? Since the while statement does not enclose the switch statment, the zero is not needed since a single selection is made then the program terminates. In essence, multiple choices are not made during excution of the program. Every switch choice entails a new execution of the program. The only reasonable purpose is to terminate the initial prompt exception. I gather that is what qwet12345 wants.



I have to use a sentinel loop for the program, so codemax88 is right.
anytime I change the while condition to while(paycode != -1) it does nothing, the rest of my program seems alright.
Was This Post Helpful? 0
  • +
  • -

#11 Crackerberg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-August 19

Re: Write a program to compute the weekly pay.

Posted 15 August 2019 - 04:57 AM

Hey guys could you also post the pseudo code and flowcharts... If it's not too much to ask...
Was This Post Helpful? 0
  • +
  • -

#12 Cup of Java   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 26-September 13

Re: Write a program to compute the weekly pay.

Posted 15 August 2019 - 05:53 AM

@Crackerberg: This thread is almost 11 years old. If you have a similar question to ask, please start another thread to receive help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1