I have just completed a mortgage calculator for my intro to C++ programming class. The code compiles and meets all the instructors requirements. My future assignments will build on top of previous ones. So what I am asking for are opinions on how to write better code. My goal is to complete my assignments with fewest lines of code and still meet all the requirements. Any constructive criticism welcome.

``````#include <iostream>
#include <cmath>           // must include for pow function

using std::cout;
using std::cin;
using std::endl;

int main ()
{
// defines varibles
double loanAmt = 0.0;
double interest = 0.0;
double term = 0.0;
char quit;

do
{

cout << endl;
cout << "Enter interest rate:";
cin >> interest;

cout << endl;
cout << "Enter term in years:";
cin >> term;

cout << endl;
cout << "Enter Loan amount:";
cin >> loanAmt;
cout << endl;

double monthlyPmt = loanAmt*(interest/1200)/(1-pow(1+(interest/1200),-1*term*12)); // amoritization formula
double monthlyInt =  loanAmt * (interest/1200);
double principalPaid = monthlyPmt - monthlyInt;
double balance = loanAmt-monthlyPmt;

cout << "For a "<< loanAmt << " Loan your payment is " << monthlyPmt << endl;
cout << "\n";
cout << " New loan balance is " << balance << endl;
cout << "\n";
cout << " Your interest paid for this month is " << monthlyInt << endl;
cout << "\n";
cout << " Your Principal paid for this month is " << principalPaid << endl;
getchar();                   // pauses the program and allows you to view the output
// user can continue in a loop or quit
//output

cout<<"\n";
cout<<"If you wish to continue press  C then the enter key on your keyboard.\n";
cout<<"If you wish to quit press Q then the enter key on your keyboard.\n";

//user input
cin>>quit;
cout<<"\n";

while((quit!='q') && (quit!='Q') &&

(quit !='c') && (quit !='C'));
}

while((quit!='q') && (quit!='Q'));

cout<<"Thank you for trying my simple mortgage calculator\n";

}``````

lines 19 and 20 can be combined

``cout << "\nEnter interest rate:";``

spacing and general program formatting style is pretty lousy. Indentation should be consistent throughout the program.

put line 66 on the same line as line 64 then delete empty lines between.

delete lines 6i0, 61 and 62. They appear to be just an infinit while loop.

I'm not a C++ programmer but the fewest lines of code doesn't always translate to the most efficient or the best way of doing things.

I'm not a C++ programmer but the fewest lines of code doesn't always translate to the most efficient or the best way of doing things.

That makes sence. Never thought of it that way.

and don't mix stream io with oldstyle io.

btw, Dani is quite correct. What counts for more than anything is how easy your code is to read (and therefore maintain).

lines 19 and 20 can be combined

``cout << "\nEnter interest rate:";``

spacing and general program formatting style is pretty lousy. Indentation should be consistent throughout the program.

put line 66 on the same line as line 64 then delete empty lines between.

delete lines 6i0, 61 and 62. They appear to be just an infinit while loop.

This was my first post here. I read a lot of other peoples posts before posting my question. Since the assignment has allready been submitted for a grade its to late to take your advice with this assignment. I will however take what you have said into account with my future assingments.

lines 19 and 20 can be combined

``cout << "\nEnter interest rate:";``

spacing and general program formatting style is pretty lousy. Indentation should be consistent throughout the program.

put line 66 on the same line as line 64 then delete empty lines between.

delete lines 6i0, 61 and 62. They appear to be just an infinit while loop.

In an attempt to have cleaner code to work from with my next assignment, I made your suggested changes as well as a few more
I took the changes you suggested to lines 19 and 20 and combined lines 23 and 24 as well as 27 and 28. I also fixed the while loop and cleaned up the formating. I must admit that it looks a lot better and is easier to read. Again, thank you for the help. Next week I start working on outputing the complete amortization schedule. This means more playing with loops (LOL) At lest now I will have easier to read code to start with.

and don't mix stream io with oldstyle io.

Meaning, use C++ style only, don't mix with getchar(), printf(). IOW, anything from stdio.h/cstdio.

In an attempt to have cleaner code to work from with my next assignment, I made your suggested changes as well as a few more...

If you post it, we can look at the code again. Maybe there's more stuff -- like main() needs a return statement.... ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.