Jump to content
  • Advertisement
Sign in to follow this  
jrevTX

Interest rate problem in C++

This topic is 4232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is the problem. The program keeps running into a debug error stating that "totInt" is being used without being defined. Now what am I missing to make this program run without an error? Also if that is the case, then I think that "totPrin" will also come up with the same error. What do I do to fix it so that my cout messages will display correctly at the bottom of the program? Right now all I am getting is a negative number that I know is not correct. Thanks ahead of time for the help. #include <iostream> #include <iomanip> using namespace std; int main() { const double INTERESTRATE = 0.10/12.0; const double PAYMENT = 300.0; double value = 8000.0; double interest, prin, totInt, totPrin; int count = 1; cout << " Beginning Interest Principal Ending Loan\n" << " Payment Payment Balance Balance\n" << "---------- ---------- ---------- -----------\n"; cout << setiosflags(ios::fixed) << setprecision(6); while(value > 0.0) { cout << setw(11) << value << " "; interest = value * INTERESTRATE; totInt = totInt + interest; if(value < PAYMENT) prin = value; else prin = PAYMENT - interest; totPrin = totPrin + prin; value = value - prin; cout << setw(9) << interest << " " << setw(10) << prin << " " << setw(11) << value << endl; } cout << "\nThe total interest paid is: " << totInt << endl; cout << "The total principal paid is: " << totPrin << endl; cin.ignore(); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
You are not initializing totPrin and totInt to a value! Which in turn means that
totPrin = totPrin + prin;

and
totInt = totInt + interest;


will not have the desired effect since totPrin and totInt are not equal to zero at the start of your program, which you seem to be expecting them to be.

Share this post


Link to post
Share on other sites
Quote:
Original post by jrevTX
Here is the problem. The program keeps running into a debug error stating that "totInt" is being used without being defined.

Declaration vs definition:
extern int i;  // declaration
int j = 5; // initialization, definition
int k; // definition, but no value assigned; k contains "garbage" value


You define your variable, but you fail to initialize it, and your first use of it after declaration/definition is in increment - totInt = totInt + interest. What's the original value of totInt?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!