Programming Basic's Help

Status
Not open for further replies.

hk3008

Distinguished
Jan 29, 2012
57
0
18,630
Hello, how are yall doing again I had a quick question I am coding a mortgage calulator for my basic programming class 1 I have this so far but Visual C++ 2005 wont debug what am I missing?

//mortgage Calculator
#include <iostream>
#include <cmath>
usingnamespace std;

//declaring variables
float p = 0; /* Loan Amount */
float i = 0; /* Interest Rate APR */
int l = 0; /* Term */
float j = i/(12 * 100); /* Calculate Interest */
int n = l * 12; /* Bumber of Months */
double m = 0; /* Monthly Payment */
int choice = 0

int main()
{
do
{
cout <<"Enter the loan amount:" << endl
cin >> p;
cout << "Enter term of loan:" << endl;
cin >> l;
cout << "The inerest rate is:" <<endl;
cin >> i;

//calculating the mortgage using this formula
m = (p*j)/(1-pow((1+j),-n))

cout << "The monthly payment is: $" << m << endl; //prints out monthly payement

return 0;
}
 
Solution

lega

Distinguished
Feb 9, 2012
19
0
18,520
There is the last } missing and do-loops require a while that ends the loop.

But it doesn't seem like your script needs a loop so just delete the do

also I think your main part isn't declared right but I'm not so sure about that. I am a beginner myself

using namespace std;

int main()
{

cout <<"Enter the loan amount:" << endl
cin >> p;
cout << "Enter term of loan:" << endl;
cin >> l;
cout << "The inerest rate is:" <<endl;
cin >> i;

//calculating the mortgage using this formula
m = (p*j)/(1-pow((1+j),-n))

cout << "The monthly payment is: $" << m << endl; //prints out monthly payement

return 0;

}
 

theDanijel

Distinguished
May 4, 2011
150
0
18,710
I agree with all above, but there is one big logical mistake.
First of all, the main can be declared a couple of ways and this is one of them.
Declare your variables inside the main() function. Global variables are just not efficient and can cause a lot of problems in big projects, so I recomend that you never get accostumed to using them. Like this all your variables are declared and initialized outside of main's scope. Althou you can stay with declarations like this, calculate "j" and "n" after user input (like this they allways stay 0). If they stay like this you will get a case of 0/0 and that will never woork!
(p*j) where j=0 will allways be 0
pow((1+j),-n) where j=0 and n=0 will allways be 1 (and 1-1 is 0)
 

hk3008

Distinguished
Jan 29, 2012
57
0
18,630
ok so I modified it but I still cannot get it to debug but here is what I have now

//mortgage Calculator;
#include <iostream>;
#include <cmath>;
usingnamespace std;

//declaring variables;
float p = 0; /* Loan Amount */;
float i = 0; /* Interest Rate APR */;
int l = 0; /* Term */;
float j = i/(12 * 100); /* Calculate Interest */;
int n = l * 12; /* Bumber of Months */;
double m = 0; /* Monthly Payment */;
int choice = 0;

int main()
{
cout <<"Enter the loan amount:" << endl;
cin >> p;
cout << "Enter term of loan:" << endl;
cin >> l;
cout << "The inerest rate is:" <<endl;
cin >> i;

//calculating the mortgage using this formula;
m = (p*j)/(1-pow((1+j),-n));

cout << "The monthly payment is: $" << m << endl; //prints out monthly payement;

return 0;
}


and @the danijel where you are saying that if the user input is 0 then the outcome will not be calculable right? if that is the case is there anyway to code it so that the user can not type in 0 and if it is not I guess I dont understand sorry this is my first project and my first programming class so I am open to learning as much as possible to make it work thankyou
 

calmstateofmind

Distinguished
//mortgage Calculator

#include <iostream>
#include <cmath>

using namespace std;

int main()
{

//declaring variables;
float p = 0; // Loan Amount
float i = 0; //Interest Rate APR
int l = 0; //Term
float j = i/(12 * 100); // Calculate Interest
int n = l * 12; // Number of Months
double m = 0; // Monthly Payment
int choice = 0;

cout << "Enter the loan amount:" << endl;
cin >> p;
cout << "Enter term of loan:" << endl;
cin >> l;
cout << "The interest rate is:" <<endl;
cin >> i;

//calculating the mortgage using this formula
m = (p*j)/(1-pow((1+j),-n));

//prints out monthly payment
cout << "The monthly payment is: $" << m << endl;

return 0;
}


The above is syntactically correct and would successfully compile & run. Also, it would be considered very acceptable in terms of code structure and formatting.

The first changes I made to your code was with the comments on the variable initializations. Usually if you're only commenting on a single line, it's general practice to simply use "//" to negate the proceeding code from compilation. "/*" and "*/" are more used for multi line comments. I also removed the ";" at the end of each of your commented lines. Remember, anything that follows "//" will be ignored by the compiler for the rest of that line, and "/*" will cause ALL following code to be ignored until a "*/" arises.

The next thing I did was move all of those variable initializations inside the main function. Unless they're constant/global variables (meaning variables that they can be referenced by other functions within a program, or by other files all together), it's not a good idea to declare variables outside the main function (or the function the variable(s) is exclusively being used in). If you have variables that aren't constant/global mixed around with ones that are, it can be very difficult and time consuming to quickly reference/edit specific ones.

When you get to more complex programs, organization is key. And, applying it to this, adding spacing and indentation is definitely needed. Whenever you have a body of code within {}'s, it's good habit to do some form of indenting. This is to avoid confusion and help in the ease of flow when writing/editing/debugging. When you get to where you have multiple, 5-10, or even more, loops within each other, it can be hard to follow what's going on. Indenting at least gives some seperation and distinction between tasks and makes it easier to read.

"using namespace std" was combined too, which would cause an error. It's gotta be typed just as how it is in the parenthesis. And in regards to the "choice = 0" idea, there's actually a variable made for this logic. It's called a boolean data type. Instead of using 0 or 1, you use TRUE or FALSE (though bool choice = 1 would functionally be the same as bool choice = TRUE). Be patient though, you'll get to it soon enough. :p

EDIT: I tried to indent the body of the main function but it realigns to the left. Just hit tab and you'll be good.
 

theDanijel

Distinguished
May 4, 2011
150
0
18,710


I'm saying this way no matter what user inputs the result will allways be uncalculable since you have a case when zero is devided with zero!
@calmstateofmind your result is the same. Syntax correct but end result the same. One more thing, the main function return int for practical and somewhat conventional reasons from C. It has allways been a convention to return -1 if there was some kind of error that was detected but could not be handeled, some other negative value coresponding to the error code or positive value wiht some result or completion code.

Try like this:

//mortgage Calculator

#include <iostream>
#include <cmath>

using namespace std;

int main()
{

//declaring variables
float p = 0; // Loan Amount
float i = 0; //Interest Rate APR
int l = 0; //Term
float j = i/(12 * 100); // Calculate Interest
int n = l * 12; // Number of Months
double m = 0; // Monthly Payment
int choice = 0;

cout << "Enter the loan amount:" << endl;
cin >> p;
cout << "Enter term of loan:" << endl;
cin >> l;
cout << "The interest rate is:" <<endl;
cin >> i;

//after user input, j and n must be recalculated, otherwise they will stay 0 !!!!
j = i/(12 * 100);
n = l * 12;

//calculating the mortgage using this formula
//if not for the recalculation, you wil have a 0/0 and depending on the runtime you will have either some garbage or an error
m = (p*j)/(1-pow((1+j),-n));

//prints out monthly payment
cout << "The monthly payment is: $" << m << endl;

return 0;
}

And if it won't debug, make sure you put breakpoints in your code and run your program in debug mode. If that won't work just print out variables or somthing in critical code places (like in the days without debuggers).
 
Solution

calmstateofmind

Distinguished


Right...because it's probably best to leave the actual programming/problem solving to the student. It's one thing to help with syntax and formatting, but for a beginning programmer who's in dire need to fully grasp concepts that are best learned only through debugging their code, it can really take away from their learning of how a program flows, how a computer "thinks", and how to handle data when writing a program.

Also, I highly doubt they've gotten to debugging yet (in the sense of breakpoints, etc), with this being their very first programming assignment.
 

hk3008

Distinguished
Jan 29, 2012
57
0
18,630
ok so I have made some more of the changes and have been reading the book but the debugging tab is still grayed out under debugging what am I doing wrong? and where would the option to compile it be? an this is in Visual C++ 2005 Express Edition, this was my original problem I couldnt figure out how to just get it to run :/
 

calmstateofmind

Distinguished



There should be a tab labeled "Run". Under there, simply select Run. It'll then prompt you to "build" (compile) the program, and if there are no errors, it'll then proceed to run. Any time that you make any change to the program, you'll have to rebuild it before running.
 

hk3008

Distinguished
Jan 29, 2012
57
0
18,630
could it have something to do with C++ 2005 express or does it have to be a .txt file I have it as a .cpp an I tried a text file it still wont let me debugg or run do ineed a different version of C++ or am I missing some comand line this is what I have now

//mortgage Calculator

#include <iostream>
#include <cmath>

using namespace std;


int main()
{

//declaring variables
float p = 0; // Loan Amount
float i = 0; //Interest Rate APR
int l = 0; //Term
float j = i/(12 * 100); // Calculate Interest
int n = l * 12; // Number of Months
double m = 0; // Monthly Payment
int choice = 0;

cout << "Enter the loan amount:" << endl;
cin >> p;
cout << "Enter term of loan:" << endl;
cin >> l;
cout << "The interest rate is:" <<endl;
cin >> i;

//after user input, j and n must be recalculated, otherwise they will stay 0 !!!!
j = i/(12 * 100);
n = l * 12;

//calculating the mortgage using this formula
m = (p*j)/(1-pow((1+j),-n));

//prints out monthly payment
cout << "The monthly payment is: $" << m << endl;

return 0;
}
 

hk3008

Distinguished
Jan 29, 2012
57
0
18,630
I was just opening a new project then creating the file in a .cpp but it wouldnt compile now I am opening up a test Project in new project and usind the stdafx.cpp and editing it to what I need and now its compiling and Debugging thankyou everyone for your help now time for my next project
 
Status
Not open for further replies.