Join 132,625 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,064 people online right now. Registration is fast and FREE... Join Now!
Need help learnig C++. I'm using Microsoft Visual Studio 2003. My assignment reads as bellow. Any help would be appreciated. I'm not a programmer of any kind, so really do not know where to start.
Write a procedural C++ program that calculates and displays the mortgage payment amount given the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. In this program, hard code the amount = $200,000, the term = 30 years, and the interest rate = 5.75%. Insert comments in the program to document the program.
Oops guess I really should have done that. Here is my code that works as a C type program, but when I tryt to use cout it will not go. I have the iostream lib called but still nada unless I use printf. Again, I am using MS Visual Sudio 2003.
CODE
// Write a procedural C++ program that calculates and displays the mortgage payment //amount given the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. //In this program, hard code //the amount = $200,000 //the term = 30 years //the interest rate = 5.75%. //Insert comments in the program to document the program.
int main() { int a = 200000; //amount of the loan double i = .00575; // the losn interest rate int y = 30; //years of the loan int t = y*12; //loan term in months int mPayment; //variable for ouputting the payment
mPayment = (a * i) / (1 - pow(1+i,-t)); //Formula to figure mortgage payment amount
printf( "Your Monthly Payment Amount is: $ %i\n", mPayment); //prints out montyly payment amount
return 0; }
This post has been edited by Dark_Nexus: 7 Sep, 2006 - 08:14 AM
#include "math.h" #include <iostream> using namespace std;
int main() { int a = 200000; //amount of the loan double i = .00575; // the losn interest rate int y = 30; //years of the loan int t = y*12; //loan term in months double mPayment; //variable for ouputting the payment
mPayment = (a * i) / (1 - pow(1+i,-t)); //Formula to figure mortgage payment amount
cout<< "Your Monthly Payment Amount is: $"<< mPayment; //prints out montyly payment amount return 0; }
It's only a slight modification, but it runs fine...I'm not sure how you were using cout before, but this version works...you can likely use it as a base to continue.
#include "math.h" #include <iostream> using namespace std;
int main() { int a = 200000; //amount of the loan double i = .00575; // the losn interest rate int y = 30; //years of the loan int t = y*12; //loan term in months double mPayment; //variable for ouputting the payment
mPayment = (a * i) / (1 - pow(1+i,-t)); //Formula to figure mortgage payment amount
cout<< "Your Monthly Payment Amount is: $"<< mPayment; //prints out montyly payment amount return 0; }
It's only a slight modification, but it runs fine...I'm not sure how you were using cout before, but this version works...you can likely use it as a base to continue.
thx for the help. I was using cout properly. Its nice to see I was not wrong, but this is the error message I get. when I compile in visual studio. When I use printf it works, when I use cout it screws up.
error C2065: 'cout' : undeclared identifier
thx for the help. I was using cout properly. Its nice to see I was not wrong, but this is the error message I get. when I compile in visual studio. When I use printf it works, when I use cout it screws up.
I think you have hit upon it. I specified it as a console project. I will allso check on the namespace as soon as VS2003 finishes loading on my home system. Thank you for the advice and direction. I'm sure it is only going to get harder, but I think I might be moving in the right direction. I'm assuming this needs to be a win32 application to use the cout and cin.
Actually, cout and cin can be used in virtually any type of c++ project...I was more concerned about the inclusion of the std namespace. I believe this project should be a console project, so I think you're on the right track there...try using the namespace line, and see if it works.
Some things will be harder, but I'm sure you'll pick it up very quickly.
Thx for your help. my project has moved along nicely up until I started playing with data validation. I'm trying to get my char to parse to double so I can use it in calculations. I've written the code the way I think it should work, but of course it does not. I've punded at this for hours and can't get it work. There is suppossed to be an easy way to do this, but I can't get it to synch up. Here's the update code. can you see what I am doing wrong?
int main()//begining of main function { char Exit = 'y'; //char used in while loop to continue or exit char P_char[ 10 ]; //char array for Principal char T_char[ 3 ]; //char array for Term char I_char[ 5 ]; //char array for Interest double P = 0; //Amount of the loan double I = 0; //The loan interest rate double T =0; //The length of the mortgage in years int C = 1; //declare variable type for c used as counter later int M = 12; //Twelve Months in a year double IM = (I/100)/M;//The interest rate per month double TM = T * M;//total number of payments over the life of the loan (Term*Months) double MonthlyPayment; //Variable for Monthly Payment
cout << fixed; //sets fixed decimal point
do{ cout <<"\n\t\t*******************************"; //output to screen cout <<"\n\t\t** Mortgage Calculator **"; //output to screen like a header cout <<"\n\t\t*******************************"; //output to screen cout << endl; cout <<"\n\t Enter the Loan Information when Prompted"; //output to screen instructions cout <<"\n\t\t*******************************"; //output to screen cout << endl;
//char P_char[ 10 ]; cout << "\n\t\tPlease enter the loan Principal: $"; cin >> P_char;
if (validNumber(&P_char[ 0 ])) { P = double.Parse(P_char); cout << endl; } else { cout << "\n\t\tyou did NOT enter a valid number! \n\t\tPlease enter nubers only: $"; cin.clear();//reset cin, so more input can be done cin.ignore(50,'\n');//remove bad data, up to 50 characters or until the end of line is reached cin >> P_char; }
//char I_char[ 10 ]; cout << "\n\t\tPlease enter the loan Interest Rate: "; cin >> I_char;
if (validNumber(&I_char[ 0 ])) { I = double.Parse(I_char); cout << endl; } else { cout << "\n\t\tyou did NOT enter a valid number! \n\t\tPlease enter numbers and decimal only: "; cin.clear();//reset cin, so more input can be done cin.ignore(50,'\n');//remove bad data, up to 50 characters or until the end of line is reached cin >> I_char; }
//char T_char[ 10 ]; cout << "\n\t\tPlease enter the loan Term in years: "; cin >> T_char;
if (validNumber(&T_char[ 0 ])) { T = double.Parse(T_char); cout << endl; } else { cout << "\n\t\tyou did NOT enter a valid number! \n\t\tPlease enter numbers only: "; cin.clear();//reset cin, so more input can be done cin.ignore(50,'\n');//remove bad data, up to 50 characters or until the end of line is reached cin >> T_char; }
IM = (I/100)/12; //Calculates Interest Monthly TM = T * M; //Calculates Term Months MonthlyPayment = P * (IM/(1 - pow((1+IM), -TM))); //Calculates monthly loan payment while (C >= 0){ cout <<"\n\t\t ******************************"; //prints to screen cout <<"\n\n\t\tYour Monthly Loan Payment is: $" << setprecision(2) << MonthlyPayment << endl; //prints out montyly payment amount cout <<"\n\t\t ******************************"; //prints to screen cout << "\n\t\t";//adds another line and move the press key statement C--; }
cout <<"\n\t\t\" y \" to continue any other to quit: "; //output to screen cin >> Exit; //Input from User system ("cls"); //clears the screen } while(Exit == 'y'); //end of do loop
return 0;
} //end of main
This post has been edited by Dark_Nexus: 7 Sep, 2006 - 08:14 AM
May I ask why you are taking the numerical input as chars? You may be familiar with java, where input was commonly taken as strings of chars, but c++ (and java's scanner class) allow for input to be taken directly as whatever variable type you require...you can take them directly as doubles, unless you have a reason for wanting them as chars that I am not aware of...
If you absolutely must take the input as characters or strings, then you can use the strtod() function or the atof() function...but I would still recommend taking the input directly as a double.
Hi, I wish I could take the input as a double, but I am attempting to do data validation, and I just don't get it, and can't make it work when I take the input as double and try to check it. If I don't use an array to check I can't get it to work. I have tried six or seven different ways of doing this and have spent ten plus hours at it, none of the methods work completely. The easiest way was to use !cin on the input, but as soon as you type in "3wd4e" or similar it treats it as a valid value. I tried !fail.cin. I even tried this string input thing I really did not understand. This array works but I have to convert the values to doubles once I am done and I can't figure out a way to do it. I’ll have to read about the strtod() and atof() functions you mentioned. I have not seen or heard of them before, and see If I can figure it out. I am just frustrated to the point of using imperfect data validation because I cannot get it to work. Thank you for your help and direction. Oh and yes Java was the first language I messed with so I might be in an incorrect mind set.
atof(-String to be converted-) will return the floating point value found in the string.
for example;
CODE
char mystring[10] = "13.337";
float myfloat = atof(mystring);
myfloat would then be 13.337
also, you may want to try cin.good() when retreiving numbers from users
CODE
float myfloat = 0.0;
cin >> myfloat; if (cin.good()) { //Input was indeed a floating point value, continue } else { cout << "ERROR: Input was not a floating point value.\n"l }