I'm a student in a basic programming class and I'm trying to complete this program for a class assignment. It's a simple program that calculates compounded interest by the inputs of the user. However, when writing the code, I noticed that the the result is 0 even though based on the input I would expect otherwise. Could anyone tell me why the program isn't showing results?
#include <iostream>
#include <cmath>
using namespace std;
// Declarations of Prototype
void futureValue(double* presentValue, float* interestRate, int* months, double* value);
// List of variables
double presentValue = 0;
float interestRate = 0;
double value = 0;
int months = 0;
// Start of Main function
int main(void)
{
cout << "What is the current value of the account?";
cin >> presentValue;
cout << "How many months will Interest be added to the account?";
cin >> months;
cout << "And what will be the Interest Rate of the account?";
cin >> interestRate;
cout << "After " << months << " months, your account balence will be $" << value << ".";
return 0;
}
void futureValue()
{
if (presentValue <= 0)
{
cout << "I'm sorry, you must have a current balence of more than 0.00 dollars to calculate.";
return;
}
else
{
value = presentValue * pow(interestRate + 1, months);
return;
}
}
Yes. You are not calling the futureValue function which would compute the value for you. Due to the value not being computed, it remains 0. Fix:
#include <iostream>
#include <cmath>
using namespace std;
// Declarations of Prototype
void futureValue(double* presentValue, float* interestRate, int* months, double* value);
// List of variables
double presentValue = 0;
float interestRate = 0;
double value = 0;
int months = 0;
// Start of Main function
int main(void)
{
cout << "What is the current value of the account?";
cin >> presentValue;
cout << "How many months will Interest be added to the account?";
cin >> months;
cout << "And what will be the Interest Rate of the account?";
cin >> interestRate;
futureValue(); //Here we compute the value
cout << "After " << months << " months, your account balence will be $" << value << ".";
return 0;
}
void futureValue()
{
if (presentValue <= 0)
{
cout << "I'm sorry, you must have a current balence of more than 0.00 dollars to calculate.";
return;
}
else
{
value = presentValue * pow(interestRate + 1, months);
return;
}
}
User - Defined Functions
The cost to become a member of a fitness center is as follows:
The senior citizens discount is 30%.
If the membership is bought and paid for 12 or more months, the discount is 15%
If more than five personal training sessions are bought and paid for, the discount on each session is 20%.
Write a menu-driven program that determines the cost of a new membership. Your program must contain a function that displays the general information about the fitness center and its charges, a function to get all of the necessary information to determine the membership cost, and a function to determine the membership cost. Use appropriate parameters to pass information in and out of a function. (Do not use any global variables.)
My codes:
#include <iostream>
#include <iomanip>
using namespace std;
// program constants
void setPrices(double&, double&);
void getInfo(bool&, bool&, bool&, int&, int&);
double membershipCost(double, int, double, int, bool, bool, bool);
void displayCenterInfo();
int main()
{
bool seniorCitizen;
bool boughtFiveOrMoreSessions;
bool paidTwelveOrMoreMonths;
int numberOfMembershipMonths;
int numberOfPersonalTrainingSessions;
double regularMembershipChargesPerMonth;
double costOfOnePersonalTrainingSession;
double memberCost;
cout << fixed << showpoint << setprecision(2);
displayCenterInfo();
cout << endl;
setPrices(regularMembershipChargesPerMonth, costOfOnePersonalTrainingSession);
getInfo(seniorCitizen, boughtFiveOrMoreSessions, paidTwelveOrMoreMonths, numberOfMembershipMonths, numberOfPersonalTrainingSessions);
// cal getInfo
memberCost = membershipCost(regularMembershipChargesPerMonth, numberOfMembershipMonths, costOfOnePersonalTrainingSession,
numberOfPersonalTrainingSessions, seniorCitizen, boughtFiveOrMoreSessions, paidTwelveOrMoreMonths);
cout << "$" << memberCost;
system("pause");
return 0;
}
void displayCenterInfo()
{
cout << "Welcome to Stay Healty and Fit center." << endl;
cout << "This program determines the cost of a new membership." << endl;
cout << "If you are a senior citizen, then the discount is 30% of "
<< "of the regular membership price." << endl;
cout << "If you buy membership for twelve months and pay today, the "
<< "discount is 15%." << endl;
cout << "If you buy and pay for 6 or more personal training session today, "
<< "the discount on each session is 20%." << endl;
}
void setPrices(double& regMemPrice, double& personalTrSesCost)
{
cout << "Please enter the cost of regular Membership per month: " << endl;
cin >> regMemPrice;
cout << "Please enter the cost of one personal traning session: " << endl;
cin >> personalTrSesCost;
}
void getInfo(bool& senCitizen, bool& bFiveOrMoreSess, bool& paidTwMnth,
int& nOfMonths, int& nOfPersonalTrSess)
{
//Senior Verification
char userInputSenior;
cout << "Are you Senior? Please enter 'Y' or 'N': ";
cin >> userInputSenior;
if (userInputSenior == 'y' && userInputSenior == 'Y')
{
senCitizen = true;
}
else
senCitizen = false;
cout << endl;
//Number of personal training session.
cout << "Enter the number of personal training sessions bought: ";
cin >> nOfPersonalTrSess;
if (nOfPersonalTrSess >= 5)
{
bFiveOrMoreSess = true;
}
else
bFiveOrMoreSess = false;
cout << endl;
//Number of months
cout << "Enter the number of months you are paying for: ";
cin >> nOfMonths;
if (nOfMonths >= 12)
{
paidTwMnth = true;
}
else
paidTwMnth = false;
}
double membershipCost(double regMemPricePerMth, int nOfMonths,
double personalTrSesCost, int nOfPersonalTrSess,
bool senCitizen, bool bFiveOrMoreSess, bool paidTwMnth)
{
double finalMembershipCost, finalSessionCost;
//Session Discount
if (bFiveOrMoreSess)
{
personalTrSesCost = personalTrSesCost * 0.8;
}
else
{
personalTrSesCost = personalTrSesCost;
}
//Month Discount
if (paidTwMnth)
{
regMemPricePerMth = regMemPricePerMth * 0.85;
}
else
{
regMemPricePerMth = regMemPricePerMth;
}
finalMembershipCost = regMemPricePerMth * nOfMonths;
finalSessionCost = personalTrSesCost * nOfPersonalTrSess;
// Check if Senior Citizen Discount Applies
if (senCitizen) {
return (finalMembershipCost * 0.7) + finalSessionCost ;
}
else {
return finalMembershipCost + finalSessionCost;
}
}
My Test Result
An error occurs on "Senior Citizen Discount".
Green color - My output.
Red color - Its output (Correct Answer).
I don't know how to get that answer ($2260.00) with my code. I have checked many times and I couldn't solve the problem. Please help me!
You should use an or-Statement for detecting if its a senior citizen:
if (userInputSenior == 'y' || userInputSenior == 'Y')
BTW: You have another small bug when calculating the discount for personal lessons, you only get a discount for more than 5 sessions, so the corresponding if-statement should be
(nOfPersonalTrSess > 5)
Thank you guys so much, I solved my problem!
Here is my complete program:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// program constants
void setPrices(double&, double&);
void getInfo(bool&, bool&, bool&, int&, int&);
double membershipCost(double, int, double, int, bool, bool, bool);
void displayCenterInfo();
int main()
{
bool seniorCitizen;
bool boughtSixOrMoreSessions;
bool paidTwelveOrMoreMonths;
int numberOfMembershipMonths;
int numberOfPersonalTrainingSessions;
double regularMembershipChargesPerMonth;
double costOfOnePersonalTrainingSession;
double memberCost;
cout << fixed << showpoint << setprecision(2);
displayCenterInfo();
cout << endl;
setPrices(regularMembershipChargesPerMonth, costOfOnePersonalTrainingSession);
getInfo(seniorCitizen, boughtSixOrMoreSessions, paidTwelveOrMoreMonths, numberOfMembershipMonths, numberOfPersonalTrainingSessions);
// cal getInfo
memberCost = membershipCost(regularMembershipChargesPerMonth, numberOfMembershipMonths, costOfOnePersonalTrainingSession,
numberOfPersonalTrainingSessions, seniorCitizen, boughtSixOrMoreSessions, paidTwelveOrMoreMonths);
cout << "$" << memberCost;
system("pause");
return 0;
}
void displayCenterInfo()
{
cout << "Welcome to Stay Healty and Fit center." << endl;
cout << "This program determines the cost of a new membership." << endl;
cout << "If you are a senior citizen, then the discount is 30% of "
<< "of the regular membership price." << endl;
cout << "If you buy membership for twelve months and pay today, the "
<< "discount is 15%." << endl;
cout << "If you buy and pay for 6 or more personal training session today, "
<< "the discount on each session is 20%." << endl;
}
void setPrices(double& regMemPrice, double& personalTrSesCost)
{
cout << "Please enter the cost of regular Membership per month: " << endl;
cin >> regMemPrice;
cout << "Please enter the cost of one personal traning session: " << endl;
cin >> personalTrSesCost;
}
void getInfo(bool& senCitizen, bool& bSixOrMoreSess, bool& paidTwMnth,
int& nOfMonths, int& nOfPersonalTrSess)
{
//Senior Verification
char userInputSenior;
cout << "Are you Senior? Please enter 'Y' or 'N': ";
cin >> userInputSenior;
if (userInputSenior == 'y' || userInputSenior == 'Y')
{
senCitizen = true;
}
else
senCitizen = false;
cout << endl;
//Number of personal training session.
cout << "Enter the number of personal training sessions bought: ";
cin >> nOfPersonalTrSess;
if (nOfPersonalTrSess > 5)
{
bSixOrMoreSess = true;
}
else
bSixOrMoreSess = false;
cout << endl;
//Number of months
cout << "Enter the number of months you are paying for: ";
cin >> nOfMonths;
if (nOfMonths >= 12)
{
paidTwMnth = true;
}
else
paidTwMnth = false;
}
double membershipCost(double regMemPricePerMth, int nOfMonths,
double personalTrSesCost, int nOfPersonalTrSess,
bool senCitizen, bool bSixOrMoreSess, bool paidTwMnth)
{
double finalMembershipCost, finalSessionCost;
//Session Discount
if (bSixOrMoreSess)
{
personalTrSesCost = (personalTrSesCost * 0.8);
}
else
{
personalTrSesCost = personalTrSesCost;
}
//Month Discount
if (paidTwMnth)
{
regMemPricePerMth = regMemPricePerMth * 0.85;
}
else
{
regMemPricePerMth = regMemPricePerMth;
}
finalMembershipCost = regMemPricePerMth * nOfMonths;
finalSessionCost = personalTrSesCost * nOfPersonalTrSess;
// Check if Senior Citizen Discount Applies
if (senCitizen) {
return (finalMembershipCost * 0.7) + finalSessionCost;
}
else {
return finalMembershipCost + finalSessionCost;
}
}
so the program is going to calculate the total mile per hour during the trip. But however my total Mile per hour for a few trips is doubling instead of staying the same. Does anyone know why is that?
this is my head (.h) file
class Distance
{
public:
Distance() :
tripMiles(0.0), tripGallons(0.0),
totalMiles(0.0), totalGallons(0.0){}
void addTrip(double miles, double gallons)
{
tripMiles = miles;
tripGallons = gallons;
totalMiles += miles;
totalGallons += gallons;
}
double getTripMiles() const {return tripMiles;}
double getTripGallons() const {return tripGallons;}
double getMilesTotal() const {return totalMiles;}
double getGallonsTotal() const {return totalGallons;}
double getTripMPG() const {return tripMiles / tripGallons; }
double getTotalMPG() const {return totalMiles / totalGallons; }
private:
double tripMiles, tripGallons;
double totalMiles, totalGallons;
};
and this is my main file.
#include <iostream>
#include "Distance.h"
using namespace std;
int main()
{
Distance myDistance;
double miles= 0.0;
double gallons = 0.0;
cout << "Enter miles driven (-1 to quit): ";
cin >> miles;
myDistance.addTrip(miles,gallons);
while(miles != -1)
{
cout << "\nEnter Gallons used: ";
cin >> gallons;
myDistance.addTrip(miles,gallons);
cout << "\nMPG this trip: " << myDistance.getTripMPG();
cout << "\nTotal MPG is: " << myDistance.getTotalMPG();
cout << "\nEnter miles driven (-1 to quit): ";
cin >> miles;
}
}
You're calling myDistance.addTrip(miles,gallons); once before the loop starts, then again in the loop. This means you count the miles from the first trip twice. You should remove the first one.
I've been trying to figure this out for sometime and I think it has something to do with the values I'm using for the calculations. I'm not exactly familiar with compound interest so I'm not sure were I'm going wrong. Any help would be appreciated.
#include <iostream>
#include <cmath>
using namespace std;
double interest_credit_card(double initial_balance, double interest_rate, int payment_months);
// Calculates interest on a credit card account according to initial balance,
//interest rate, and number of payment months.
int main ()
{
double initial_balance, interest_rate;
int payment_months;
char answer;
do
{
cout << "Enter your initial balace: \n";
cin >> initial_balance;
cout << "For how many months will you be making payments?\n";
cin >> payment_months;
cout << "What is your interest rate (as a percent)?: %\n";
cin >> interest_rate;
cout << endl;
cout << "You will be paying: $ " << interest_credit_card( initial_balance, interest_rate, payment_months) << endl;
cout << "Would you like to try again? (Y/N)\n";
cin >> answer;
}while (answer == 'Y' || answer == 'y');
cout << "Good-Bye.\n";
return 0;
}
double interest_credit_card(double initial_balance, double interest_rate, int payment_months)
{
double compound_interest, compounding, compounding2, compounding3, compounding4;
while(payment_months > 0)
{
initial_balance = initial_balance + (initial_balance * interest_rate/100.0);
compounding = (interest_rate /12);
compounding2 = compounding + 1;
compounding3 = interest_rate * (payment_months/12);
compounding4 = pow(compounding2, compounding3);
compound_interest = initial_balance * compounding4;
initial_balance = initial_balance + compound_interest;
payment_months--;
}
return initial_balance;
}
Inputs and expected outputs:
Enter your initial balance: 1000
For how many months will you be making payments?: 7
What is your interest rate (as a percent)?: 9
You will be paying: $1053.70
It looks like you were trying a bunch of things and then left them in. The first solution you tried was almost right, you just forgot "/12":
double interest_credit_card(double initial_balance, double interest_rate, int payment_months)
{
while (payment_months > 0)
{
initial_balance = initial_balance + (initial_balance * interest_rate / 100.0/12);
payment_months--;
}
return initial_balance;
}
With a little better style:
double interest_credit_card(double initial_balance, double interest_rate, int payment_months)
{
double total_payment = initial_balance;
double monthly_rate = interest_rate / 100.0 / 12;
for (int month = 1; month <= payment_months; ++month)
total_payment += total_payment * monthly_rate;
return total_payment;
}
I am taking a class on C++ currently and need help with a certain part of my code. It all compiles but when I go to test it I am only allowed to input one value into the window before the rest of the code advances through without input.
For reference, this is the question I am answering:
11. Payroll
Design a PayRoll class that has data members for an employee’s hourly pay rate, number of hours worked of type double . The default constructor will set the hours worked and pay rate to zero. The class must have a mutator function to set the pay rate for each employee and hours worked. The class should include accessors for both the hours worked and the rate of pay. The class should lastly have a getGross function that will return a double calculated by multiplying the hours worked by the rate
of pay.
Write a program with an array of seven PayRoll objects . The program should ask the user for the rate of pay for each employee and the number of hours each employee has worked. Be sure to include an employee claiming to work more then 60 hours per week. Print out, the array number of the employee, the hours worked, the rate of pay, and the gross pay, of all the employee's each on their own line. Set the precision for printing the doubles to two decimal places.
Input Validation: Do not accept values greater than 60 for the number of hours worked, simply have the set function set number of hours worked to 60, the maximum allowed.
My code is as follows:
#include <iostream>
#include <iomanip>
//Garrett Bartholomay
/*This program defines and implements the Payroll class.
* The class is then used in a program that calculates gross pay for an array of
* Payroll objects after accepting values for hours and pay rate from standard input*/
class Payroll
{
private:
int hoursWorked;
double payRate;
public:
Payroll();
Payroll(int, double);
void setHours(int);
void setPayRate(double);
int getHours() const;
double getPayRate() const;
double getGross()const;
};
Payroll::Payroll()
{
hoursWorked = 0;
payRate = 0.0;
}
Payroll::Payroll(int h, double r)
{
payRate = r;
hoursWorked = h;
}
void Payroll::setHours(int h)
{
hoursWorked = h;
}
void Payroll::setPayRate(double p)
{
payRate = p;
}
int Payroll::getHours() const
{
return hoursWorked;
}
double Payroll::getPayRate() const
{
return payRate;
}
double Payroll::getGross() const
{
double gross = static_cast<double>(hoursWorked) * payRate;
return gross;
}
using namespace std;
int main()
{
const int NUM_EMPLOYEES = 7;
Payroll employee[NUM_EMPLOYEES];
int pay;
int hours;
int i;
double grossPay;
for (i = 0; i < NUM_EMPLOYEES; i++)
{
cout << "Enter the # " << (i) << " employee's rate of pay per hour: ";
cin >> pay;
cout << "Enter the # " << (i) << " employee's hours worked for the week: ";
cin >> hours;
employee[i].setPayRate(pay);
employee[i].setHours(hours);
}
cout << "\n\nHere is the gross pay for each employee:\n";
cout << fixed << showpoint << setprecision(2);
for (int i = 0; i < NUM_EMPLOYEES; i++)
{
grossPay = employee[i].getGross();
cout << "The gross pay for employee # " << (i) << " is: " << grossPay << endl;
}
return 0;
}
The input resides within the loops.
Any help would be greatly appreciated.
Thanks,
G