Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,172 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,928 people online right now. Registration is fast and FREE... Join Now!




2 quick questions

 
Reply to this topicStart new topic

2 quick questions

The1
15 Dec, 2007 - 08:27 PM
Post #1

New D.I.C Head
*

Joined: 15 Dec, 2007
Posts: 10


My Contributions
I have to write a program that produces an outcome of this effect;

QUOTE
Beginning Interest Principal Ending Loan
Payment Payment Balance Balance
---------- --------- ---------- ------------
8000.000000 66.666667 233.333333 7766.666667
7766.666667 64.722222 235.277778 7531.388889
7531.388889 62.761574 237.238426 7294.150463
7294.150463 60.784587 239.215413 7054.935050
....


Well, when I use this class/program (don't laugh I suck at classes..lol)
CODE
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

class Balance

{
private:
    int balance;
    int prin;
    int paid;
    int begBalance;
    int cumInt;
    int monInt;


public:
    Balance();
    Balance(int);
    
};

Balance::Balance()

{
    begBalance = 8000;
    paid;
    
};


int main()
{
  const int MAX_BEGBALANCE = 8000;
  const int MIN_BALANCE = 0;
  double balance = 8000;
  double paid = 300;
  double prin, cumInt, begBalance, monInt, totalPrin;


    



  cout << setiosflags(ios::fixed) << setprecision(6);


    cout << "Beginnging   Interest    Principal    Ending Loan\n";
    cout << "Payment      Payment     Balance      Balance\n";
    cout << "-------------------------------------------------\n";

  {
    
    
    monInt = begBalance * (0.10/12);
    prin = paid - monInt;
    balance = begBalance - prin;
    cumInt = monInt++;
    totalPrin = begBalance--;

    cout << setw(10) << begBalance << "       "
         << setw(10) << monInt << "     "
         << setw(10) << prin << "        "
         << setw(10) << balance << "     " << endl;


  }

  return 0;
};


I get the response that begBalance is not being initiated. I don't see what is done wrong.

Next question is when I take a few things out, it starts on 7999 instead of 8000. Does anyone see what is causing this??

This post has been edited by The1: 15 Dec, 2007 - 08:53 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: 2 Quick Questions
15 Dec, 2007 - 09:39 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well your first problem is that you define a variable called "begBalance" but then you attempt to immediately use it in the statement

CODE

monInt = begBalance * (0.10/12);


Remember, if you are going to use it in a calculation you must first set it to a value. Until you do that, it is sitting there as NULL. So when you define begBalance, try setting it to a beginning value BEFORE you use it in the multiplication.

It may start at 7999 because you subtract one using the line...

CODE

totalPrin = begBalance--;


With such a line you are assigning the value begBalance to totalPrin, but then you are decrementing it. So its value will be 7999 from that statement on.

Now I don't know what you have taken out or put in, but these two situations are probably what are causing your errors.

Hope that helps! smile.gif
User is offlineProfile CardPM
+Quote Post

The1
RE: 2 Quick Questions
16 Dec, 2007 - 03:52 AM
Post #3

New D.I.C Head
*

Joined: 15 Dec, 2007
Posts: 10


My Contributions
I appreciate the help but now I'm have the hardest time trying to add in this loop. I have the desired outcome which is

QUOTE
Beginning Interest Principal Ending Loan
Payment Payment Balance Balance
---------- --------- ---------- ------------
8000.000000 66.666667 233.333333 7766.666667
7766.666667 64.722222 235.277778 7531.388889
7531.388889 62.761574 237.238426 7294.150463
7294.150463 60.784587 239.215413 7054.935050
7054.935050 58.791125 241.208875 6813.726176
6813.726176 56.781051 243.218949 6570.507227
6570.507227 54.754227 245.245773 6325.261454
6325.261454 52.710512 247.289488 6077.971966
6077.971966 50.649766 249.350234 5828.621732
5828.621732 48.571848 251.428152 5577.193580
5577.193580 46.476613 253.523387 5323.670193
5323.670193 44.363918 255.636082 5068.034112
5068.034112 42.233618 257.766382 4810.267729
4810.267729 40.085564 259.914436 4550.353294
4550.353294 37.919611 262.080389 4288.272904
4288.272904 35.735608 264.264392 4024.008512
4024.008512 33.533404 266.466596 3757.541916
3757.541916 31.312849 268.687151 3488.854766
3488.854766 29.073790 270.926210 3217.928555
3217.928555 26.816071 273.183929 2944.744627
2944.744627 24.539539 275.460461 2669.284165
2669.284165 22.244035 277.755965 2391.528200
2391.528200 19.929402 280.070598 2111.457601
2111.457601 17.595480 282.404520 1829.053081
1829.053081 15.242109 284.757891 1544.295190
1544.295190 12.869127 287.130873 1257.164317
1257.164317 10.476369 289.523631 967.640686
967.640686 8.063672 291.936328 675.704359
675.704359 5.630870 294.369130 381.335228
381.335228 3.177794 296.822206 84.513022
84.513022 0.704275 84.513022 0.000000

But I'm exhausting myself trying to figure out how to add the interest. Do I use a nested loop or just a "for" method? Which is the easiest route?

This post has been edited by The1: 16 Dec, 2007 - 04:13 AM
User is offlineProfile CardPM
+Quote Post

The1
RE: 2 Quick Questions
16 Dec, 2007 - 03:59 PM
Post #4

New D.I.C Head
*

Joined: 15 Dec, 2007
Posts: 10


My Contributions
*looking around and bumps*

biggrin.gif
User is offlineProfile CardPM
+Quote Post

kdbolt70
RE: 2 Quick Questions
16 Dec, 2007 - 04:31 PM
Post #5

New D.I.C Head
*

Joined: 16 Dec, 2007
Posts: 19


My Contributions
Well I'm very new to the forums, so I'm not sure how much of the solution I can post up. What it looks like to me is you have this class defined, and then never use it in main. There's really no reason to declare all these variables:

CODE
  double balance = 8000;
  double paid = 300;
  double prin, cumInt, begBalance, monInt, totalPrin;


When you have them all hanging out in your Balance class. What you really want to do is construct a Balance object in main. Then all of your variables you are modifying and printing are contained in this object.

Also, it looks like you gave up on defining your constructors halfway through. If you initialize all your member variables in your constructor, you don't have to touch them untill you want to use them. Then, you could just do something like this:

CODE

Balance bal;
while(bal.balance > MIN_BALANCE){
    bal.monInt = bal.begBalance * (0.10/12);
    bal.prin = bal.paid - bal.monInt;
    bal.balance = bal.begBalance - bal.prin;
    bal.cumInt = bal.monInt++;

    cout << setw(10) << bal.begBalance << "       "
         << setw(10) << bal.monInt << "     "
         << setw(10) << bal.prin << "        "
         << setw(10) << bal.balance << "     " << endl;
}


This post has been edited by kdbolt70: 16 Dec, 2007 - 04:32 PM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: 2 Quick Questions
16 Dec, 2007 - 04:54 PM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(The1 @ 16 Dec, 2007 - 06:52 AM) *

But I'm exhausting myself trying to figure out how to add the interest. Do I use a nested loop or just a "for" method? Which is the easiest route?



Well, since you've got a working version, here's mine:
CODE


#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void amortizeForPayment(double startingBalance, const double paymentRate, const double payment) {
    double totalInterest = 0;
    int paymentNum = 0;

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    cout << " Pmt  Beginnging    Interest   Principal      Ending" << endl;
    cout << " Num     Balance     Payment     Payment     Balance" << endl;
    cout << "----------------------------------------------------" << endl;
    
    while (startingBalance > 0.0) {    
        paymentNum++;
        double paymentInterest = startingBalance * paymentRate;
        // round to nearest cent
        paymentInterest = round(paymentInterest * 100)/100;
        totalInterest += paymentInterest;
        double paymentPrincipal = payment - paymentInterest;
        // if we're on the last payment, adjust
        if (startingBalance<=paymentPrincipal) {
            paymentPrincipal = startingBalance;
        }
        
        cout << setw(4) << paymentNum;
        cout << setw(12) << startingBalance;
        cout << setw(12) << paymentInterest;
        cout << setw(12) << paymentPrincipal;
        
        startingBalance -= paymentPrincipal;
        
        cout << setw(12) << startingBalance;
        cout << endl;
    }
    
    cout << "----------------------------------------------------" << endl;
    cout << "Total Interest Paid = " << totalInterest << endl;
}


void amortizeForAnnual(double startingBalance, double annualRate, double payment) {
    double paymentsPerYear = 12;
    double paymentRate = (annualRate / paymentsPerYear);
    cout << "Payment = " << payment << endl;
    cout << "Annual Rate = " << (annualRate*100) << "%" << endl;
    amortizeForPayment(startingBalance, paymentRate, payment);
}


int main() {  
    amortizeForAnnual(8000, 0.10, 300);
   return 0;
}


I don't know how reasonable it is to create a class for what's essentially output from a single function call.

Hope this helps.

User is offlineProfile CardPM
+Quote Post

The1
RE: 2 Quick Questions
16 Dec, 2007 - 05:18 PM
Post #7

New D.I.C Head
*

Joined: 15 Dec, 2007
Posts: 10


My Contributions
<<<hates you all....lol

But this is what I came up with

CODE
int main()
{
  
  double begBalance = 8000;
  double paid = 300;
  double balance, prin, cumInt, monInt;


  balance = 8000;
  paid = 300;

  w

  cout << setiosflags(ios::fixed) << setprecision(6);


    cout << "Beginnging         Interest      Principal         Ending Loan\n";
    cout << "Payment            Payment       Balance           Balance\n";
    cout << "-------------------------------------------------------------\n";


while(balance > 0)
  {
    begBalance = balance;
    monInt = begBalance * (0.10/12);
    prin = paid - monInt;
    balance = begBalance - prin;
    cumInt = monInt++;

if(begBalance < 100)
    prin = begBalance;
    balance = begBalance - prin;



    
    cout << setw(10) << begBalance << "       "
         << setw(10) << monInt << "     "
         << setw(10) << prin << "        "
         << setw(10) << balance << "     " << endl;

    
  }
  cout << "The total interest paid is:" << cumInt << endl;

};


But then when it comes to the total interest, it just gives me the last monInt which is 0.704275. I'm just on the verge of throwing my laptop..lol
User is offlineProfile CardPM
+Quote Post

baavgai
RE: 2 Quick Questions
16 Dec, 2007 - 06:34 PM
Post #8

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Here's you code with very minor changes and comment as to why:
CODE

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
  
  double begBalance = 8000;
  double paid = 300;
  double balance, prin, cumInt, monInt;


  balance = 8000;
  paid = 300;
  // make sure this starts at 0
  cumInt = 0;

  cout << setiosflags(ios::fixed) << setprecision(6);


    cout << "Beginnging         Interest      Principal         Ending Loan\n";
    cout << "Payment            Payment       Balance           Balance\n";
    cout << "-------------------------------------------------------------\n";


while(balance > 0)
  {
    begBalance = balance;
    monInt = begBalance * (0.10/12);
    prin = paid - monInt;
    // you rewrite this later, so don't do it
     // balance = begBalance - prin;
     // this says, um, make cumInt = monInt and then add 1 to monInt
    // cumInt = monInt++;
     cumInt += monInt;
    
     // not the best logic -- if(begBalance < 100)
     // this will always work
     if(begBalance < prin) {
         prin = begBalance;
     }
    balance = begBalance - prin;



    
    cout << setw(10) << begBalance << "       "
         << setw(10) << monInt << "     "
         << setw(10) << prin << "        "
         << setw(10) << balance << "     " << endl;

    
  }
  cout << "The total interest paid is:" << cumInt << endl;

};


I'd, um, take the comments out before you turn it in. wink2.gif

User is offlineProfile CardPM
+Quote Post

The1
RE: 2 Quick Questions
16 Dec, 2007 - 10:07 PM
Post #9

New D.I.C Head
*

Joined: 15 Dec, 2007
Posts: 10


My Contributions
Crap...I knew I forgot something...*hoping my instructor doesn't catch the comments* lol

But seriously, after a little tweaking, I finally got it. And Baav, I thought for a minute why wouldn't my if statement not work all time. I get it now..

*hi 5*
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:36AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month