VoidFunctions

Question 6c

Write a program named question6c.cpp that demonstrates the use of the following functions.

A C++ function named getName() prompts the user for two string values; first name and last name and return a combination of the two as one value.

The second function getHours() prompts the user for hours worked for each day of the week, i.e. Monday – Friday and calculate the total number of hours worked. The function getHours() should return the total number of hours that the employee worked that week.

The third function printPay() calculates an employee’s weekly pay, it must receive two actual parameters, fullName, a string variable, and a float value for the number of hours the employee has worked. Employees who have worked more than 40 hours that week will receive a bonus of 10% and those who have worked less than 40 hour will receive 10% less pay for that week. The function printPay() should print a message to indicate whether the employee receive a bonus or whether 10% will be deducted from his pay. It also prints the total amount that the employee will be paid for that week. Use a constant RATE with a value of 120.35 to represent the hourly rate that an employee is paid.

**(The program just ends once the values are entered. The printPay() function is not working, can anyone tell me what i am doing wrong)**



#include <iostream>
#include <cmath>
#include <string>
using namespace std;

string getName(string firstname, string lastname, string fullname)
{

fullname = firstname + " " + lastname;
cout << fullname;

return fullname;
}

float getHours(float Mon, float Tue, float Wed, double Thu, float Fri)
{
float TotalHours;
TotalHours = Mon + Tue + Wed + Thu + Fri;

return TotalHours;
}

int main()
{
string firstname, lastname, fullname;
float Mon, Tue, Wed, Thu, Fri;

cout << "Enter your first name: " << endl;
cin >> firstname;
cout << "Enter your last name: " << endl;
cin >> lastname;

cout << "Enter the hours you worked for each day of the week (Monday-Friday)" << endl << endl;
cout << "Monday: "; cin >> Mon;
cout << "Tuesday: "; cin >> Tue;
cout << "Wednesday: "; cin >> Wed;
cout << "Thursday: "; cin >> Thu;
cout << "Friday: "; cin >> Fri;

return firstname, lastname, fullname, Mon, Tue, Wed, Thu, Fri;
}

float printPay(string fullname, float TotalHours)
{
const float HOURLY_PAY_RATE = 120.35;
float percentage, totalpay, totalpayplusbonus;

totalpay = TotalHours * HOURLY_PAY_RATE;

if (TotalHours >= 40)
{
percentage = (totalpay / 100) * 10;
totalpayplusbonus = totalpay + percentage;
}
else
{
percentage = (totalpay / 100) * 10;
totalpayplusbonus = totalpay - percentage;
}
cout << fullname << "If you worked over 40 hours in the week you will receive a 10 % bonus"
<< " ,however if you havent worked over 40 hours there will be 10% deducted from your pay." << endl;
cout << "Your pay is R:" << totalpayplusbonus << endl;
return 0;
}
closed account (48T7M4Gy)
Please use code tags
Main should only ever return an integer; either 0 for success or an integer indicating the error code. I don't know what you are trying to do with your return from main here but it isn't right. To answer your question, you never call "printPay()" so how could it ever execute?
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

string getName(string firstname, string lastname)
{
string fullname;

fullname = firstname + " " + lastname;

return fullname;

}

float getHours(float Mon, float Tue, float Wed, float Thu, float Fri)
{
float TotalHours;

TotalHours = Mon + Tue + Wed + Thu + Fri;

return TotalHours;
}

float printPay(string fullname, float TotalHours)
{
const float HOURLY_PAY_RATE = 120.35;
float percentage, totalpay, totalpayplusbonus;

totalpay = TotalHours * HOURLY_PAY_RATE;

if (TotalHours == 40)
{
percentage = (totalpay / 100) * 10;
totalpayplusbonus = totalpay + percentage;
cout << fullname << " Congradulations! You have worked over 40 hours this week so you receive a 10% bonus" << endl;
cout << "Your pay is R:" << totalpayplusbonus << endl;
}
else if(TotalHours != 40)
{
percentage = (totalpay / 100) * 10;
totalpayplusbonus = totalpay - percentage;
cout << fullname << " Sorry! You did not work 40 hours this week so 10% will be deducted from your pay" << endl;
cout << "Your pay is R:" << totalpayplusbonus << endl;
}

return 0;
}

int main()
{
string firstname, lastname,a, b, c, fullname;
float Mon, Tue, Wed, Thu, Fri, TotalHours;

cout << "Enter your first name: " << endl;
cin >> firstname;
cout << "Enter your last name: " << endl;
cin >> lastname;

a = getName(firstname, lastname);

cout << "Enter the hours you worked for each day of the week (Monday-Friday)" << endl << endl;
cout << "Monday: "; cin >> Mon;
cout << "Tuesday: "; cin >> Tue;
cout << "Wednesday: "; cin >> Wed;
cout << "Thursday: "; cin >> Thu;
cout << "Friday: "; cin >> Fri;

b = getHours(Mon, Tue, Wed, Thu, Fri);

c = printPay(fullname, TotalHours);

return 0;
}


** this is my final program.. but the final answer of my pay is coming as a weird answer like R2.31e25.. and the full name is not displaying.. can anyone tell me why??
closed account (48T7M4Gy)
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
int main()
{
    //string firstname, lastname,a, b, fullname;
    string firstname, lastname,a,  c, fullname;

    //float Mon, Tue, Wed, Thu, Fri, TotalHours;
    float Mon, Tue, Wed, Thu, Fri, TotalHours, b;

    cout << "Enter your first name: " << endl;
    cin >> firstname;
    cout << "Enter your last name: " << endl;
    cin >> lastname;

    a = getName(firstname, lastname);

    cout << "Enter the hours you worked for each day of the week (Monday-Friday)" << endl << endl;
    cout << "Monday: ";
    cin >> Mon;
    cout << "Tuesday: ";
    cin >> Tue;
    cout << "Wednesday: ";
    cin >> Wed;
    cout << "Thursday: ";
    cin >> Thu;
    cout << "Friday: ";
    cin >> Fri;

    b = getHours(Mon, Tue, Wed, Thu, Fri);

    //c = printPay(fullname, TotalHours);
    c = printPay(fullname, b);
    return 0;
}
closed account (48T7M4Gy)
You also have a small problem with the 40 hour test. There are 3 tests - < 40 hours, > 40 hours and = 40 hours. Presumably if someone works exactly 40 hours there is no adjustment to their pay.
Thanks so much!! its working perfectly now..
Topic archived. No new replies allowed.