Adding Basic Procedures?

So I am currently in the middle of learning c++ and I decided to jump ahead of the book and tried to do add procedures. My goal is to have main calling other procedures such as welcome message(). I know my code is a mess. I am pretty much new to this (sorta). Please tell me what I am doing wrong! Any help would be welcome!

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  #include <iostream>
#include <iomanip>
#include <string>

using namespace std;

#define MIN_HOURS            0.00  // minimum hours per week
#define MAX_HOURS           60.00  // maximum hours per week
#define MIN_RATE             0.00  // minimum pay rate
#define MAX_RATE            99.99  // maximum pay rate
#define REGULAR_HOURS_LIMIT 40.00  // hours at which overtime begins
#define OVERTIME_RATE        1.50  // overtime rate
#define TAX_RATE             0.30  // tax rate (30%)
#define PARKING_RATE        10.00  // parking deduction

void show_welcome_message();
void input_employee_data(string &first_name, string &last_name, string &hours, string &hourly_rate);
void join_name(string &first_name,string &last_name,string full_name);
void split_hours(double &hours,double regular_hours,double overtime_hours);
void calculate_gross_pay(double &regular_hours,double &overtime_hours,double &hourly_rate,double gross_pay);
void calculate_tax(double &gross_pay, double &tax_rate, double tax);
void calculate_net_pay(double &gross_pay,double &tax,double &deduction,double net_pay);
void output_payroll_data(string &full_name,double &regular_hours,double &overtime_hours,double &hourly_rate,double &gross_pay,double &tax,double &deductions,double &net_pay);
void get_yesno(string &prompt,char yesno);

int main()
{
    string first_name,   // employee's first name, input by user
        last_name,       // employee's last name, input by user
        full_name;       // employee's concatenated full name

    double hours,        // number of hours worked, input by user
        regular_hours,   // number of regular hours
        overtime_hours,  // number of overtime hours
        hourly_rate,     // hourly pay rate
        gross_pay,       // employee's gross pay
        tax,             // employee's tax amount
        deductions,      // monthly deductions
        net_pay;         // employee's net pay
    char   yesno;        // user's yes or no response
  
show_welcome_message();
input_employee_data(first_name,last_name,hours,hourly_rate);
join_name(first_name,last_name,full_name);
split_hours(hours,regular_hours,overtime_hours);
calculate_gross_pay(regular_hours,overtime_hours,hourly_rate,gross_pay);
calculate_tax(gross_pay,tax_rate,tax);
calculate_net_pay(gross_pay,tax,deduction,net_pay);
output_payroll_data(full_name,regular_hours,overtime_hours,hourly_rate,gross_pay,tax,deductions,net_pay);
get_yesno(prompt,yesno);
}


//
// Displays a welcome message to the user, with the name of the program
// and a short discription.
//
void show_welcome_message()
{
    cout << "Payroll Program" << endl << endl;
    cout << "This program computes and displays the gross pay, taxes,"
         <<  endl;
    cout << "and net pay for employees of the Super Supermarket, given"
         <<  endl;
    cout << "each employee's name, number of hours worked, and hourly"
         << endl;
    cout << "pay rate."
         << endl;
}

//
// Prompts user to enter employee first name and last name
//
void input_employee_data(string &first_name, string &last_name, string &hours, string &hourly_rate)
{
    do {

        cout << "Enter employee's first name: ";
        cin >> first_name;
        cout << "Enter employee's last name: ";
        cin >> last_name;


        do {
            cout << "Enter number of hours worked: ";
            cin >> hours;
            if (hours < MIN_HOURS || hours > MAX_HOURS)
                cout << fixed << setprecision(2)
                << "The number of hours worked must be between "
                << MIN_HOURS << " and " << MAX_HOURS << endl
                << endl;
        } while (hours < MIN_HOURS || hours > MAX_HOURS);

        do {
            cout << "Enter hourly pay rate: ";
            cin >> hourly_rate;
            if (hourly_rate < MIN_RATE || hourly_rate > MAX_RATE)
                cout << fixed << setprecision(2)
                << "The hourly pay rate must be between "
                << MIN_RATE << " and " << MAX_RATE << endl
                << endl;
        } while
			(hourly_rate < MIN_RATE || hourly_rate > MAX_RATE);
    }
}

//
//  Began processing names
//
void join_name(string &first_name, string &lastname, string fullname)
{
    full_name = last_name + ", " + first_name;
}

//
// Began processing split hours
//
void split_hours(double &hours,double regular_hours,double overtime_hours)
{
        if (hours <= REGULAR_HOURS_LIMIT) {
            regular_hours = hours;
            overtime_hours = 0.0;
        }
        else {
            regular_hours = REGULAR_HOURS_LIMIT;
            overtime_hours = hours - REGULAR_HOURS_LIMIT;
        }
        }

//
// Began calculating gross pay
//
void calculate_gross_pay(double &regular_hours, double &overtime_hours, double &hourly_rate, double gross_pay)
{
    gross_pay = (regular_hours * hourly_rate) + (overtime_hours *
        hourly_rate * OVERTIME_RATE);
}

//
// Began calculating tax
//
void calculate_tax(double &gross_pay,double &tax_rate,double tax)
        {
			tax = gross_pay * TAX_RATE;}

//
// Deduction
//
			deductions = PARKING_RATE;

//
// Began calculating net pay
//
void calculate_net_pay(double &gross_pay,double &tax,double &deduction,double net_pay)
{
	net_pay = gross_pay - tax - deductions;}

//
// Began outputting payroll data
//
void output_payroll_data(string &full_name,double &regular_hours,double &overtime_hours,double &hourly_rate,double &gross_pay,double &tax,double &deductions,double &net_pay)
{
        cout << endl;
        cout << "12345678901234567890##21.00##21.00##321.00##4321.00##321.00##321.00##4321.00" << endl;
        cout << "                      Reg.   Ovt.   Hourly  Gross                    Net    " << endl;
        cout << "Name                  Hours  Hours  Rate    Pay      Taxes   Deduct  Pay    " << endl;
        cout << "====================  =====  =====  ======  =======  ======  ======  =======" << endl;
        cout << left << setw(20) << full_name << "  ";
        cout << right << fixed << setprecision(2);
        cout << setw(5) << regular_hours << "  ";
        cout << setw(5) << overtime_hours << "  ";
        cout << setw(6) << hourly_rate << "  ";
        cout << setw(7) << gross_pay << "  ";
        cout << setw(6) << tax << "  ";
        cout << setw(6) << deductions << "  ";
		cout << setw(7) << net_pay << endl << endl;}

//
// Prompts the user to answer a yes/no question, returning 'Y' or 'N'.
//
void get_yesno(string &prompt,char yesno)
{
        do {
            cout << "Process another employee (Y/N)? ";
            cin >> yesno;
            if (yesno == 'y')
                yesno = 'Y';
            else if (yesno == 'n')
                yesno = 'N';
            if (yesno != 'Y' && yesno != 'N')
                cout << "Please type 'Y' for yes or 'N' for no"
                     << endl << endl;
        } while (yesno != 'Y' && yesno != 'N');
        cout << endl;

    } while (yesno == 'Y');cout << endl;
Hi,

Welcome to cplusplus :+)

Congratulations on using code tags - that's awesome !

Here are some things to help you out, although you didn't actually ask a question or specify what problems you have.

Don't have line 5 - google to see why this is bad.

Don't use #define 's - make them const variables instead - but don't make them global. A funny thing I read yesterday: The names of global variables should start with // :+D

With this:

28
29
30
31
32
33
34
35
36
37
38
39
40
    string first_name,   // employee's first name, input by user
        last_name,       // employee's last name, input by user
        full_name;       // employee's concatenated full name

    double hours,        // number of hours worked, input by user
        regular_hours,   // number of regular hours
        overtime_hours,  // number of overtime hours
        hourly_rate,     // hourly pay rate
        gross_pay,       // employee's gross pay
        tax,             // employee's tax amount
        deductions,      // monthly deductions
        net_pay;         // employee's net pay
    char   yesno;        // user's yes or no response 


Always specify 1 variable per line, and always initialise it to something, as in:

28
29
30
31
32
33
34
35
36
37
38
39
40
        std::string first_name = "aaa";       // employee's first name, input by user
        std::string last_name = "bbb";       // employee's last name, input by user
        std::string full_name  = "zzz";      // employee's concatenated full name

        double hours              = 0.0;        // number of hours worked, input by user
        double regular_hours  = 0.0; // number of regular hours
        double overtime_hours = 0.0;  // number of overtime hours
        double hourly_rate       = 0.0;    // hourly pay rate
        double gross_pay        = 0.0;      // employee's gross pay
        double tax                  = 0.0;       // employee's tax amount
        double deductions       = 0.0;     // monthly deductions
        double net_pay           = 0.0;      // employee's net pay
    char   yesno;        // user's yes or no response 


With this:

void input_employee_data(string &first_name, string &last_name, string &hours, string &hourly_rate)

Functions should do 1 thing, so have it to get the names, but make other functions to get hours and rates.

With the do loops, notice how you have the conditions twice? I am not a fan of do loops - I always find a way of using a while or for loop. See if you can figure out how to do the same. Just because a do loop always executes at least once, should not be a motivation to use a do loop IMO.

Function parameters should be const , unless you wish to change their value in the scope they were called from:

1
2
3
4
void join_name(const std::string &first_name, const std::string &lastname,  std::string &fullname)
{
    full_name = last_name + ", " + first_name;
}


References are fine for things like std::string and anything that might be big, but aren't needed for basic types like int float double or char etcetera unless you want to change the value:

1
2
void split_hours(const double hours, double &regular_hours, double &overtime_hours)
{


Was this bit meant to be a function?

1
2
3
4
//
// Deduction
//
			deductions = PARKING_RATE;


This function is an absolute mess :+)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
// Prompts the user to answer a yes/no question, returning 'Y' or 'N'.
//
void get_yesno(string &prompt,char yesno)
{
        do {
            cout << "Process another employee (Y/N)? ";
            cin >> yesno;
            if (yesno == 'y')
                yesno = 'Y';
            else if (yesno == 'n')
                yesno = 'N';
            if (yesno != 'Y' && yesno != 'N')
                cout << "Please type 'Y' for yes or 'N' for no"
                     << endl << endl;
        } while (yesno != 'Y' && yesno != 'N');
        cout << endl;

    } while (yesno == 'Y');cout << endl;


It achieves nothing because it is void Even if it did return something, it is still a bloody mess. Search on this forum (at the top of this page) using TheIdeasMan bool controlled while loop

When processing a single char answer make use of the toupper or tolower functions to halve your logic.

I particularly hate constructs like this:

if (yesno != 'Y' && yesno != 'N')

because they are ugly and error prone and non scalable.

There you go some stuff to work on.
Hmm I don't get what procedures are... Are those normal functions???

Use of Functions:
If you have lines of code that you would like to use over and over it makes sense to declare and define a funtion in the global scope. I never tried to define them in main(). I usually do it like this

Pseudo-code...

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
#include <iostream>
#include <string>

std::string LastName;
std::string FristName;
std::string FullName;

void DisplayWelcomeMessage()
{
    std::cout << "Welcome to ... this program was made to ... and ...\n";
    //Do more stuff maybe...
}

bool GetLastName()
{
    std::cout << "Enter your last Name: ";
    std::cin >> LastName;
    return LastName
}

bool GetFristName()
{
    std::cout << "Enter your first Name: ";
    std::cin >> FirstName;
    return FirstName;
}

void DisplayFullName()
{
    FullName = FirstName + LastName;
    std::cout << "Hello " << Fullname;
}

//... declare and define other functions and variables

int main()
{
    DisplayWelcomeMessage(); //Display welcoming message
    GetLastName(); //Letting user enter his last Name and save it in std::string LastName
    GetFristName(); //Letting user enter his first Name and save it in std::string FirstName
    DisplayFullName(); //Joining the two strings together, save it in std::string FullName and Output std::string in FullName
  
    return 0;
}



Functions can contain other functions... so i could do

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
#include <iostream>
#include <string>

std::string LastName;
std::string FristName;
std::string FullName;

void DisplayWelcomeMessage()
{
    std::cout << "Welcome to ... this program was made to ... and ...\n";
    //Do more stuff maybe...
}

bool GetLastName()
{
    std::cout << "Enter your last Name: ";
    std::cin >> LastName;
    return LastName
}

bool GetFristName()
{
    std::cout << "Enter your first Name: ";
    std::cin >> FirstName;
    return FirstName;
}

void DisplayFullName()
{
    FullName = FirstName + LastName;
    std::cout << "Hello " << Fullname;
}


void DoAllStuff()
{
    DisplayWelcomeMessage(); //Needs to be defined above defining DoAllStuff or else you get an error message
    GetLastName(); //Needs to be defined above defining DoAllStuff or else you get an error message
    GetFristName(); //Needs to be defined above defining DoAllStuff or else you get an error message
    DisplayFullName(); //Needs to be defined above defining DoAllStuff or else you get an error message
}

int main()
{
    DoAllStuff(); //Displays welcome message, gets last and first Name, joins them and Outputs the result

    return 0;
}


Did this answer your question? Mfg HalfNOob


Hi HalfNOoB,

There are problems with your code, so I am not that is helping the OP.

You have global variables - that is bad unless you are an expert.

Your name functions return a bool, but you return the name - so that will always be true What was you intention there ?

The layout should usually be function declarations, int main(), followed by function definitions. I personally don't want to scroll down to see where main is.

std::cin only reads up to the first space, so if the name is "di Masi" it won't do what you expect. Use std::getline instead. Forgot to mention that to the OP.

Be careful posting replies: if you are wrong someone will let you know - it has happened to me a relatively small number of times, and I am not the only one.
Last edited on
@TheIdeasMan

You are right in all of your Points ;)

1) std::cin only reads up until first space. Didn't think of a last Name with more than one word. Just check out Google and you'll find tons of explanations for getline()

2) I used global variables here because the program is still realtively small and it saves time. I've got the same opinion of Namespace as theIdeasMan NEVER USE NAMESPACE IN BIG PROJECT IT KILLS, SERIOUSLY. I'd recommend to use classes if you plan to make this program any bigger...

3) bool is obviously complete bull**** :o and should not be the type of the functions above. It should be std::string for GetlastName() and for GetFirstName().

Sorry for this hasty answer an thank you IdeaMan, to stop other People being confused by my alsdfjsldfj post.

What I wanted to show you, is that functions can contain other functions, if those were previously defined... (I dont have a Debugger at my working Station...)

Greets HalfNOoB
Last edited on
Hi HalfNOoB,

I think you meant to explain to the OP, not to me :+).

I used global variables here because the program is still relatively small and it saves time.


A bad habit: don't do it for any program - especially when replying to a beginner.

With namepsace ideally one should put their own stuff in their own namespace.

(I dont have a Debugger at my working Station...)


I doubt that would help here: did you mean compiler?

At the moment we are running the risk of hijacking the OP's topic.
Topic archived. No new replies allowed.