5 Replies - 1658 Views - Last Post: 07 December 2008 - 04:12 PM Rate Topic: -----

#1 mistymoon1966   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

New problem: looping to beginning of program

Post icon  Posted 07 December 2008 - 02:14 PM

I have a mortgage amortization program that works but no matter what I try I cannot get the program to loop back to the beginning. I just posted the assingment but have to build on it next week and i need the program to loop back to the beginning. I have tried to add an indicator to my loop and tried creating a new question loop to no avail. I am really frustrated here, can someone help me. Can someone explain to me how to loop the program back to the beginning and where to insert the code, I would really appreciate it, thanks.

#include "math.h"
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

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

//mortgage class//
class Mortgage

{
	public: 
	void header(); 
	void enterprinc();
	double princ; 
	double anInt; 
	int yrs; 
};

//define function//

void Mortgage::header() 
{

	cout << "Welcome to Smith's Amortization Calculator\n\n";
	cout << endl;
	cout << "Week 3 Individual Assignment C++ Mortgage Amortization Calculator\n\n";
	cout << endl;

}

void Mortgage::enterprinc()
{

	cout << endl;
	cout << "Enter the amount The Mortgage Amount:$"; 
	cin >> princ;

}

//main function//
int main ()
{

Mortgage mortgageTotal;

	double princ = 0; 
	double anInt [4]= {0, 5.35, 5.5, 5.75,}; 
	double yrs [4]= {0, 7, 15, 30,}; 
	double pmt = 0; 
	double intPd = 0; 
	double bal = 0; 
	double amtPd = 0; 
	double check(int min, int max);
	int NOP = 0; 
	int Mnth = 0; 
	int SL = 0; 
	char indicator = ('A' ,'a','R','r','D','d'); 
	int select;  

//Begin Loop
{
	mortgageTotal.header ();
	mortgageTotal.enterprinc ();

		cout<< "Please Enter Your Selection!"<< endl;
		cout<< "Press A to make selection for term of mortgage and interest rate"<<endl;
		cin >> indicator;

			if (indicator == 'A','a')
			{

				cout << "Please select your Terms and Rates from the following choices: " << endl;
				cout << "1. 7 years at 5.35%" << endl;
				cout << "2. 15 years at 5.5%" << endl;
				cout << "3. 30 years at 5.75%" << endl;
				cout << endl;
				cout << "What is your selection:";

					select = 0;
					cin >> select;
					cout << endl << endl << endl;
						switch (select)
			{

				case 0: cout << endl; 
				break;
				case 1:
					yrs[2] = 7;
					anInt[2] = 5.35; 
					cout << "You have selected a 7 year mortgage at 5.35% interest." << endl; 
					break;

				case 2: 
					yrs[3] = 15;
					anInt[3]= 5.50; 
					cout << "You have selected 15 year mortgage at 5.50 interest%" << endl; 
					break;

				case 3: 
					yrs[4] = 30;
					anInt[4] = 5.75; 
					cout << "You have selected a 30 year mortgage at 5.75% interest" << endl; 
					break;

				default: 
					cout << "Invalid Line Number" << endl;
					if ((select < 1)|| (select > 3))

					{ 
						system("cls");
						cout << "Your choice is invalid, please enter a valid choice!!!";
						system("PAUSE");
						system("cls");
						return main();
					}
			}
			}
				
//Formulas//
double pmt = (mortgageTotal.princ*((anInt[select]/1200)/(1 - pow((1+(anInt[select]/1200)),-1*(yrs[select]*12))))); //  notice the variable in the brackets that i added.

	cout << "Your Monthly Payment is: $" << pmt << "\n";
	cout << endl;

double NOP = yrs [select] * 12;
	SL = 0;
		for (Mnth = 1; Mnth <= NOP; ++Mnth)
		{

	intPd = mortgageTotal.princ * (anInt[select] / 1200);   
	amtPd = pmt - intPd;
	bal = mortgageTotal.princ - amtPd;
		if (bal < 0)bal = 0;
			mortgageTotal.princ = bal;

			if (SL == 0)
			{
				cout << "Balance of Loan" << "\t\t\tAmount of Interest Paid" << endl;
			}

				cout << setprecision(2) << fixed << "$" << setw(5) << bal << "\t\t\t\t$"<< setw(5) << intPd << endl;
				++SL;

//Allows user to decide whether or not to continue//

	if (SL == 12)
	{
		cout << "Would you like to continue the amoritization or quit the program?\n";
		cout << endl;
		cout << "Enter 'R' to see the remainder or 'D' for done.\n";
		cin >> indicator;

			if 
				(indicator == 'R','r')SL = 0;

					else

			if

				(indicator == 'D','d')
				cout << endl;

	}

}

}

return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: New problem: looping to beginning of program

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,234
  • Joined: 14-September 07

Re: New problem: looping to beginning of program

Posted 07 December 2008 - 02:27 PM

//Begin Loop
do
{
    mortgageTotal.header ();
    mortgageTotal.enterprinc ();

        cout<< "Please Enter Your Selection!"<< endl;
        cout<< "Press A to make selection for term of mortgage and interest rate"<<endl;
        cin >> indicator;

            if (indicator == 'A','a')
            {

                cout << "Please select your Terms and Rates from the following choices: " << endl;
                cout << "1. 7 years at 5.35%" << endl;
                cout << "2. 15 years at 5.5%" << endl;
                cout << "3. 30 years at 5.75%" << endl;
                cout << endl;
                cout << "What is your selection:";

                    select = 0;
                    cin >> select;
                    cout << endl << endl << endl;
                        switch (select)
            {

                case 0: cout << endl;
                break;
                case 1:
                    yrs[2] = 7;
                    anInt[2] = 5.35;
                    cout << "You have selected a 7 year mortgage at 5.35% interest." << endl;
                    break;

                case 2:
                    yrs[3] = 15;
                    anInt[3]= 5.50;
                    cout << "You have selected 15 year mortgage at 5.50 interest%" << endl;
                    break;

                case 3:
                    yrs[4] = 30;
                    anInt[4] = 5.75;
                    cout << "You have selected a 30 year mortgage at 5.75% interest" << endl;
                    break;

                default:
                    cout << "Invalid Line Number" << endl;
                    if ((select < 1)|| (select > 3))

                    {
                        system("cls");
                        cout << "Your choice is invalid, please enter a valid choice!!!";
                        system("PAUSE");
                        system("cls");
                        return main();
                    }
            }
            }
               
//Formulas//
double pmt = (mortgageTotal.princ*((anInt[select]/1200)/(1 - pow((1+(anInt[select]/1200)),-1*(yrs[select]*12))))); //  notice the variable in the brackets that i added.

    cout << "Your Monthly Payment is: $" << pmt << "\n";
    cout << endl;

double NOP = yrs [select] * 12;
    SL = 0;
        for (Mnth = 1; Mnth <= NOP; ++Mnth)
        {

    intPd = mortgageTotal.princ * (anInt[select] / 1200);  
    amtPd = pmt - intPd;
    bal = mortgageTotal.princ - amtPd;
        if (bal < 0)bal = 0;
            mortgageTotal.princ = bal;

            if (SL == 0)
            {
                cout << "Balance of Loan" << "\t\t\tAmount of Interest Paid" << endl;
            }

                cout << setprecision(2) << fixed << "$" << setw(5) << bal << "\t\t\t\t$"<< setw(5) << intPd << endl;
                ++SL;

//Allows user to decide whether or not to continue//

    if (SL == 12)
    {
        cout << "Would you like to continue the amoritization or quit the program?\n";
        cout << endl;
        cout << "Enter 'R' to see the remainder or 'D' for done.\n";
        cin >> indicator;

//your checks are incorrect, needs to be in each conditional
            if (indicator == 'R','r')SL = 0;
             //do nothing cause it will continue the program

//your checks are incorrect, needs to be in each conditional
            if (indicator == 'D','d') {
                cout << endl;
                break;
            }

    }//end if from above?
}//end for?? you indentation is screwed up
} while ((indicator != 'D') && (indicator != 'd'));



See code comments
Was This Post Helpful? 0
  • +
  • -

#3 mistymoon1966   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Re: New problem: looping to beginning of program

Posted 07 December 2008 - 03:04 PM

I fixed the issues you mentioned below, but the program still does not loop back to the beginning and honestly I cannot figure out how to loop it back so the user can enter another mortage amount?


View PostKYA, on 7 Dec, 2008 - 01:27 PM, said:

//Begin Loop
do
{
    mortgageTotal.header ();
    mortgageTotal.enterprinc ();

        cout<< "Please Enter Your Selection!"<< endl;
        cout<< "Press A to make selection for term of mortgage and interest rate"<<endl;
        cin >> indicator;

            if (indicator == 'A','a')
            {

                cout << "Please select your Terms and Rates from the following choices: " << endl;
                cout << "1. 7 years at 5.35%" << endl;
                cout << "2. 15 years at 5.5%" << endl;
                cout << "3. 30 years at 5.75%" << endl;
                cout << endl;
                cout << "What is your selection:";

                    select = 0;
                    cin >> select;
                    cout << endl << endl << endl;
                        switch (select)
            {

                case 0: cout << endl;
                break;
                case 1:
                    yrs[2] = 7;
                    anInt[2] = 5.35;
                    cout << "You have selected a 7 year mortgage at 5.35% interest." << endl;
                    break;

                case 2:
                    yrs[3] = 15;
                    anInt[3]= 5.50;
                    cout << "You have selected 15 year mortgage at 5.50 interest%" << endl;
                    break;

                case 3:
                    yrs[4] = 30;
                    anInt[4] = 5.75;
                    cout << "You have selected a 30 year mortgage at 5.75% interest" << endl;
                    break;

                default:
                    cout << "Invalid Line Number" << endl;
                    if ((select < 1)|| (select > 3))

                    {
                        system("cls");
                        cout << "Your choice is invalid, please enter a valid choice!!!";
                        system("PAUSE");
                        system("cls");
                        return main();
                    }
            }
            }
               
//Formulas//
double pmt = (mortgageTotal.princ*((anInt[select]/1200)/(1 - pow((1+(anInt[select]/1200)),-1*(yrs[select]*12))))); //  notice the variable in the brackets that i added.

    cout << "Your Monthly Payment is: $" << pmt << "\n";
    cout << endl;

double NOP = yrs [select] * 12;
    SL = 0;
        for (Mnth = 1; Mnth <= NOP; ++Mnth)
        {

    intPd = mortgageTotal.princ * (anInt[select] / 1200);  
    amtPd = pmt - intPd;
    bal = mortgageTotal.princ - amtPd;
        if (bal < 0)bal = 0;
            mortgageTotal.princ = bal;

            if (SL == 0)
            {
                cout << "Balance of Loan" << "\t\t\tAmount of Interest Paid" << endl;
            }

                cout << setprecision(2) << fixed << "$" << setw(5) << bal << "\t\t\t\t$"<< setw(5) << intPd << endl;
                ++SL;

//Allows user to decide whether or not to continue//

    if (SL == 12)
    {
        cout << "Would you like to continue the amoritization or quit the program?\n";
        cout << endl;
        cout << "Enter 'R' to see the remainder or 'D' for done.\n";
        cin >> indicator;

//your checks are incorrect, needs to be in each conditional
            if (indicator == 'R','r')SL = 0;
             //do nothing cause it will continue the program

//your checks are incorrect, needs to be in each conditional
            if (indicator == 'D','d') {
                cout << endl;
                break;
            }

    }//end if from above?
}//end for?? you indentation is screwed up
} while ((indicator != 'D') && (indicator != 'd'));



See code comments

Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,234
  • Joined: 14-September 07

Re: New problem: looping to beginning of program

Posted 07 December 2008 - 03:22 PM

Problem: indicator is an array so you need to either make it a char and determine tests that way or use array indexes. Or use a boolean (my recommendation).


edited for typos

This post has been edited by KYA: 07 December 2008 - 03:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 Linkowiezi   User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 316
  • Joined: 07-October 08

Re: New problem: looping to beginning of program

Posted 07 December 2008 - 03:33 PM

I have added a coupple of lines and also fixed a little of your messed up indentation:
Se code for the lines added to it(7 total):
#include "math.h"
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

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

//mortgage class//
class Mortgage

{
    public:
    void header();
    void enterprinc();
    double princ;
    double anInt;
    int yrs;
};

//define function//

void Mortgage::header()
{

    cout << "Welcome to Smith's Amortization Calculator\n\n";
    cout << endl;
    cout << "Week 3 Individual Assignment C++ Mortgage Amortization Calculator\n\n";
    cout << endl;

}

void Mortgage::enterprinc()
{

    cout << endl;
    cout << "Enter the amount The Mortgage Amount:$";
    cin >> princ;

}

//main function//
int main ()
{
  
  Mortgage mortgageTotal;
  
  double princ = 0;
  double anInt [4]= {0, 5.35, 5.5, 5.75,};
  double yrs [4]= {0, 7, 15, 30,};
  double pmt = 0;
  double intPd = 0;
  double bal = 0;
  double amtPd = 0;
  double check(int min, int max);
  int NOP = 0;
  int Mnth = 0;
  int SL = 0;
  char indicator = ('A' ,'a','R','r','D','d');
  int select;  
  
  //Begin Loop
  bool finished = false;  //  Added this line
  while( !finished )      //  And this line
  {
    mortgageTotal.header ();
    mortgageTotal.enterprinc ();
    
    cout<< "Please Enter Your Selection!"<< endl;
    cout<< "Press A to make selection for term of mortgage and interest rate"<<endl;
    cin >> indicator;
    
    if (indicator == 'A','a')
    {
      cout << "Please select your Terms and Rates from the following choices: " << endl;
      cout << "1. 7 years at 5.35%" << endl;
      cout << "2. 15 years at 5.5%" << endl;
      cout << "3. 30 years at 5.75%" << endl;
      cout << endl;
      cout << "What is your selection:";
      
      select = 0;
      cin >> select;
      cout << endl << endl << endl;
      switch (select)
      {
        case 0: cout << endl;
        break;
        case 1:
        yrs[2] = 7;
        anInt[2] = 5.35;
        cout << "You have selected a 7 year mortgage at 5.35% interest." << endl;
        break;
        
        case 2:
        yrs[3] = 15;
        anInt[3]= 5.50;
        cout << "You have selected 15 year mortgage at 5.50 interest%" << endl;
        break;
        
        case 3:
        yrs[4] = 30;
        anInt[4] = 5.75;
        cout << "You have selected a 30 year mortgage at 5.75% interest" << endl;
        break;
        
        default:
        cout << "Invalid Line Number" << endl;
        if ((select < 1)|| (select > 3))
        {
          system("cls");
          cout << "Your choice is invalid, please enter a valid choice!!!";
          system("PAUSE");
          system("cls");
          return main();
        }
      }
    }
    
    //Formulas//
    double pmt = (mortgageTotal.princ*((anInt[select]/1200)/(1 - pow((1+(anInt[select]/1200)),-1*(yrs[select]*12))))); //  notice the variable in the brackets that i added.
    
    cout << "Your Monthly Payment is: $" << pmt << "\n";
    cout << endl;
    
    double NOP = yrs [select] * 12;
    SL = 0;
    for (Mnth = 1; Mnth <= NOP; ++Mnth)
    {
      intPd = mortgageTotal.princ * (anInt[select] / 1200);  
      amtPd = pmt - intPd;
      bal = mortgageTotal.princ - amtPd;
      if (bal < 0)bal = 0;
      mortgageTotal.princ = bal;
      
      if (SL == 0)
      {
        cout << "Balance of Loan" << "\t\t\tAmount of Interest Paid" << endl;
      }
      
      cout << setprecision(2) << fixed << "$" << setw(5) << bal << "\t\t\t\t$"<< setw(5) << intPd << endl;
      ++SL;
      
      //Allows user to decide whether or not to continue//
      
      if (SL == 12)
      {
        cout << "Would you like to continue the amoritization or quit the program?\n";
        cout << endl;
        cout << "Enter 'R' to see the remainder or 'D' for done.\n";
        cin >> indicator;
        
        if(indicator == 'R','r')
          SL = 0;
        else if(indicator == 'D','d')
          cout << endl;
      }
    }
//  Added the lines below to the next comment
    char tempChar;
    cout << "Do you wish to loop trough the code again? [Y/N]: ";
    cin >> tempChar;
    if( tempChar == 'N' || tempChar == 'n' )
      finished = true;
//  Added the lines above to the previous comment
  }
  return 0;
}


EDIT1:
Oh and you also seem to have a bunch of unused variables in your code.
Try compiling with warnings.
Hope things work out for ya ;)

EDIT2:
Btw, didn't really read trough all your code, just tossed in the while statement.
Wondering, is it the indicator variable that's gonna be used for termination?
If, then just use my code and toss in the finished = true to that and maybe better... a break or even return statement so you won't run some mystic loop that you might have build in somewhere.

This post has been edited by Linkowiezi: 07 December 2008 - 03:39 PM

Was This Post Helpful? 0
  • +
  • -

#6 mistymoon1966   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Re: New problem: looping to beginning of program

Posted 07 December 2008 - 04:12 PM

Thank you very much I really appreciate it. Yes, I wanted to use the indicator to loop the program back to the beginning. I also changed some other things in my code and just wanted to thank u so much. I really appreciate it.....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1