I have compiled and run the my equation and it seems to work completely fine from a an user interface and from the compile side, but the equation is getting lost in the inheritance somewhere. Any thoughts?
#include <iostream>
#include <cmath>
using namespace std;
class MortgageCalc
{
private:
float loan, interest, term;
int years;
float monthlyDue, totalDue, totalInt;
public:
MortgageCalc() = default;
void setData(float, float, float);
float setLoan(void); //mutator
float setIntrest(void); //mutator
float setYears(void);
float setTerm (); //mutator
float getMonthlyDue();
float getTotalDue();
float getTotalInt();
};
void MortgageCalc::setData(float l, float i, float y)
{
loan = l;
interest = i;
years = y;
}
float MortgageCalc::setLoan(void)
{
return loan;
}
float MortgageCalc::setIntrest(void)
{
return interest;
}
float MortgageCalc::setYears(void)
{
return years;
}
float MortgageCalc::setTerm()
{
term = pow((1 + ((interest/100) / 12)), (12 * years)); //simple interest calculation with power calc to establish whole number translation
return term;
}
float MortgageCalc::getMonthlyDue()
{
monthlyDue = (loan * ((interest/100) / 12) * term) / (term - 1);
return monthlyDue;
}
float MortgageCalc::getTotalDue()
{
totalDue = (getMonthlyDue() * (years * 12));
return totalDue;
}
float MortgageCalc::getTotalInt()
{
totalInt = (getTotalDue() - loan);
return totalInt;
}
int main()
{
MortgageCalc mort1;
int choice = 0;
int years = 0;
double term(0) , loan(0), interest(0);
cout << "Enter the total loan amount on your mortgage loan: $"; //established loan variable
cin >> loan;
cout << "Enter the interest rate (in whole #'s only): "; //establishes interest rate variable
cin >> interest;
cout << "Enter the length of the loan in years: "; //establishes term of payments
cin >> years;
mort1.setData(loan, interest, years);
while (choice != 3) //loop for menu options and clean function exit
{
cout << endl;
cout << "Program to Calculate Mortgage Payments" << endl <<
"1. Monthly Payment" << endl <<
"2. Total Payment" << endl <<
"3. Exit" << endl << endl <<
"Enter an option above: ";
cin >> choice;
if (choice == 1)
cout << "Monthly payment due is " << mort1.getMonthlyDue() << "." << endl;
else if (choice == 2)
cout << "Total amount for entire loan payments plus interest is $" << mort1.getTotalDue() << "." << endl <<
"Total Interest Paid for this loan amount $" << mort1.getTotalInt() << "." << endl;
}
return 0;
}
Here is the text output:
Enter the total Loan Amount: $100,000
Enter the interest Rate: 5
Enter the length of the loan: 10
Program to calculate mort payments
1. Monthly Payments
2. Total Payments
3. Exit Enter and Option Above: 1
Monthly Payment Due is: -1.56868e-036
Thanks for the support on this one, I found out that the term call was being placed outside of the child reference and therefore was able to update the field and return the correct input.
#include <iostream>
#include <cmath>
using namespace std;
class MortgageCalc
{
private:
float loan, interest, term;
int years;
float monthlyDue, totalDue, totalInt;
public:
MortgageCalc() = default;
void setData(float, float, float);
float setLoan(void); //mutator
float setIntrest(void); //mutator
float setYears(void);
float setTerm (); //mutator
float getMonthlyDue();
float getTotalDue();
float getTotalInt();
};
void MortgageCalc::setData(float l, float i, float y)
{ //copies arguments from input to private members
loan = l;
interest = i;
years = y;
setTerm();
}
float MortgageCalc::setTerm()
{ //simple interest calculation with power calc to establish whole number translation
term = pow((1 + ((interest/100) / 12)), (12 * years));
return term;
}
float MortgageCalc::setLoan(void)
{ //returns loan amt to private member
return loan;
}
float MortgageCalc::setIntrest(void)
{ //returns interest amt to private member
return interest;
}
float MortgageCalc::setYears(void)
{ //returns years to private member
return years;
}
float MortgageCalc::getMonthlyDue()
{ //simple cal to output monthly
monthlyDue = (loan * ((interest/100) / 12) * term) / (term - 1);
return monthlyDue;
}
float MortgageCalc::getTotalDue()
{
totalDue = (getMonthlyDue() * (years * 12));
return totalDue;
}
float MortgageCalc::getTotalInt()
{
totalInt = (getTotalDue() - loan);
return totalInt;
}
int main()
{
MortgageCalc mort1;
int choice = 0;
int years = 0;
double term(0) , loan(0), interest(0);
cout << "Enter the total loan amount on your mortgage loan: $"; //established loan variable
cin >> loan;
cout << "Enter the interest rate (in whole #'s only): "; //establishes interest rate variable
cin >> interest;
cout << "Enter the length of the loan in years: "; //establishes term of payments
cin >> years;
mort1.setData(loan, interest, years);
while (choice != 3) //loop for menu options and clean function exit
{
cout << endl;
cout << "Program to Calculate Mortgage Payments" << endl <<
"1. Monthly Payment" << endl <<
"2. Total Payment" << endl <<
"3. Exit" << endl << endl <<
"Enter an option above: ";
cin >> choice;
if (choice == 1)
cout << "Monthly payment due is " << mort1.getMonthlyDue() << "." << endl;
else if (choice == 2)
cout << "Total amount for entire loan payments plus interest is $" << mort1.getTotalDue() << "." << endl <<
"Total Interest Paid for this loan amount $" << mort1.getTotalInt() << "." << endl;
}
return 0;
}
Related
Can someone help with this code, I keep getting these two errors [Error] expected unqualified-id before 'while' and [Error] expected declaration before '}' token. Coding is not my strong suit and I'm trying to get this assignment done yet my inexperience seems to be showing. Someone tried to explain to me but something with coding doesn't make sense sometimes. Anyone that can maybe help would be greatly appreciated.
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
void GetData (string & fullName, int &idNum, float &workHours,
float &hourlyRate);
void computePay (float &workHours, float &hourlyRate, float &grossPay);
void printReport (string fullName, int idNum, float workHours,
float hourlyRate, float grossPay);
char another;
int idNum;
string name;
float rate;
float hoursWorked;
float grossPay;
int
main ()
{
GetData (name, idNum, hoursWorked, rate);
computePay (hoursWorked, rate, grossPay);
printReport (name, idNum, rate, hoursWorked, grossPay);
cout << "Would you like to enter in another employee (y/n)? \t ";
cin >> another;
}
while (another != 'n');
}
void
GetData (string & name, int &idNum, float &hoursWorked, float &rate)
{
cout << "Please enter your first and last name separated by a space:\t";
getline (cin, name);
cout << "Please enter the four digit employee I.D. number:\t";
cin >> idNum;
cout << "Please enter the hours worked:\t";
cin >> hoursWorked;
cout << "Please enter the hourly rate of pay:\t";
cin >> rate;
}
void
computePay (float &hoursWorked, float &rate, float &grossPay)
{
if (hoursWorked <= 40)
{
grossPay = hoursWorked * rate;
}
else if (hoursWorked <= 60)
{
grossPay = (rate * 40) + (hoursWorked - 40) * 1.5 * rate;
}
else
{
grossPay = (rate * 40) + (30 * rate) + (hoursWorked - 60) * 2.0 * rate;
}
}
void
printReport (string name, int iD, float hoursWorked, float rate,
float grossPay)
{
cout << "Employee Name:\t" << name << endl;
cout << "Employee I.D:\t" << setw (4) << setfill ('0') << idNum << endl;
cout << "Hours worked:\t" << hoursWorked << endl;
cout << "Hourly rate:\t" << rate << endl << endl;
cout.precision (2);
cout << "Gross Pay:\t$" << grossPay << endl << endl;
}
Here is the answer. You were missing a do statement for the corresponding while statement.
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
void GetData (string & fullName, int &idNum, float &workHours,
float &hourlyRate);
void computePay (float &workHours, float &hourlyRate, float &grossPay);
void printReport (string fullName, int idNum, float workHours,
float hourlyRate, float grossPay);
char another;
int idNum;
string name;
float rate;
float hoursWorked;
float grossPay;
int
main ()
{
do
{
GetData (name, idNum, hoursWorked, rate);
computePay (hoursWorked, rate, grossPay);
printReport (name, idNum, rate, hoursWorked, grossPay);
cout << "Would you like to enter in another employee (y/n)? \t ";
cin >> another;
}
while (another != 'n');
}
void
GetData (string & name, int &idNum, float &hoursWorked, float &rate)
{
cout << "Please enter your first and last name separated by a space:\t";
getline (cin, name);
cout << "Please enter the four digit employee I.D. number:\t";
cin >> idNum;
cout << "Please enter the hours worked:\t";
cin >> hoursWorked;
cout << "Please enter the hourly rate of pay:\t";
cin >> rate;
}
void
computePay (float &hoursWorked, float &rate, float &grossPay)
{
if (hoursWorked <= 40)
{
grossPay = hoursWorked * rate;
}
else if (hoursWorked <= 60)
{
grossPay = (rate * 40) + (hoursWorked - 40) * 1.5 * rate;
}
else
{
grossPay = (rate * 40) + (30 * rate) + (hoursWorked - 60) * 2.0 * rate;
}
}
void
printReport (string name, int iD, float hoursWorked, float rate,
float grossPay)
{
cout << "Employee Name:\t" << name << endl;
cout << "Employee I.D:\t" << setw (4) << setfill ('0') << idNum << endl;
cout << "Hours worked:\t" << hoursWorked << endl;
cout << "Hourly rate:\t" << rate << endl << endl;
cout.precision (2);
cout << "Gross Pay:\t$" << grossPay << endl << endl;
}
my program is a simple payroll calculator.
if the employee type is 0 any hours worked over 40 are time and a half.
if type is 1 they don't get overtime just straight pay.
I am trying to hold all the data in struct arrays. This is a homework assignment, and my program meets the requirements, but the issue im having is after i input hours for employees and then choose menu option c, my output is just zero.
Im not sure why exactly there wasn't any data outputted except zero in the column. I have no errors in my compiler.
What did I do wrong?
C++
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//*********************************************************************************************
// Structure to hold employee ID, Name, Pay rate per hour, Employee type
//*********************************************************************************************
struct employeeRecord {
int IDnum; // holds employee id number
string name; // holds employess name
float payRate; // holds employee pay rate
int empType; // holds employee type
};
//*********************************************************************************************
// structure to hold time sheet
//*********************************************************************************************
struct timeSheet{
float hours; //hours worked
float grossPay; // gross pay
float netPay; // net pay
float taxAmount; // tax amount deduction
};
//*********************************************************************************************
// function prototype
//*********************************************************************************************
void addEmployee(employeeRecord *employee, int &index);
void addTime(timeSheet *time, employeeRecord *employee, int &index);
void calcTime(timeSheet *time, employeeRecord *employee, int &index, float taxRate);
void outTime(timeSheet *time, employeeRecord *employee, int &index);
//*********************************************************************************************
// main function
//*********************************************************************************************
int main(){
float taxRate = .15; // taxrate on pay
char choice; // holds user choice option
int index = 0; // index of array
// create struct arrays to hold data
employeeRecord employee[2];
timeSheet time[2];
// menu title
cout << "\nArmadillo Automotive Group Payroll\n" << endl;
//*********************************************************************************************
// do loop to cycle through menu options & validate input
//*********************************************************************************************
do{
//menu
cout << "\nMenu Options: \n";
cout << "A - Add Employee" << endl;
cout << "B - Add Employee's Hours" << endl;
cout << "C - Print Payroll Report" << endl;
//take user menu choice
cout << "\nEnter Menu Option: ";
cin >> choice;
choice = tolower(choice);
//validate user selection
while (choice != 'a' && choice != 'b' && choice!= 'c'){
cout << "Please enter 'A' 'B' or 'C' : ";
cin >> choice;
}
// Process menu selection choice
if (choice != 'c'){
if (choice == 'a'){
//process new employee
if (index < 2){
addEmployee(employee, index);
}
else{
cout << "\nEmployee index full" << endl;
}
}
// if choice 'b' add employee work hours
else{
addTime(time, employee, index);
}
}
}while (choice != 'c');
// calculates and stores employees time sheet information in struct array
calcTime(time, employee, index, taxRate);
//display pay check outputs
outTime(time, employee, index);
return 0;
}
//*********************************************************************************************
// function to add employee to index
//*********************************************************************************************
void addEmployee(employeeRecord *employee, int &index){
//take employees ID #
cout << "\nEnter employee's ID number: ";
cin >> employee[index].IDnum;
//validate employee id # is greater than 0
while (employee[index].IDnum < 0){
cout << "\nPlease enter an ID # above 0 : ";
cin >> employee[index].IDnum;
}
//take employees name
cout << "\nEnter employee's Name: ";
cin.ignore();
getline(cin, employee[index].name);
//validate line has characters only
//**********************
//**********************
//**********************
//take employees payrate
cout << "\nEnter employee's pay rate: $";
cin >> employee[index].payRate;
//validate payrate is amount above 0
while(employee[index].payRate < 0){
cout << "\nPlease enter a value greater than 0: ";
cin >> employee[index].payRate;
}
// take employees emptype
cout << "\nEnter Employee type \n";
cout << "0 for union, 1 for management: ";
cin >> employee[index].empType;
//validate 0 or 1 was entered
while(employee[index].empType != 0 && employee[index].empType != 1){
cout << "Enter 1 or 0: ";
cin >> employee[index].empType;
}
// increment index for future entry
index++;
}
//*********************************************************************************************
// function to add time to an employee
//*********************************************************************************************
void addTime(timeSheet *time, employeeRecord *employee, int &index){
//set index to zero to start with first employee on list
index = 0;
//main instruction
cout << "Enter timecard information for each employee: " << endl;
//enter hours for each employee for loop
for(int i=0; i < 2; i++){
cout << "Hours worked for " << employee[index].name << ": ";
cin >> time[index].hours;
index++;}
}
//*********************************************************************************************
// function to to calculate timesheet
//*********************************************************************************************
void calcTime(timeSheet *time, employeeRecord *employee, int &index, float taxRate){
index = 0; // set index back to zero to start at first array index
float tempHours; //temp hour hold
float overTime; // overTime hours
float hours = time[index].hours; //hours worked
float grossPay = time[index].grossPay; //employes's gross pay
float netPay = time[index].netPay; //employes's net pay
float taxAmount = time[index].taxAmount; ////employes's tax deduction
int empType = employee[index].empType; // employee type
float payRate = employee[index].payRate; // employes pay rate
for (int i=0; i < 2; i++){
if (empType == 0){
if(hours > 40){
tempHours = 40;
overTime = hours - 40;
grossPay = (overTime * (payRate * 1.5)) + (tempHours * payRate);
taxAmount = grossPay * taxRate;
netPay = grossPay - taxAmount;
}
else{
grossPay = hours * payRate;
taxAmount = grossPay * taxRate;
netPay = grossPay - taxAmount;
}
}
else{
grossPay = hours * payRate;
taxAmount = grossPay * taxRate;
netPay = grossPay - taxAmount;
}
// increase index number
index++;
}
}
//*********************************************************************************************
// function to to print out timesheet
//*********************************************************************************************
void outTime(timeSheet *time, employeeRecord *employee, int &index){
index = 0; // set index back to zero to start at first array index
int empID = employee[index].IDnum; //employes id number
string name = employee[index].name; // employees name
float grossPay = time[index].grossPay; //employes's gross pay
float taxAmount = time[index].taxAmount; ////employes's tax deduction
float netPay = time[index].netPay; //employes's net pay
cout << "Pay Roll Report\n" << endl;
cout << setw(10) << left << "ID" << setw(30) << left << "Name";
cout << setw(10) << right << "Gross Pay" << setw(10) << right << "Tax";
cout << setw(10) << right << "Net Pay" << endl;
// for loop to display employee information.'
for (int i=0; i < 2; i++){
cout << setw(10) << left << empID << setw(30) << left << name;
cout << setw(10) << right << grossPay << setw(10) << right << taxAmount;
cout << setw(10) << right << netPay << endl;
index++;}
}
Him and thank you for the help in advance. I was just doing one of my assignments for school and I stumbled upon a problem. It's a simple bill calculator that differentiates you between a premium user and a regular one and calculates your bill based on the different rates. I thought I had finished it but now I'm getting this error. Please any help will do, just want to get this all said and done with.
Debug Error!
Program : ...\ConsoleApplication12.exe
Module : ...\ConsoleApplication12.exe
Run-Time Check Failure #3 - T
#include <iostream>
#include<iomanip>
using namespace std;
int main() {
const float Regular = 10;
const float Premium = 25;
const float RRate = .2;
const float DRate = .1;
const float NRate = .05;
int account;
int Rminutes;
int Dminutes;
int Nminutes;
int totalmin;
float Dcharge;
float Ncharge;
float total;
char service;
cout << "Enter the account number\n";
cin >> account;
cout << "Enter the service code\n";
cin >> service;
switch(service)
{case'r':
case'R':
cout << "Please enter the total amount of minutes used\n";
cin >> Rminutes;
if (Rminutes > 50) {
total = (Rminutes - 50)*RRate + Regular;
}
else {
total = Regular;
}
break;
case'P':
case'p':
cout << "Please enter how many minutes were used during the day\n";
cin >> Dminutes;
cout << "Please enter how many minutes were used during the night\n";
cin >> Nminutes;
if (Dminutes > 75) {
Dcharge = (Dminutes - 75)*DRate;
}
if (Nminutes > 100) {
Ncharge = (Nminutes - 100)*NRate;
}
total = Premium + Dcharge + Ncharge;
break;
default:
cout << "Invalid service code\n";
return 1;
break;
}
totalmin = Rminutes + Dminutes + Nminutes;
cout << "Your account number is:" << account;
cout << "Your type of service is:" << service;
cout << "your total minutes used was" << totalmin;
cout << "your bill is" << total;
return 0;
}
Try to define initialization values for all your variables. This code worked on Visual Studio too:
#include <iostream>
#include<iomanip>
using namespace std;
int main() {
const float Regular = 10;
const float Premium = 25;
const float RRate = .2;
const float DRate = .1;
const float NRate = .05;
int account=0;
int Rminutes=0;
int Dminutes=0;
int Nminutes=0;
int totalmin=0;
float Dcharge=0;
float Ncharge=0;
float total=0;
char service=0;
cout << "Enter the account number\n";
cin >> account;
cout << "Enter the service code\n";
cin >> service;
switch (service)
{
case'r':
case'R':
cout << "Please enter the total amount of minutes used\n";
cin >> Rminutes;
if (Rminutes > 50) {
total = (Rminutes - 50)*RRate + Regular;
}
else {
total = Regular;
}
break;
case'P':
case'p':
cout << "Please enter how many minutes were used during the day\n";
cin >> Dminutes;
cout << "Please enter how many minutes were used during the night\n";
cin >> Nminutes;
if (Dminutes > 75) {
Dcharge = (Dminutes - 75)*DRate;
}
if (Nminutes > 100) {
Ncharge = (Nminutes - 100)*NRate;
}
total = Premium + Dcharge + Ncharge;
break;
default:
cout << "Invalid service code\n";
return 1;
break;
}
totalmin = Rminutes + Dminutes + Nminutes;
cout << "Your account number is: " << account << "\n";
cout << "Your type of service is: " << service << "\n";
cout << "your total minutes used was " << totalmin << "\n";
cout << "your bill is " << total;
system("PAUSE");
return 0;
}
The code is to calculate the monthly payment of a car. Although I know I am not finished, I wish to see why I am not passing functions correctly.
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
void instructions()
{
cout << "We will calculate the monthly payment for your particular car." << endl;
cout << "Please Follow the instructions." << endl;
}
string getCarType()
{
string carType;
cout << "Please enter the type of your car: " << endl;
cin >> carType;
return carType;
}
double getPurchasePrice()
{
double purchasePrice;
cout << "Please enter the price in which you purchased the vehicle: " << endl;
cin >> purchasePrice;
return purchasePrice;
}
double getDownPayment()
{
double downPayment;
cout << "Please enter the down payment made on the vehicle: " << endl;
cin >> downPayment;
return downPayment;
}
int getYears()
{
int years;
cout << "Please enter the number of years remaining to pay off the loan: " << endl;
cin >> years;
return years;
}
double getRate()
{
double rate;
double correctedRate;
cout << "Please enter the annual interest rate of the loan as a whole number: " << endl;
cin >> rate;
//calculations
correctedRate = (rate/100);
return correctedRate;
}
double findAmountFinanced(double &purchasePrice, double &downPayment)
{
double amountFinanced;
//calculations
amountFinanced = purchasePrice - downPayment;
return amountFinanced;
}
double findMonthlyPayment(double &amountFinanced, double &rate, int &years)
{
double monthlyPayment;
double sideOne;
double sideTwo;
//calculations
sideOne = amountFinanced * (rate/12);
sideTwo = pow(1 - (1 + (rate/12)) / (-12*years));
monthlyPayment = sideOne/sideTwo;
return monthlyPayment;
}
int findNumberOfPayments(int &years)
{
int payments;
payments = 12 * years;
return payments;
}
int main()
{
instructions();
string carType;
double purchasePrice;
double downPayment;
int years;
double rate;
double amountFinanced;
double monthlyPayment;
int payments;
carType = getCarType();
purchasePrice = getPurchasePrice();
downPayment = getDownPayment();
years = getYears();
rate = getRate();
monthlyPayment = findMonthlyPayment();
payments = findNumberOfPayments();
cout << "Make of car: " << carType << endl;
cout << "Price Purchased at: " << purchasePrice << endl;
cout << "Down payment made at purchase: " << downPayment << endl;
cout << "Years to pay off loan: " << years << endl;
cout << "Annual rate of interest: " << rate << endl;
cout << "Your monthly payment is: " << monthlyPayment << endl;
cout << "The total amount of payments is: " << payments << endl;
return 0;
}
Again, my error is that I have too few arguments to function.
In some of the functions like findMonthlyPayment you don't pass an argument from the main, whereas these functions expect arguments. You error is self-explanatory you should have debugged it yourself.
If you look at your method definitions for findAmountFinanced, findMonthlyPayment and findNumberOfPayments, they take arguments when they are called. In your main() function where you calling them, you are not passing any arguments. Hence, too few arguments :)
FYI, a trick to troubleshooting the issue is to look at the complete stack trace of the error message and work your way down through line numbers.
Yes, when you call a certain method and that method have X numbers of arguments or parameters, whenever you call that function in your program you need to call it with exactly the same number of arguments and type of arguments.
Write a program a program that would compute the net salary of employees. The program should prompt the user to input number of hours worked and the hourly wage. Employees pay 17.5% of their gross salary as social security contribution. All employees pay 20% tax on their gross salary .Employees with gross salary less than 2500 pay 10% tax on their gross salary. Your program should use the following functions;
These are the instructions for the task. I have no problem writing the code, but I have trouble utilizing the grosspay variable in the necessary functions. Would really appreciate some help in modifying my code.
#include <iostream>
using namespace std;
double computeGross(double, double);
double computeDeductions(double);
void computeNet();
void validateHours();
void validateWage();
int main()
{
double hours, wage;
cout << "Please input the hourly wage of the employee. " << endl;
cin >> wage;
cout << "Please input the number of hours the employee worked." << endl;
cin >> hours;
computeGross(hours, wage);
computeDeductions(grosspay);
computeNet();
validateHours();
validateWage();
return 0;
}
double computeGross(double hours, double wage)
{
double grosspay = hours*wage;
cout << "The employee's gross pay is: " << " " << grosspay << endl;
}
double computeDeductions(double grosspay)
{
double total, sstotal, taxtotal;
double socialsecurity = .175;
double tax = .2;
double reducedtax = .1;
sstotal = grosspay*socialsecurity;
if (grosspay < 2500)
{
taxtotal = grosspay*reducedtax;
total = sstotal + taxtotal;
cout << "The total amount of deductions is: " << total << endl;
}
else
{
taxtotal = grosspay*tax;
total = sstotal + taxtotal;
cout << "The total amount of deductions is: " << total << endl;
}
}
void computeNet()
{
double netsalary;
netsalary = grosspay - deductions;
cout << "The net salary is: " << netsalary << endl;
cout << "The gross salary is: " << grosspay << endl;
cout << "The deductions total: " << deductions << endl;
}
void validateHours()
{
if (hours > 150 || hours < 0)
{
cout << "Invalid number of hours." << endl;
}
}
void validateWage()
{
if (wage > 200 || wage < 0)
{
cout << "Invalid wage." << endl;
return ;
}
}