Effective January 1st of each year, Gabby recieves a 5% raise on her previous year's salary. She wants a program that calculates and displays the amount of her annual raises for the next three years. The program also should calculate and display her total salary for the three years.
I have to test the program and this is what i get but when i desk check it comes out wrong.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double RATE = .05;
double salary = 0.0;
double raise = 0.0;
double totalSalary = 0.0;
cout << "Enter the salary:";
cin >> salary;
for(int counter = 0; counter <=3; counter++)
{
cout <<salary<<endl;
raise = (salary * 0.05);
}
return 0;
} //end of main function
You're not adding the raise to the salary:
salary += raise;
This one is a little more accurate. It will calculate with decimals without rounding and post the raise for each year, then add how much the total of her raises will be over three years. Also, some notes are added so you know what was going on in the code.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//declare variables
double salary = 0.0;
double totalSalary = 0.0;
const double RATE = 0.05;
//input
cout << "Enter the salary: ";
cin >> salary;
cout << endl;
//loop for years and calculation
for(int numYears = 1; numYears <= 3; numYears ++)
{
cout << fixed;
cout << setprecision(2);
salary = salary*(1+RATE);
cout << "Year " << numYears;
cout << ": $" << salary << endl;
//end for
}
cout << "The total salary over three years is $" << totalSalary << endl;
cout << endl;
system("pause");
return 0;
}
First of all, you declare a constant called RATE (which should really be at the top of the program rather than in your main function) but don't bother to use this in your calculation. Instead you use the hard-coded value of 0.05.
Second, you're not adding the calculation to the salary variable. You can use salary += raise or salary *= (1.0 + RATE).
Third, you're not doing anything with the totalSalary variable at all.
Your code should look something like this:
#include <iostream>
using namespace std;
const double RATE = 0.05;
int main()
{
double salary = 0.0;
double totalSalary = 0.0;
cout << "Enter the salary:" << endl;
cin >> salary;
for(int counter = 0; counter <=3; counter++)
{
cout << salary << endl;
salary *= (1.0 + RATE);\
totalSalary += salary;
}
cout << "The total salary is " << totalSalary << endl;
return 0;
}
Related
This is a simple Pass By Reference Problem to calculate the salary after tax, when directly printing "income-income*tax" it's giving the expected output but when the value of variable is updated it's giving a wrong answer.
It is working fine in the online compiler.
What could be the reason of this?
// Using Reference Variable
#include<iostream>
using namespace std;
void incomeAfterTax(int &income)
{
float tax = 0.10;
cout << income-income*tax << endl; // printing 90
income = income - income*tax;
cout << income << endl; // but here it is 89
}
int main()
{
int salary = 100;
cout << "Total Salary: " << salary << endl;
incomeAfterTax(salary);
cout << "Income After Tax: " << salary; // prints 89
return 0;
}
This is probably a floating point issue; were the result of 'income-tax' is being converted to an integer.
The issue is avoidable by using the round() function in (for C++11). (roundf() if using C99)
#include<iostream>
#include <cstdint>
#include<cmath>
using namespace std;
void incomeAfterTax(int& income)
{
const float tax = 0.10;
cout << income-income*tax << endl;
const float tax_amt = static_cast<float>(income) * tax; // income is int, cast to float
income -= round(tax_amt); //round the float value to closest int
cout << income << endl;
}
int main()
{
int salary = 100;
cout << "Total Salary: " << salary << endl;
incomeAfterTax(salary);
cout << "Income After Tax: " << salary;
return 0;
}
The result of this question, it should have a payroll record consists all of these things.
But i have a problem in calculating the TOTAL GROSS PAY FOR ALL EMPLOYEES by using arrays in struct (C++) but I am stuck.
The total gross pay should be printed at bottom of the payroll record. I feel like something is missing in my coding but I can`t figure out what that thing is. I only have a problem in finding the total gross pay, others are okay.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double gross[10];
double sum = 0.0;
double totalGrossPay;
struct GrossPay {
int empID;
string empName;
double chargeHour;
int workingHours;
double grossPay;
};
GrossPay employee[10];
for (int i = 0; i < 10; i++) {
cout << (i + 1) << "."
<< "Employee Name :";
cin >> employee[i].empName;
cout << "Employee ID :";
cin >> employee[i].empID;
cout << "Employee`s charge rate per hour :";
cin >> employee[i].chargeHour;
cout << "Working hours :";
cin >> employee[i].workingHours;
cout << endl;
}
cout << "Employee ID\t"
<< "Employee Name\t"
<< "Gross Pay(RM)" << endl;
for (int i = 0; i < 10; i++) {
double gross = employee[i].chargeHour * employee[i].workingHours;
cout << employee[i].empID << "\t\t" << employee[i].empName << "\t\t" << gross;
cout << endl;
}
cout << endl;
for (int i = 0; i < 10; i++) {
totalGrossPay = sum + gross[i];
}
cout << "Total gross pay of 10 employees : RM" << totalGrossPay;
return 0;
}
You have an uninitialized array
double gross[10];
So its elements have indeterminate values.
As a result this loop
for (int i = 0; i < 10; i++) {
totalGrossPay = sum + gross[i];
}
invokes undefined behavior.
Also the variable sum has not changed in the preceding code. So its using in this for loop does not make a sense.
Maybe you mean in the body of the loop
double totalGrossPay = 0.0;
for (int i = 0; i < 10; i++) {
totalGrossPay += gross[i];
}
provided that the array gross is filled with values.
It seems that in this for loop
for (int i = 0; i < 10; i++) {
double gross = employee[i].chargeHour * employee[i].workingHours;
cout << employee[i].empID << "\t\t" << employee[i].empName << "\t\t" << gross;
cout << endl;
}
you mean elements of the array gross instead of the local variable gross as for example
for (int i = 0; i < 10; i++) {
gross[i] = employee[i].chargeHour * employee[i].workingHours;
cout << employee[i].empID << "\t\t" << employee[i].empName << "\t\t" << gross[i];
cout << endl;
}
Also the data member double grossPay; of the structure is not used. Maybe instead of the array gross you need to fill this data member of elements of the array of structures do not you?
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;
}
I'm fairly new to programming with C++ and I'm trying to understand where I'm missing the flow of the algorithm in this code. The code is supposed to take the cost of a set of tickets ($25, $30, $15). When I run the code it will add the total number of tickets bought, but not the total cost of all the tickets. I'm believe I've assigned the variables correctly but I don't know why it isn't adding together the total cost unless I need to assign those variables separately.
Any help would be appreciated, thanks!
using namespace std;
int main()
{
//declare variables
double orchestra = 25;
double mainFloor = 30;
double balcony = 15;
double totalSales = 0.0;
//enter input items
cout << "Enter orchestra tickets ";
cin >> orchestra;
cout << "Enter main floor tickets ";
cin >> mainFloor;
cout << "Enter balcony tickets ";
cin >> balcony;
//add the input items and print the total
totalSales = orchestra + mainFloor + balcony;
//display the total sales
cout << "Total Sales $" << totalSales << endl;
system("pause");
return 0;
} //end of main function
As pointed it out in another comment, there was no explanation.
You are assigning the cost to the same variable that you are using for input on the number of tickets (and consequently overwriting the cost). Instead, put the costs in separate variables (or constants if you prefer) and then do the math after getting the user input.
Try this:
using namespace std;
int main()
{
//declare variables
double cost_per_orchestra = 25;
double cost_per_mainFloor = 30;
double cost_per_balcony = 15;
double orchestra = 0;
double mainFloor = 0;
double balcony = 0;
double totalSales = 0.0;
//enter input items
cout << "Enter orchestra tickets ";
cin >> orchestra;
cout << "Enter main floor tickets ";
cin >> mainFloor;
cout << "Enter balcony tickets ";
cin >> balcony;
//add the input items and print the total
totalSales = cost_per_orchestra * orchestra + cost_per_mainFloor * mainFloor + cost_per_balcony * balcony;
//display the total sales
cout << "Total Sales $" << totalSales << endl;
system("pause");
return 0;
} //end of main function
You're overwriting your price values with your cin statements: Better create separate variables for the price and multiply them with your input.
#include <iostream>
using namespace std;
int main(){
//declare variables
const double orchestraPrice = 25;
const double mainFloorPrice = 30;
const double balconyPrice = 15;
double orchestra = 0;
double mainFloor = 0;
double balcony = 0;
double totalSales = 0.0;
//enter input items
cout << "Enter orchestra tickets ";
cin >> orchestra;
cout << "Enter main floor tickets ";
cin >> mainFloor;
cout << "Enter balcony tickets ";
cin >> balcony;
//add the input items and print the total
totalSales = orchestra * orchestraPrice + mainFloor * mainFloorPrice + balcony * balconyPrice;
//display the total sales
cout << "Total Sales $" << totalSales << endl;
system("pause");
return 0;
} //end of main function
Instead of static declarations per ticket value you can take it at runtime. Yes, as previous answerer mentioned new input value is overwriting to variable.
#include <iostream>
#define MAX 3
using namespace std;
typedef struct
{
double ticket_type_per_cost;
double total_no_of_tickets;
}ticket;
int main()
{
double totalSales=0;
ticket t;
int i;
for(i=0; i<MAX; i++)
{
cout<< "\nenter cost per ticket of type "<<i+1 <<": ";
cin>>t.ticket_type_per_cost;
cout<<"\nenter number of tickets: ";
cin>>t.total_no_of_tickets;
totalSales= totalSales + (t.ticket_type_per_cost * t.total_no_of_tickets);
}
//display the total sales
cout << "Total Sales $" << totalSales << endl;
system("pause");
return 0;
} //end of main function
I am getting an uninitialized Local variable error when I do believe I have already initialized. The error reads uninitialized local variable wk1 being used (It's wk1-wk5).
Here is the code:
#include <iostream>
using namespace std;
const double tax = 0.14;
int main()
{
int wk1,wk2,wk3,wk4,wk5;
wk1,wk2,wk3,wk4,wk5 = 0;
int thours = wk1 + wk2 + wk3 + wk4 + wk5; <------------ This is the error line.
thours = 0;
double payrate;
payrate = 0;
double gross = thours * payrate;
double taxes = tax * gross;
double net = gross - taxes;
double clothes = 0.10 * net;
double supplies = 0.10 * net;
double remaining = net - clothes - supplies;
double bonds = 0.25 * remaining;
double pbonds = 0.50 * bonds;
bonds = 0;
gross = 0;
net = 0;
clothes = 0;
supplies = 0;
remaining = 0;
cout << "Please enter the payrate for employee." << endl;
cin >> payrate;
payrate = 0;
cout << "Please enter employee's total hours for week one:" << endl;
cin >> wk1;
wk1 = 0;
cout << "Please enter employee's total hours for week two:" << endl;
cin >> wk2;
wk2 = 0;
cout << "Please enter employee's total hours for week three:" << endl;
cin >> wk3;
wk3 = 0;
cout << "Please enter employee's total hours for week four:" << endl;
cin >> wk4;
wk4 = 0;
cout << "Please enter employee's total hours for week five:" << endl;
cin >> wk5;
wk5 = 0;
cout << "Here is income before taxes: " << gross << endl;
cout << "Here is income after taxes: " << net << endl;
cout << "Here is clothes and accesories: " << clothes << endl;
cout << "Here is School supplies: " << supplies << endl;
cout << "Here is personal bonds: " << bonds << endl;
cout << "Here is parents bonds: " << pbonds << endl;
return 0;
}
wk1,wk2,wk3,wk4,wk5 = 0;
This line is a comma operator expression, which is equivalent to:
wk5 = 0;
Because expressions like wk1 has no side effect. Only the variable wk5 has been assigned a value, the other variables are still uninitialized. You can do:
wk1 = wk2 = wk3 = wk4 = wk5 = 0;