Hello NewGuy76,
If this is a class assignment it really helps to post the complete instructions that you were given. This way everyone has a better idea of what needs to me done.
It is always a good idea to post a complete program that can be compiled and run with at least enough code to demonstrate the problem. This way people do not have to guess what yo might have done. Also it helps to find problems you may not have seen yet.
Also any pointers on keeping code neat, clean and to the point would be appreciated.
|
Since you asked.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
constexpr double HUNDRED{ 100 };
double interestRate{}, interestAmt{}, totalCost{}, loanAmt{};
int monthlyPayment{}; // <--- Both lines should be "double"s.
int loanTerm{};
cout << "Please enter Loan Amount: "; // <--- Changed. Added a space after the (:).
cin >> loanAmt;
cout << "Please enter Interests Rate: ";
cin >> interestRate;
cout << "Please enter Loan Term: ";
cin >> loanTerm;
while (interestRate >= 1.0) // <--- Changed.
interestRate = interestRate / HUNDRED;
//interestRate /= HUNDRED;
interestAmt = loanAmt * interestRate;
totalCost = loanAmt + interestAmt;
monthlyPayment = loanTerm / totalCost;
cout << "Total cost:" << totalCost << '\n';
cout << "Monthly Payment:" << monthlyPayment << '\n';
}
|
Notice the use of blank lines to break up the program. It does help in readability, first for you and for others.
Lines 24 and 25 demonstrate the use of the constant variable I added. In your programs you should avoid using magic numbers. Not a problem in this short code, but later when you programs grow finding all those magic numbers to change will be a challenge to find and change all of them.
For your variables it would be easier to make all of them "double"s instead of mixing "int"s and "double"s in a calculation. As
salem c pointed out in line 31 you have a (int = int / double). On the rhs of = the int will be promoted to a "double" before the calculation, but the result is stored in an "int". The problem here is that the "int" only stores whole numbers, so the decimal part is dropped.
The drawback to this is if the rhs has a result of (0.125) only the (0)zero will be stored in the "int" and your output may not be what you want.
Your while loop should loop 1 time, but if it does not the result would not be what you want. Any how the while loop can be replaced with the commented line that follows.
The (/=) is the same as
interestRate = interestRate / 100.0;
. Just a shorter way of writing it. You do not have to use it if you are not familiar with it yet, but you should get use to it.
Your prompts and input are good. Although adding a space after the (:) keeps the input from everything running together. Makes for a nicer display on the screen.
When I compiled your program I did get this warning:
Severity Code Description Project File Line Suppression State
Warning C4244 '=': conversion from 'double' to 'int', possible loss of data Main V1 31
|
I took out the part that does not apply here. At the end is the file that th warning is in and the line number.
Changing all the variable to "double"s eliminated the warning.
A good habit to get into is to initialize all your variables when they are defined. What you did with
interestAmt{};
is all you need. This is making use of what the language allows. For the empty {}s the compiler will decide what type of (0)zero is needed based on the variables type. No extra work need. For the future a "std::string" does not need initialized because it is empty to start with.
With the C++11 standards and any updates prefer to use the (\n) over (endl) most of the time. These days the (\n) works much the same as the (endl) without the time needed to call the function. Also any "cout" statements followed by "cin". The "cin" will flush the buffer of any remaining output before any input is taken. This works to your advantage.
Andy