Calculating employee pay using pass by reference and function overloading - c++

I have not done so well on an assignment for school - I have submitted what is below and I would like to learn and understand more about why my code is terrible lol.
The grading criteria is here
As you can tell, I did not do well at all. I am getting a few errors all related to error C2064: term does not evaluate to a function taking 3 arguments. One of the requirements is using pass by reference. When I attempted that, I had many more errors - so I decided to take it back to something a bit simpler for me and I still cannot see what I am doing wrong.
My instructor specified which parameters we needed to use so I am using those specific ones for each function. The other criterion in writing is below as well as my code.
Again, yes this was for a school assignment, I've already submitted it and got a terrible grade. I would like to have an understanding of what I did wrong and how to correct it so I can learn. Thank you for any and all help.
The weekly gross pay of a salaried employee is calculated by dividing
the employee’s annual salary by 52. Create a named constant variable
to hold and use the value in your overloaded function.
• The weekly gross pay of an Hourly employee is calculated as follows:
If the number of hours worked by the employee is 40 hours or less,
the gross pay is calculated by multiplying the number of hours worked
by the pay rate. If the number of hours worked by the employee
exceeds 40 hours, the employee will receive regular pay for the first
40 hours, and receive time-and-a-half pay for the hours in excess of
40. An hourly employee must not work more than 50 hours per week.
• The weekly gross pay of an intern is calculated by multiplying the
number of hours worked by the pay rate. If an intern works over 15
hours a week, the intern receives time-and-a-quarter pay for the
hours over 15 hours. An intern must not work more than 20 hours per
week.
• The weekly gross pay of a contract employee is calculated by
multiplying the number of hours worked by the pay rate. A contract
employee must not work more than 40 hours per week.
Create four overloaded functions (one each for salaried employee,
hourly employee, contract employee, and Intern) as follows:
 For the Salaried employee function, prompt for two values (employee
number and yearly salary), and pass these two values to the
appropriate function.
 For the Hourly employee function, prompt for three values (employee
number, hours worked and pay rate), and pass these three values to the
appropriate function.
 For the Intern function, prompt for four values (Institution Code,
Department Code, hours worked and pay rate), and pass these four
values to the appropriate function.
• For the Contract employee function, prompt for five values (company
number, project number, employee number, hours worked and pay rate),
and pass these five values to the appropriate function.
For any employee, if more hours than the employee is allowed to work
(based on the type of employee) is entered for number of hours,
reject the input as invalid input, and repeatedly prompt for the valid
range of hours for the employee type until the correct information is
entered.
Continue to request, calculate and display employee information until
there is no more employee data to process. The information displayed
must include all the information collected for each employee and the
calculated weekly pay. Use the end-of-file indicator to terminate
input.
Your program must be able to process zero employees, to an infinite
number of employees. When the program starts, if there is no data to
process during this processing cycle, immediately use the end-of-file
indicator to terminate processing, and display an appropriate
message.
Demonstrate the use of the following in your solution:
• Iteration (Looping)
• Modularity (using functions)
• Function Overloading (use overloaded functions for calculating Gross
Pay)
• Function Prototyping (create a function prototype for each of the
overloaded functions)
• Inter-function communication. To demonstrate inter-function
communication, all prompts for input must occur in the function
main(), all calculated gross pay amounts must be explicitly returned
to the function main(), and all output must be displayed by the
function main(). No output can be displayed by the functions.
• Use of pass-by-reference using Reference arguments. There must be,
at least, one example of pass-by-reference using Reference arguments
with each function in the program, except main(). Your program,
depending on the argument(s) supplied by the user, should invoke the
appropriate overloaded function.*
#include <iostream>
#include <iomanip>
using namespace std;
double grossPay(int number, double hours, double pay); // hourly function
double grossPay(int number, double salary); // salary function
double grossPay(int company, int project, int number, double hours, double pay); // contract function
double grossPay(int institution, int department, double hours, double pay); // intern function
int main() {
// prompt user for type of employee and give EOF instructions.
cout << "Enter '1' for hourly. Enter '2' for salaried." << endl;
cout << "Enter '3' for contracter. Enter '4' for intern." << endl;
cout << "If result is -1, the input was invalid, please input correct employee hours." << endl;
cout << endl;
cout << "Terminate input by using <ctrl> z on Windows then press enter." << endl;
cout << "Terminate input by using <ctrl> z on UNIX / Linux / Mac OS X then press enter." << endl;
int employeeType{ 0 };
int employeeNumber{ 0 };
double grossPay{ 0 };
double overtimePay{ 0 };
// salaried
double employeeSalary{ 0 };
// hourly
double hoursWorked{ 0 };
double payRate{ 0 };
// contractor
int companyNum{ 0 };
int projectNum{ 0 };
// intern
int schoolCode{ 0 };
int departmentCode{ 0 };
while (cin >> employeeType) {
switch (employeeType) {
case 1:
// HOURLY employee prompts and output
cout << "Enter employee number: " << endl;
cin >> employeeNumber;
cout << "Enter number of hours employee worked: " << endl;
cin >> hoursWorked;
cout << "Enter employees pay rate: " << endl;
cin >> payRate;
cout << setprecision(2) << fixed;
cout << "Gross pay of employee including overtime, is $" << grossPay(employeeNumber, hoursWorked, payRate) << endl;
cout << endl;
break;
case 2:
// SALARIED employee prompts and output
cout << "Enter employee number: " << endl;
cin >> employeeNumber;
cout << "Enter employees salary: " << endl;
cin >> payRate;
cout << setprecision(2) << fixed;
cout << "Gross pay of employee" << employeeNumber << "is $" << grossPay(employeeNumber, payRate) << endl;
cout << endl;
break;
case 3:
// CONTRACT employee prompts and output
cout << "Enter company number: " << endl;
cin >> companyNum;
cout << "Enter project number: " << endl;
cin >> projectNum;
cout << "Enter employee number: " << endl;
cin >> employeeNumber;
cout << "Enter number of hours employee worked: " << endl;
cin >> hoursWorked;
cout << "Enter employees pay rate: " << endl;
cin >> payRate;
cout << setprecision(2) << fixed;
cout << "Gross pay of contractor is $" << grossPay(companyNum, projectNum, employeeNumber, hoursWorked, payRate) << endl;
cout << endl;
break;
case 4:
// INTERN prompts and output
cout << "Enter institution code: " << endl;
cin >> schoolCode;
cout << "Enter department code: " << endl;
cin >> departmentCode;
cout << "Enter number of hours employee worked: " << endl;
cin >> hoursWorked;
cout << "Enter employees pay rate: " << endl;
cin >> payRate;
cout << setprecision(2) << fixed;
cout << "Gross pay of intern $" << grossPay(schoolCode, departmentCode, hoursWorked, payRate) << endl;
cout << endl;
break;
}
cout << "Enter '1' for hourly. Enter '2' for salaried." << endl;
cout << "Enter '3' for contracter. Enter '4' for intern." << endl;
cout << endl;
}
cout << endl;
cout << "Thank you for using this program. " << endl;
}
// hourly function
double grossPay(int number, double hours, double pay) {
double hourlyWeek{ 0 };
while (hours > 0 && hours <= 50.00) {
if (hours > 40.00) {
hourlyWeek = ((pay * 40.00) + (hours - 40.00) * (pay * 1.50));
}
else {
hourlyWeek = (hours * pay);
}
}
while (hours > 50.00) {
hourlyWeek = -1;
}
return hourlyWeek;
}
// salary function
double grossPay(int number, double salary) {
double salaryWeek{ 0 };
salaryWeek = (salary * 40);
return salaryWeek;
}
//contractor function
double grossPay(int company, int project, int number, double hours, double pay) {
double contractWeek{ 0 };
while (hours > 0 && hours <= 40.00) {
contractWeek = ((pay * 40.00) + (hours - 40.00) * (pay * 1.50));
}
while (hours > 40.00) {
contractWeek = -1;
}
return contractWeek;
}
// intern function
double grossPay(int institution, int department, double hours, double pay) {
double internWeek{ 0 };
while (hours > 0 && hours <= 20.00) {
if (hours > 15.00) {
internWeek = ((pay * 40.00) + (hours - 40.00) * (pay * 1.25));
}
else {
internWeek = (hours * pay);
}
}
while (hours > 20.00) {
internWeek = -1;
}
return internWeek;
}

rename your local variable:
double grossPay{ 0 };
because you cannot let your variables and function have the same name (grossPay)
by default, the compiler would use virable instead of the method, that's why you got the mistake :)

Related

Having some trouble with a assignment in c++

I have a assignment I've been working on and my code is working but the results are off by a hair. I know the issue has to do with rounding but I just can't seem to figure out where the issue lies. I've included the assignment details as well as the results i'm getting versus the expected result. Any help is appreciated.
Link for images https://imgur.com/a/bqIcxfT
'''
// This program will display the size of a population for given number of years
// taking into account annual birth and death rates as well as the number of
// people who move away and move into the area.
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototype
double calculatePop (double, double, double, int, int);
int main ()
{
double P; // Starting population
double B; // Annual birth rate
double D; // Annual death rate
int A; // Average number of people who arrive
int M; // Average number of people who move away
int nYears; // The number of years to display
cout << "This program calculates population change." << endl;
// Set numeric formatting
cout << setprecision(0) << fixed;
// Get starting population size
cout << "Enter the starting population size: ";
cin >> P;
while (P<2){
cout << "Starting population must be 2 or more.";
cout << "Please re-enter:";
cin >> P;}
// Get the annual birth rate
cout << "Enter the annual birth rate (as % of current population): ";
cin >> B;
while (B<0){
cout << "Birth rate percent cannot be negative.";
cout << "Please re-enter:";
cin >> B;}
B/=100;
// Get annual death rate
cout << "Enter the annual death rate (as % of current population): ";
cin >> D;
while (D<0){
cout << "Death rate percent cannot be negative.";
cout << "Please re-enter:";
cin >> D;}
D/=100;
// Get number of people who arrive
cout << "How many individuals move into the area each year? ";
cin >> A;
while (A<0){
cout << "Arrivals cannot be negative.";
cout << "Please re-enter:";
cin >> A;}
// Get number of people who move away
cout << "How many individuals leave the area each year? ";
cin >> M;
while (M<0){
cout << "Departures cannot be negative.";
cout << "Please re-enter:";
cin >> M;}
// Get number of years to see data for
cout << "For how many years do you wish to view population changes? " << endl << endl;
cin >> nYears;
while (nYears<1){
cout << "Years must be one or more.";
cout << "Please re-enter:";
cin >> nYears;}
cout << "Starting population: " << P << endl << endl;
//Display the population to user
for (int y=1; y<=nYears; y++)
{
P = calculatePop(P, B, D, A, M);
cout << "Population at the end of year " << y << " is " << P << ".\n";
}
}
double calculatePop (double P, double B, double D, int A, int M)
{
double N; //New Population Size
N = P + (B*P) - (D*P) + A - M;
return N;
}
'''
The value is correctly calculated but not outputted the same way as in the assignment. Setting setprecision(0) along with fixed will round the number to the nearest integer while the result shown in the assignment is the truncated number. To truncate the result, use
cout << "Population at the end of year " << y << " is " << int(P) << ".\n";

Compiler keeps ignoring cin values ?? help me solve

I am getting a problem while using
cin >> PayRate
and
cin >> H_worked
what could be the problem? Even the compiler doesn't show any errors but when program runs the 2nd and 3rd cin values aren't read by compiler.
Program:
#include <iostream.h>
#include<conio.h>
int main()
{
int employeeName, H_worked;
float PayRate, GrossPay, NetPay;
cout << "Please input your employee's name:";
cin >> employeeName;
cout << "Input hourly wage rate:";
cin >> PayRate;
cout << endl;
cout << "Input hours worked :";
cin >> H_worked;
cout << endl;
if (H_worked > 40)
{
H_worked = H_worked*1.5;
GrossPay = H_worked*PayRate;
cout << "Your employees gross pay for this week is" << GrossPay << endl;
NetPay = GrossPay - (GrossPay* 3.625);
cout << "Your employees net pay is" << NetPay << endl;
}
else (H_worked <= 40);
{
GrossPay = H_worked*PayRate;
cout << "Your employees gross pay for this week is" << GrossPay << endl;
NetPay = GrossPay - (GrossPay*3.625);
cout << "And your employees net pay is" << NetPay << endl;
}
return 0;
getch();
}
You declared employeeName as an int but that does not make any sense as names have letters not numbers. If you are actually entering in character data then this will cause cin to fail and that will make any subsequent call fail as well. This fits with your description of what is happening. To fix this first we need to make employeeName a std::string so we can store letters.
int employeeName, H_worked;
//becomes
std::string employeeName;
int H_worked;
Then we need to change the input method. Since a name can have spaces in it we need to use std::getline instead of >> to get the name as >> will stop when it sees a space.
cout << "Please input your employee's name:";
std::getline(std::cin, employeeName);
You also have a semicolon at the end of your else condition
else (H_worked <= 40);
This means that
{
GrossPay = H_worked*PayRate;
cout << "Your employees gross pay for this week is" << GrossPay << endl;
NetPay = GrossPay - (GrossPay*3.625);
cout << "And your employees net pay is" << NetPay << endl;
}
Will always run as ; ends the else part.
Then we have the issue that you are using non standard includes. In standard c++
#include <iostream.h>
Should be
#include <iostream>
As all standard includes omit the .h. Since all of the standard components live in the std namespace you are also going to have to deal with that. You can either put std:: in front of all the std components or you can put using std::cout;, using std::cin, ect. I would not suggest using using namespace std; for the reasons outlined in Why is “using namespace std;” considered bad practice?

C++: Calculations are incorrect when entering a certain input

I've been teaching myself C++ on and off for a few months and now I'm trying to make a payroll system. Here's my code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void wageCompute (int, int);
int main()
{
int loopTimes=0;
int empNum=100, workHours, otHours, rate, bPay, otPay, grossPay;
string empName, empPos;
cout << "PAYROLL FOR THE MONTH OF MARCH" << endl;
cout << "Employees: " << empNum << endl;
while (empNum>loopTimes)
{
cout << "NAME: ";
cin.ignore();
getline (cin, empName);
cout << "\nPOSITION: ";
getline (cin, empPos);
cout << "\nHOURS WORKED: ";
cin >> workHours;
cout << "\nWAGE PER HOUR: ";
cin >> rate;
bPay = workHours*rate;
cout << "YOUR BASE PAY IS: " << bPay << endl << endl;
cout << "HOURS WORKED OVERTIME: ";
cin >> otHours;
otPay = (1.5*rate)*otHours;
cout << "\nOVERTIME PAY: " << otPay << endl;
grossPay = bPay + otPay;
cout << "GROSS PAY: " << grossPay << endl;
wageCompute(bPay, grossPay);
loopTimes++;
}
return EXIT_SUCCESS;
}
void wageCompute(int bPay, int grossPay)
{
double rate, dedInsurance, dedMedical, totDeduct, netPay, tax;
if (bPay<10001)
{
rate = 0.05;
}
else if (bPay<15001)
{
rate = 0.1;
}
else if (bPay<20001)
{
rate = 0.15;
}
else
{
rate = .2;
}
tax = bPay*rate;
dedInsurance = bPay*0.05;
dedMedical = bPay*0.01;
totDeduct = tax + dedInsurance + dedMedical;
cout << "TAX: " << tax << endl;
cout << "SSS DEDUCTION: " << dedInsurance << endl;
cout << "Medicare DEDUCTION: " << dedMedical << endl;
cout << "TOTAL DEDUCTIONS: " << totDeduct << endl;
netPay = grossPay - totDeduct;
cout << "NET PAY: " << netPay << endl;
}
The part where it goes wrong is when I input a certain value for the Hours Worked, Wage per Hour and Hours worked overtime. The program checks the basic pay for the suitable amount of tax it should deduct, what I input was 160 for the hours worked, 100 for the wage per hour, and 10 for overtime. I've tried lessening and increasing it and it worked just fine it seems that it's just these combination of numbers is the part where it goes wrong.
A screenshot of the output:
Your question isn't very clear but I suspect that what you are seeing here is a well known limitation of floating point numbers; numbers that are easy to represent exactly in base 10 don't have an exact representation in base 2. One example : 0.1 in base 10 is 0.0001100110011… repeating in base 2; the accuracy of the approximation depends on how many bits one is willing to use to write it.
An alternative approach is to use integer arithmetic with a known precision, so say you want to calculate to the nearest hundredth of a penny (I'm using UK currency here). Represent £1.01 as 10100 and when your finished val / 10000 is the pounds and (val % 10000) / 100 is the pence. If needed you can implement some more complex rules around rounding for the pence.

C++ Segfault No compile errors can't find cause

Hoping someone can help. I'm able to compile with no error, I'm not finding any syntax errors but when I run this, it crashes. Debug segfaults on launch. Full disclosure, this is homework. I'm not asking for someone to code this, just look at my problem and my existing code and maybe point me toward what I did that broke this so badly?
Question: You found an exciting summer job for five weeks. It pays, say, $15.50 per hour. Suppose that the total tax you pay on your summer job income is 14%. After paying the taxes you spend 10 % of your net income to buy new clothes and other accessories for the next school year and 1% to buy school supplies. After buying clothes and school supplies, you use 25% of the remaining money to buy savings bonds. For each dollar you spend to buy savings bonds, your parents spend $0.50 to buy additional savings bonds for you. Write a program that prompts the user to enter the pay rate for an hour and the number of hours you worked each week. The program then outputs the following:
a. Your income before and after taxes from your summer job.
b. The money you spend on clothes and other accessories.
c. The money you spend on school supplies.
d. The money you spend to buy savings bonds.
e. The money your parents spend to buy additional savings bonds for you.
Code:
// Libraries defined
#include <iomanip>
#include <iostream>
using namespace std;
//Main function
int main ()
{
//Input variables
double hourlyrate;
double hweek1;
double hweek2;
double hweek3;
double hweek4;
double hweek5;
//Output variables
double beforetax;
double netincome;
double clothmoney;
double suppliesmoney;
double moneyonbonds;
double additionalbonds;
double remain;
//This statement takes care of the decimal places
cout << fixed << showpoint << setprecision(2);
//Input from user
cout << "Enter your hourly rate: " << hourlyrate;
cin >> hourlyrate;
cout << "Week 1: " << hweek1;
cin >> hweek1;
cout << "Week 2: " << hweek2;
cin >> hweek2;
cout << "Week 3: " << hweek3;
cin >> hweek3;
cout << "Week 4: " << hweek4;
cin >> hweek4;
cout << "Week 5: " << hweek5;
cin >> hweek5;
//Mathematics
beforetax = hourlyrate * (hweek1 + hweek2 + hweek3 + hweek4+
hweek5) ;
netincome = beforetax - beforetax * 0.14;
clothmoney = netincome * 0.1;
suppliesmoney = netincome * 0.01;
remain = netincome - clothmoney - suppliesmoney;
moneyonbonds = remain * 0.25;
additionalbonds = static_cast<double>(moneyonbonds) * .50;
//Output to user
cout << endl << "Income before tax = $" << beforetax << endl
<< "Net income = $" << netincome << endl << "Money for clothes/accesories = $"
<< clothmoney << endl << "Money for supplies = $"<< suppliesmoney << endl
<< "Money for saving bonds = $" << moneyonbonds << endl
<< "Additional saving bonds money = $" << additionalbonds;
return 0;
}
I received this error
cout << "Enter your hourly rate: " << hourlyrate;
You try to output the variable before you initialize it.
This is probably unintentional.
The next line is
cin >> hourlyrate
It is the same for every variable. You should initialize them or not output them.
Are you sure about this:
cout << "Enter your hourly rate: " << hourlyrate;
cin >> hourlyrate;
cout << "Week 1: " << hweek1;
cin >> hweek1;
cout << "Week 2: " << hweek2;
cin >> hweek2;
cout << "Week 3: " << hweek3;
cin >> hweek3;
cout << "Week 4: " << hweek4;
cin >> hweek4;
cout << "Week 5: " << hweek5;
cin >> hweek5;
I think that you wanted:
cout << "Enter your hourly rate: ";
cin >> hourlyrate;
instead of:
cout << "Enter your hourly rate: "<< hourlyrate;
cin >> hourlyrate;

C++ breaking a program up into functions

My assignment is use 3 functions with parameters, as follows:
function calcRegBill – accepts one integer argument for the number of minutes used. Determines and returns the total amount due.
function calcPremBill – accepts two integer arguments, for the number of day minutes and number of night minutes used. Determines and returns the total amount due.
function printBill – accepts 4 arguments: a string account number, a character service code, an integer total number of minutes used, and an amount due. Note that this is a generic print bill function, which prints either a regular or premium bill, using the following format:
Account Number: XXXX
Service Type: Regular (or Premium, depending on the character received)
Total Minutes: XXX
Amount Due: $XXX.XX
Your main function will prompt the user for the account number and service code. Based on the service code, main will ask for the correct number of minutes, then call your functions above as needed to finish the job. In addition you must :
Incorporate a loop in your program to run the bill as many times as needed. You may do this either by a sentinel controlled loop, or with a counter controlled loop.
I already built the program and tested it with everything in the main function of the program. I am just really confused about how to break it into the 3 separate functions and have it all still work. I am a total noob at C++
Here is the program so far, I started to add the functions, but I do not believe they're right.
// Cell Bill Fun
// April 14, 2013
#include <iostream>
#include <iomanip>
using namespace std;
double calcRegBill(int a);
double calcPremBill(int b, int c);
void printBill(string acctNumber, char serviceCode, int d, double e);
int main()
{
//declare variables for question 4
char serviceCode;
int acctNumber;
int minutes;
int dayMinutes;
int nightMinutes;
int charge;
int dayFee;
int nightFee;
double amtDue;
//get input
cout << "Please enter your information to calculate your cell phone bill ";
cout << "What is your account number? (please enter a 4-digit number-example 1234): ";
cin >> acctNumber;
cout << "Do you have regular or premium service? Enter r for regular service, p for Premium.: ";
cin >> serviceCode;
//format output
cout<< setprecision(2) << fixed;
//output
switch (serviceCode)
{
case 'r':{
cout << "How many minutes did you use?: ";
cin >> minutes;
if (minutes <= 50)
amtDue = 10;
else if (minutes > 50)
amtDue=10+((minutes-50)*.20);
else
cout <<"You have input an invalid service code. Please type r for regular or p for premium service." << endl;
cout <<"Cellular Account #:" << acctNumber << endl;
cout <<"Type of Service: Regular" << endl;
cout <<"Total Minutes:" << minutes << endl;
cout <<"Amount Due: $"<< amtDue << endl;}
break;
case 'R':{
cout << "How many minutes did you use?: ";
cin >> minutes;
if (minutes <= 50)
amtDue = 10;
else if (minutes > 50)
amtDue=10+((minutes-50)*.20);
else
cout <<"You have input an invalid service code. Please type r for regular or p for premium service." << endl;
cout <<"Cellular Account #:" << acctNumber << endl;
cout <<"Type of Service: Regular" << endl;
cout <<"Total Minutes:" << minutes << endl;
cout <<"Amount Due: $"<< amtDue << endl;}
break;
case 'p':
cout << "How many daytime minutes did you use?";
cin >> dayMinutes;
if (dayMinutes <= 75)
dayFee = 0;
else if (dayMinutes > 75)
dayFee=((dayMinutes-75)*.10);
cout << "How many night time minutes did you use?";
cin >> nightMinutes;
if (nightMinutes <= 100)
nightFee = 0;
else if (nightMinutes > 100)
nightFee=((nightMinutes-100)*.05);
else
cout <<"You have input an invalid service code. Please type r for regular or p for premium service." << endl;
cout <<"Cellular Account #:" << acctNumber << endl;
cout <<"Type of Service: Premium" << endl;
cout <<"Total Minutes:" <<dayMinutes+nightMinutes << endl;
cout <<"Amount Due: $"<<25<<"+"<<dayFee<<"+"<<nightFee<<"= $"<<25+dayFee+nightFee << endl;
break;
case 'P':
cout << "How many daytime minutes did you use?";
cin >> dayMinutes;
if (dayMinutes <= 75)
dayFee = 0;
else if (dayMinutes > 75)
dayFee=((dayMinutes-75)*.10);
cout << "How many night time minutes did you use?";
cin >> nightMinutes;
if (nightMinutes <= 100)
nightFee = 0;
else if (nightMinutes > 100)
nightFee=((nightMinutes-100)*.05);
else
cout <<"You have input an invalid service code. Please type r for regular or p for premium service." << endl;
cout <<"Cellular Account #:" << acctNumber << endl;
cout <<"Type of Service: Premium" << endl;
cout <<"Total Minutes:" <<dayMinutes+nightMinutes << endl;
cout <<"Amount Due: $"<<25<<"+"<<dayFee<<"+"<<nightFee<<"= $"<<25+dayFee+nightFee << endl;
break;
default:
cout << "Invalid Service Code. Enter r for regular service, p for Premium.";
}
return 0;
}
double calcRegBill(int a)
{
}
double calcPremBill(int b, int c)
{
}
void printBill(string acctNumber, char serviceCode, int d, double e )
{
return;
}
Functions work by requesting data (the parameters you pass to them), and operating on this data, often by returning data.
For example, in the case 'r': block, instead of of your code, you would want to have:
cout << "How many minutes did you use?: ";
cin >> minutes;
amtDue = calcRegBill(minutes);
cout <<"Cellular Account #:" << acctNumber << endl;
cout <<"Type of Service: Regular" << endl;
cout <<"Total Minutes:" << minutes << endl;
cout <<"Amount Due: $"<< amtDue << endl;}
break;
Then, you can move the code that was previously in main to calculate amtDue into the calcRegBill() function, like this:
double calcRegBill(int minutes)
{
double bill;
if (a < 50)
bill = 10;
else
bill = 10+((minutes-50)*.20);
return bill;
}
The key here is that instead of calculating amtDue in the main function, you calculate it in the calcRegBill function and return it to the main function. Also, notice that I changed the name of the parameter from a to minutes. This improves clarity of its purpose in the function.
The general structure of your program is mostly correct, and I am not really sure what you are asking.
For trouble shooting, write one of your functions and test it without all the other stuff. For example, write calcRegBill...
Then write a very simple main:
int main() {
cout << calcRegBill(3) << endl;
cout << calcRegBill(11) << endl;
}
Did you get the values expected? If so then move on to the next function. Development is often about breaking the problem down into smaller manageable problems.
You need to break down the flow of what you're doing.
First, you gather information. Note: The information you're looking for is the same, regardless of whether it's a premium or regular bill. You don't need to branch so early.
In order to calculate their bill, you branch based on premium or regular. Either way, you get back a double from this, so you can have a double variable that you store the return from calcRegBill or calcPremBill into. Declare this double variable before the branch, assign to it inside of your two branches (regular or premium).
Then, when you call printBill, you can pass this value and what type of bill it was.