423,867 Members | 3,449 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,867 IT Pros & Developers. It's quick & easy.

Help with modular functions and looping

P: 1
Okay, so I've gotten what I think is a good start on this project, not sure though. I am confused on how to make my program be able to run for 0 to an infinite number of employees. I know its a loop but not sure how to prompt int main to run again after computing for an employee. I'm assuming I need a kill command of like "5" to stop the program. Also, I'm confused on how to get information from my main to get the program to go to the correct next function based on the employee type. I appreciate any advice and help you can give me:).

Here are the project directions.

Create a program that calculates and displays (to 2 places of decimal) the weekly gross pay of hourly employees and salaried employees.
The weekly gross pay of a salaried employee is calculated by dividing the employee’s annual salary by 52.
The weekly gross pay of an Hourly employee is calculated by multiplying the number of hours worked by the pay rate.
Create two separate functions (one each for the Hourly employees and the Salaried employees) as follows:
 For the Hourly employee function, prompt for two values (hours worked and pay rate), and pass them to the function
 For the Salaried employee function, prompt for one value (yearly salary), and pass it to the function.
Continue to request, calculate and display employee information until there is no more employee data to process. Your program must be able to process zero employees, to an infinite number of employees.
Demonstrate the use of the following in your solution:
 Looping
 Modularity (using functions)
Prompt for the type of employee to process, and invoke the Hourly Employee function or the Salaried employee function, depending on the type of employee pay the program’s user wants to process.
Except for the number of weeks in a year (52), there must be no hard-coded input in your program.


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

#include "File.hpp"
#include <iostream>
using namespace std;


int Salaryemp(float yrsal);
int Hourlyemp(float hr, float wage);
float hr;
float wage;
float yrsal;
int emp;

int main()
{

cout << "enter employee type (1 for hourly and 0 for a salaried employee):";
cin >> emp;

if (emp == 1) // 1 = a hourly employee.
{
cout << "Enter hours worked this week:";
cin >> hr;
cout << "Enter horly pay:";
cin >> wage;


}
else (emp = 0); // 0 = a salaried employee.
{
cout << "Enter yearly salary:";
cin >> yrsal;

}


}





int Hourlyemp()
{
return 0;
}



int Salaryemp()
{

cout << yrsal / 52 << "is the weekly pay";


return 0;
}
Mar 10 '18 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,196
There are several issues here.

First, the main() is just the function called by the OS to start your program. Nothing special about it. In fact, you can tell your linker through a setting to call some other function than main(), if you want.

Second, your functions should do only one thing. The less a function does, the more places you can use it. Reuse of code is a big deal. It saves time recoding and debugging code that already exists.

So your program could look like this:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   int exit = 0;
  4.     while (1)  <--- your infinite loop
  5.         {
  6.          if (exit == 1)
  7.          break;  <-- blows you out of while(1) and back to main(). This is your program exit
  8.  
  9.           int choice;
  10.             Display Menu();    
  11.            choice = GetChoice();
  12.            switch(choice)  
  13.            {
  14.              1: AddEmployee();  <--don't have the user enter the employee type. Make it part of the data.
  15.                 break;  <--takes you back to while (1)
  16.  
  17.              2: exit = GetExit();     <-- sets exit to 1
  18.                 break;     <-- back to while(1)
  19.  
  20.  
  21.         }
  22. }

What do you think?
Mar 11 '18 #2

weaknessforcats
Expert Mod 5K+
P: 9,196
Next your employee data needs to be corralled so it stays together. If you don't know about C++ classes, start with a struct:

Expand|Select|Wrap|Line Numbers
  1. struct Employee
  2. {
  3.    float hr;
  4.    float wage;
  5.    float yrsal;
  6.    int emp;
  7. };
You would an instance of struct Employee and copy the instance to your storage array. You then clear the data in the instance and reuse it for the next Employee.
Mar 11 '18 #3

Post your reply

Sign in to post your reply or Sign up for a free account.