C++ Mortgage payment calculator formula - c++

I've been working on a mortgage calculator for my C++ class. However, I am stuck.
I got the following formula off of Nerdwallet and tried to implement it in my program:
M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
M=mortgage payment
P=Principal
i=interest
n=number of payments
Here's the code that I am currently using.
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(int argc, char** argv)
{
int years, homePrice, creditScore, totalPayments;
float rate, mortgageTotal, monthlyPayment;
cout << "What is the price of the home that you are looking to mortgage?\n";
cin >> homePrice; //Assigns the variable homePrice to a value
cout << "What is your credit score?\n";
cin >> creditScore; //Assigns the creditScore variable a value
cout << "Would you prefer a 15 year or 30 year mortgage? Please type 15 or 30.\n";
cin >> years;
if ( years = 15 ) //If input is 15 year it will go down this logical path
{
if (creditScore >=760) //If their credit rating is equal to or more than 760, their rate will be .043 also nested if.
{
rate = .043;
cout << "Your interest rate is 4.3%\n";
}
else if (creditScore >= 700) //If their credit rating is equal to or more than 700, their rate will be .0455
{
rate = .0455;
cout << "Your interest rate is 4.55%\n";
}
else if (creditScore >= 660) //If their credit rating is equal to or more than 660, their rate will be .048
{
rate = .048;
cout << "Your interest rate is 4.8%\n";
}
else if (creditScore >= 620) //If their credit rating is equal to or more than 620, their rate will be .058
{
rate = .058;
cout << "Your interest rate is 5.8%\n";
}
else if (creditScore >= 580) //If their credit rating is equal to or more than 580, their rate will be .0655
{
rate = .0655;
cout << "Your interest rate is 6.55%\n";
}
else if (creditScore >= 500) //If their credit rating is equal to or more than 500, their rate will be .083
{
rate = .083;
cout << "Your interest rate is 8.3%\n";
}
}
else if ( years=30 )
{
if (creditScore >= 760)
{
rate=.043;
cout <<"Your interest rate is 4.3%\n";
}
else if (creditScore >= 700)
{
rate=.0455;
cout << "Your interest rate is 4.55%\n";
}
else if (creditScore >= 660)
{
rate=.048;
cout << "Your interest rate is 4.8%\n";
}
else if (creditScore >= 620)
{
rate=.058;
cout << "Your interest rate is 5.8%\n";
}
else if (creditScore >= 580)
{
rate=.0655;
cout << "Your interest rate is 6.55%\n";
}
else if (creditScore >= 500)
{
rate=.083;
cout << "Your interest rate is 8.3%\n";
}
}
totalPayments = years * 12;
monthlyPayment = homePrice * [[rate * (1 + rate)pow(totalPayments)] / [(1 + rate)pow(totalPayments) - 1]];
mortgageTotal = monthlyPayment * totalPayments;
cout << "Your mortgage will cost approximately " << mortgageTotal << " and your monthly payment will be " << monthlyPayment << endl;
return 0;
}
However, when I go to compile it, I get the following errors:
Errors
I just don't understand the errors and why they are there.
If someone could help me, I'd greatly appreciate it.
Thank you.

While your math formula uses both []s and ()s for grouping expressions, only ()s can be used in such a way in C++.
pow is a function call, not an in-place operator like you seem to be using it. It needs to look like pow(1 + rate, totalPayments).
Your ifs also are doing assignments (=) instead of comparisons (==). As it is, your code will only follow the first if because it is setting years to 15.

Your mortgage formula seems wrong. Try this
monthlyPayment = homePrice * ((rate * pow(1 + rate, totalPayments)) /
(pow(1.00 + rate, totalPayments) - 1));

Related

Don't understand why output is incorrect

I'm currently doing a Zybooks lesson for my C++ class and we're going over while loops. In this question, it wants me to calculate how many years it takes for a bank account to double it's initial balance. There is also an annual contribution added. My code is as follows:
#include <iostream>
using namespace std;
int main()
{
const double RATE = 5;
const double INITIAL_BALANCE = 10000;
const double TARGET = 2 * INITIAL_BALANCE;
cout << "Annual contribution: " << endl;
double contribution;
cin >> contribution;
double balance = INITIAL_BALANCE;
int year = 0;
while (balance < TARGET)
{
year++;
double interest = balance * RATE / 100;
balance = balance + interest + contribution;
}
cout << "Year: " << year << endl;
cout << "Balance: " << balance << endl;
return 0;
}
I used this as an answer but was met with this unexpected result:
Output differs. See highlights below.
Input
100
Your output
Annual contribution:
Year: 13
Balance: 20627.8
Expected output
Annual contribution:
Year: 13
Balance: 20527.8
I see the expected output and your output differs by a 100, i.e. your contribution. Maybe the evaluation system doesn't add annual contribution once your target is reached. The below code gets your required output, but I think your code should have been the correct answer.
#include <iostream>
using namespace std;
int main()
{
const double RATE = 5;
const double INITIAL_BALANCE = 10000;
const double TARGET = 2 * INITIAL_BALANCE;
cout << "Annual contribution: " << endl;
double contribution;
cin >> contribution;
double balance = INITIAL_BALANCE;
int year = 0;
while (balance < TARGET)
{
year++;
double interest = balance * RATE / 100;
balance = balance + interest;
if (balance < TARGET) {
balance += contribution;
}
}
cout << "Year: " << year << endl;
cout << "Balance: " << balance << endl;
return 0;
}
The problem is that you're making a contribution after the 13th year even though the target has been reached.
I would restructure to something like this, in order to only check the condition once:
while (true)
{
year++;
double interest = balance * RATE / 100;
balance += interest;
if (balance >= TARGET)
{
break;
}
balance += contribution;
}
This is because the last time the loop is being executed (at year=13), balance is less than TARGET but, after adding a contribution and interest into it, it jumps out of the loop with contribution and interest added to it.
So, the solution can be to use an if statement within the while loop to check if it exceeds the TARGET; if it does, then don't add contribution into it.
Replace your while loop with the following:
while (balance < TARGET)
{
year++;
double interest = balance * RATE / 100;
balance = balance + interest;
if (balance < TARGET) {
balance += contribution;
}
}

Creating a tax computing program on c++ , error with uninitialized local variables

First time posting here. Recently started a C++ class and I'm really enjoying it! I'm creating a tax computing program but ran into a bit of wall on this part. I don't know how its not initialized if I already stated it using 'double'
The variable that I'm having trouble with is "taxes2" on line 129
Please help if you can!
Also, this is the prompt im working with:
"Write a program to compute income tax as follows:
First, read the income for the year. Then, display the following menu and ask the user to select his or her filing status:
Single
Married filing separately
Married filing jointly
Head of household
For Single status, apply a deduction of $2000, for #2 $1700, for #3, $2300 and Head of household $2700. Also, ask for and read the number of dependents. Add to the deduction amount, $150 for each dependent up to a maximum of 3 dependents if single, add $125 for each dependent up to a maximum of 4 for Married filing separately, and $175 for each dependent, up to a maximum of 5, if married filing jointly or head of household.
Then, compute the taxable income by subtracting the total deduction computed above from his or her income. Anyone with a taxable income of less than $8,000 pays no taxes. For the rest, the first $15,000 of the income has a tax rate of 10%, the next $25,000 gets taxed at 15%, the next $30,000 at 20%, the next $30,000 at 25% and anything above $100,000 at 30%. So, for example, someone who had taxable income of $125,000, her first $15,000 gets taxed at 10% ($1,500), plus 15% of $25,000 ($3,750), plus 20% of $30,000 ($6,000), plus 25% of $30,000 ($7,500) and the remaining $25,000 at 30% ($7,500) for a total tax amount of $26,250. For someone with income of $13,000, the tax is 10% of that or $1,300. For someone who made $7,999.99 it would be 0, but for someone who made $8,000, it's $800. For someone who made $30,000, it would be 10% of $15,000 or $1,500 plus 15% of the remaining $15,000 or $2,250 making it $3,750, and so on. Display the amount of taxes to be paid."
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
const double tax1 = 1500.00;
const double tax2 = 3750.00;
const double tax3 = 6000.00;
const double tax4 = 7500.00;
double income;
double istatus;
double itaxes;
double itaxes2;
double ideduction1;
double ideduction2;
double idependant;
const double idependantrate1 = 150;
const double idependantrate2 = 125;
const double idependantrate3 = 175;
double idependanttotal;
cout << "What is your income? " << endl;
cin >> income;
cout << "Please enter your filing status: \n";
cout << "1. Single \n";
cout << "2. Married Filing Separately \n";
cout << "3. Married Filing Jointly \n";
cout << "4. Head of Household \n";
cin >> istatus;
// Begin Deductions
if (istatus == 1)
{
cout << "You've selected Single";
ideduction1 = 2000;
cout << "Please select number of dependants (max: 3): ";
cin >> idependant;
if (idependant >= 0)
{
idependant * idependantrate1 == idependanttotal;
}
idependanttotal + ideduction1 == ideduction2;
}
else if (istatus == 2)
{
cout << "You've selected Married Filing Separately";
ideduction1 = 1700;
cout << "Please select number of dependants (max: 4): ";
cin >> idependant;
if (idependant >= 0)
{
idependant * idependantrate2 == idependanttotal;
}
idependanttotal + ideduction1 == ideduction2;
}
else if (istatus == 3)
{
cout << "You've selected Married Jointly";
ideduction1 = 2300;
cout << "Please select number of dependants (max: 5): ";
cin >> idependant;
if (idependant >= 0)
{
idependant * idependantrate3 == idependanttotal;
}
idependanttotal + ideduction1 == ideduction2;
}
else if (istatus == 4)
{
cout << "You've selected Head of Household";
ideduction1 = 2700;
cout << "Please select number of dependants (max: 5): ";
cin >> idependant;
if (idependant >= 0)
{
idependant * idependantrate3 == idependanttotal;
}
idependanttotal + ideduction1 == ideduction2;
}
// Calculate Income Taxes
if (income <= 7999.99)
{
cout << "No taxes need to be paid. " << endl;
return 0;
}
else if (income >= 8000.00 && income <= 15000.00)
{
itaxes = income * .10; //calculates tax amount
itaxes - ideduction2 == itaxes2;
cout << "The amount owed in taxes is: $" << itaxes2 << endl;
}
else if (income >= 15000.01 && income <= 40000.00)
{
itaxes = ((income - 15000) * .15) + tax1; //calculates tax amount
itaxes - ideduction2 == itaxes2;
cout << "The amount owed in taxes is: $" << itaxes2 << endl;
}
else if (income >= 40000.01 && income <= 70000.00)
{
itaxes = ((income - 40000) * .20) + tax1 + tax2; //calculates tax amount
itaxes - ideduction2 == itaxes2;
cout << "The amount owed in taxes is: $" << itaxes2 << endl;
}
else if (income >= 70000.01 && income <= 100000.00)
{
itaxes = ((income - 70000) * .25) + tax1 + tax2 + tax3; //calculates tax amount
itaxes - ideduction2 == itaxes2;
cout << "The amount owed in taxes is: $" << itaxes2 << endl;
}
else if (income > 100000.01)
{
itaxes = ((income - 100000) * .30) + tax1 + tax2 + tax3 + tax4; //calculates tax amount
itaxes - ideduction2 == itaxes2;
cout << "The amount owed in taxes is: $" << itaxes2 << endl;
}
else
cout << "You have entered an unuseable number. Please restart the program and enter a positive number " << endl;
return 0;
idependant * idependantrate1 == idependanttotal will calculate the value of idependant * idependantrate1, and then check if it equals idependanttotal. If you want to store the calculated value in idependanttotal write idependanttotal = idependant * idependantrate1;.

C++ Output error

I have a problem with my program and I would appreciate help.
It's supposed to allow you to enter an account number, a service code, and the number of minutes the service was used. The program then calculates the bill and it varies depending on your service. When I execute the program, it doesn't allow you to enter anything.
Regular service:$10.00 plus first 50 minutes free. Charges for over 50 minutes are $0.20 per minute.
Premium service:
$25.00 plus:
a)
For calls made from 6:00 am to 6:00 pm, the first 75 minutes are free; charges for over
75 minutes are $0.10 per minute.
b)For calls made from 6:00 pm to 6:00 am, the first 100 minutes are free; charges for over 100 minutes are $0.05 per minute.
Here is the program that I've typed.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int minutes = 0,
day_minutes = 0, //minutes used during the day
night_minutes = 0; //minutes used during the night
string service_code,
account_number;
double final_amount,
final_damount, //final amount for day minutes
final_namount = 0; //final amount for night minutes
cout << "Please enter your account number: ";
cin >> account_number;
cout << "Please enter your service code (r or R for regular service and p or P for premium service): ";
cin >> service_code;
if (service_code == "r")
{
cout << "Please enter the amount of minutes used: " << minutes << endl;
}
if (minutes <= 50)
{
final_amount = 10;
cout << "Your final amount is $: " << final_amount << endl;
}
if (minutes > 50)
{
final_amount = (minutes * 0.20) + 10;
cout << "Your final amount is $: " << final_amount << endl;
}
else if (service_code == "p")
{
cout << "Please enter the amount of minutes used during the day: " << day_minutes << endl;
cout << "Please enter the amount of minutes used during the night: " << night_minutes << endl;
}
if (day_minutes <=75)
{
final_damount = 0;
final_amount = final_damount + final_namount + 20;
}
if (day_minutes > 75)
{
final_damount = day_minutes * 0.10;
final_amount = final_damount + final_namount + 20;
}
if (night_minutes <= 100)
{
final_namount = 0;
final_amount = final_damount + final_namount + 20;
}
if (night_minutes > 100)
{
final_namount = night_minutes * 0.05;
final_amount = final_damount + final_namount + 20;
cout << "Your final amount is: $ " << final_amount << endl;
}
else
cout << "Error, this program does not accept negative numbers.\n";
return 0;
}
Does anyone the problem to my program? Thank you.
In your code you never specify to ask for user input (read from any input streams or files), hence it doesn't ask for any input.
You will probably want to somewhere do something such as cin or cin.readline or any of several other various methods to read the user's input from stdin.
To avoid duplicating other questions I'm not putting further details here.

Illegal else without matching if [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm getting this error message that says illegal else without matching if. I think something is wrong with my else statement, but I don't see where. Do I have an unneeded bracket? It's on line 78 column 2. Thank you
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
string service_code, //hold the service code
account_number; //hold the account number
double final_amount = 0, // hold the final amount
final_damount, //hold the amount for the day minutes
minutes = 0, //hold the amount for minutes
day_minutes = 0, // hold the amount for fay minutes
night_minutes = 0, //hold the amount for night minutes
final_namount = 0; //hold the amount for night minutes
cout << "Please enter your account number: ";
cin >> account_number; //Entering the account number
cout << "Please enter your service code (r or R for regular service and p or P for premium service): ";
cin >> service_code; //Enteringthe service code
cout << "Please enter the amount of minutes used: ";
cin >> minutes; //Entering the minutes you used
if(service_code == "r" || "R")
{
if(minutes <= 50)
final_amount = 10;
cout << "Your final amount is $: " << final_amount << endl; //Displaying final amount when your minutes are less than or equal to 50
}
{
if(minutes > 50)
final_amount = (minutes - 50) * 0.20 + 10;
cout << "Your final amount is: $ " << final_amount << endl; //Displaying final amount when your minutes are greater than 50
}
{
else if(service_code == "p" || "P")
cout << "Please enter the amount of minutes used during the day: " << day_minutes << endl; //Entering minutes used during the day
cout << "Please enter the amount of minutes used during the night: " << night_minutes << endl; //Entering minutes used during the night
}
{
if(day_minutes <=75)
final_damount = 0;
final_amount = final_damount + final_namount + 20; //Calculating final amount for minutes used during the day
}
{
if(day_minutes > 75)
final_damount = day_minutes * 0.10;
final_amount = final_damount + final_namount + 20; //Calcuating final amount for minutes used during the day
}
{
if(night_minutes <= 100)
final_namount = 0;
final_amount = final_damount + final_namount + 20; //Calcuating final amount for minutes used during the night
}
{
if(night_minutes > 100)
final_namount = night_minutes * 0.05;
final_amount = final_damount + final_namount + 20; //Calcuating final amount for minutes used during the night
cout << "Your final amount is: $ " << final_amount << endl; //Displaying final amount
}
{
else
cout << "Error, this program does not accept negative numbers.\n"; //Displaying error message
cout << "Account number: " << account_number << endl; //Displaying account number
cout << "Service code: " << service_code << endl; //Displaying service code
cout << "Service code: " << minutes << endl; //Displaying minutes
}
return 0;
}
Most of your if statements are missing their opening curly-braces. This means that the completely wrong code is actually being executed by your program because only the first statement after each if statement will be executed if the conditional expression is true and the rest of the code will always be executed.
...this is the same issue as Apple's infamous goto error bug: http://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/
C is not Python, indentation is not respected by the compiler and it does not mean code belongs to a certain keyword or block.
I strongly suggest reading up on C's syntax, and it's (generally) a good idea to always use curly-braces with all statement blocks (if, for, do, while, etc) to help avoid nasties like these. Internally, my team at Microsoft, runs a program called StyleCop which won't let us check-in code to our central repository if it contains any brace-less statements.

Salary calculator (over time pay) require assistance of senior level programmers

4.16 (Salary Calculator) Develop a C++ program that uses a while statement to determine the
gross pay for each of several employees.
When someone works 41 hours or more. They get paid 1.5x more so my problem is that in my else if statement. I did rate * 1.5 which should translate into 10 * 1.5 = 15 but I get 425 because my code on the top probably, but I don't understand it at the moment.
He gave us this example. Which I'm trying to emulate.
"
Enter hours worked (-1 to end): 41
Enter hourly rate of the employee ($00.00): 10.00
Salary is $415.00
"
#include <iostream>
using namespace std;
int main()
{
double salary=0,rate=0,hours=0,counter=0;
cout << "Enter your hours worked: ";
cin >> hours;
cout << "Enter your rate: ";
cin >> rate;
while(counter<=hours)
{ salary=hours*rate;
counter++;
}
if(hours==-1)
{exit(0);
}
else if(hours>=41)
{
rate = rate * 1.5;
salary = salary + rate;
}
cout << "$" << salary;
return 0;
}
The loop you used has no functionality related to the problem, so I omitted it. Below is what will work. As others have said, define why you need a loop. I'm guessing you need to loop the code so the user can repeat this to their heart's content. If that is the case, I'll let you try to figure out how to break out of the loop when a user enters -1.
if (hours == -1)
exit(0);
else if (hours <= 40)
salary = rate * hours;
else {
double overtimeHours = hours - 40;
salary = rate * 40;
rate = rate * 1.5;
salary = salary + (rate * overtimeHours);
}
cout << "$" << salary;