I cannot figure out why my program gets the wrong output but I am assuming it has something to do with the array or the switch statement. Can someone guide me in the right direction? I am at a point where I am making no progress right now and now ask for help. Thanks in advance.

/*
Write the program as a procedural C++ program. Allow the user to input 
the amount of a mortgage and then select from a menu of mortgage loans: 

7 year at 5.35% 
15 year at 5.5% 
30 year at 5.75% 

Use an array for the different loans. Display the mortgage payment amount. 
Then, list the loan balance and interest paid for each payment over the term 
of the loan. On longer-term loans, the list will scroll off the screen. 
Do not allow the list to scroll off the screen, but rather display a partial 
list and then allow the user to continue the list. Allow the user to loop back 
and enter new data or quit. Insert comments in the program to document the 
program.
*/

#include <math.h>                // Math Constant
#include <cstdlib>               // Standard Library
#include <iostream>              // Defines facilities for basic I/O operations
using namespace std;             // Namespace standard library

int main(int argc, char *argv[]) // Main, argc and *argv give the number and value of the program's command-line arguments
{
    cout << "   " << endl;       // Empty line before assignment title
    cout << "   ///////////////////////////////////////////////////////////////////////// " << endl;
    cout << "   //                                                                     // " << endl;
    cout << "   //  Calculate and display the mortgage payment amount using the amount // " << endl;
    cout << "   //  of the mortgage, the term of the mortgage, and the interest rate   // " << endl;
    cout << "   //  of the mortgage. The user will enter the amount of the mortgage    // " << endl;
    cout << "   //  and will select from a menu the amount of years and interest rate. // " << endl;
    cout << "   //  The mortgage payment will display and the ammortization table will // " << endl;
    cout << "   //  display the loan balance and interest paid. The user will be       // " << endl;
    cout << "   //  allowed to either continue the ammortization table, enter new      // " << endl;
    cout << "   //  loan data or quit the program altogether.                          // " << endl;
    cout << "   //                                                                     // " << endl;
    cout << "   ///////////////////////////////////////////////////////////////////////// " << endl;
    cout << "   " << endl << endl << endl << endl << endl << endl;

    double loanAmount,           // Amount of the Loan
           monthlyPayment,       // Monthly Payment of the Loan
           loanBalance,          // Balance of the Loan
           intPaid,              // Interest Paid on the Loan
           amountPaid,           // Amount Paid on the Loan
           intRate[3]={5.25, 
                       5.50, 
                       5.75};    // Interest Rate of the Loan

    int numPayments,             // Number of Payments 
        paymentCounter, 
        dividelist = 0,          // Display partial list
        length[3]={7, 15, 30};   // Length of the Loan

    char listmore;
    char quit;                   // Prompt user to quit
         quit = 'C';             // Quit variable

    while (quit != 'Q' && quit != 'q')
        {
            cout << "\t       Enter Loan Amount (*NUMBERS ONLY*): ";
            cin >> loanAmount;
            cout << "\t       Select Line Number for Length of Loan and Interest Rate  " << endl;
            cout << "\t       1.  7 Years at 5.35% " << endl; 
            cout << "\t       2. 15 Years at 5.50% " << endl;  
            cout << "\t       3. 30 Years at 5.75% " << endl;
            cout << "\t       Enter Line Number Here: ";

            int choice;
            cin >> choice;
            cout << endl;

            int i;
            for (i = 0; i < 1; i++)
                switch (choice)
                {
                       case 1: 
                            cout << "\t       You Selected  7 Years at 5.35%" << endl; 
                            break;
                       case 2: 
                            cout << "\t       You Selected 15 Years at 5.50%" << endl; 
                            break;
                       case 3: 
                            cout << "\t       You Selected 30 Years at 5.75%" << endl; 
                            break;
                       default: 
                            cout << "\t       Invalid Line Number" << endl;
                            // Still having minor issues with invalid number
                            if (choice != 1,2,3)
                            {
                            cout << "\t       Enter Line Number Here: ";
                            int choice;
                            cin >> choice;
                            cout << endl; 
                            if (choice != 1,2,3)
                               {
                               cout << "\t       You cannot follow directions. " << endl; 
                               case 9:
                               cout << "\t       Program will now END " << endl;
                               break; 
                               }
                            }
                }

            cout << endl;

            monthlyPayment = loanAmount * (intRate[choice] / 1200) /
                             (1 - pow(1 + (intRate[choice] / 1200), - 1 * (length[choice] * 12)));

            cout << "\t       Monthly Payments: $" << monthlyPayment << endl << endl;
            numPayments = length[choice] * 12;
            dividelist = 0;

            for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter)
                {
                    // Math Formula for intPaid to set up value for amountPaid
                    intPaid = loanAmount * (intRate[choice] / 1200);
                    amountPaid = monthlyPayment - intPaid;
                    loanBalance = loanAmount - amountPaid;

                    if (loanBalance < 0)loanBalance = 0;
                    loanAmount = loanBalance;

                    if (dividelist == 0)
                        {
                            cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl;
                            cout << "\t\t____________" << "\t\t_____________" << endl << endl;
                        }

                    cout << "\t\t$" << loanBalance << "\t\t\t$" << intPaid << endl;
                    ++dividelist;

                    // User to Continue, enter New Data or Quit
                    if (dividelist == 12)
                        {
                            cout << endl << endl;
                            cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> ";
                            cin >> listmore;
                            if ((listmore == 'C')||(listmore == 'c'))dividelist = 0;
                            else if ((listmore == 'N')||(listmore == 'n'))
                                break; // Halt Current Program if New Data is entered, verify and restart
                            else if ((listmore == 'Q')||(listmore == 'q'))
                                return 0; // End Program
                        }
                }

            do      // This loops valididates user input to enter New Data
                {
                    cout << "\tEnter 'C' to Continue, 'Q' to Quit> ";
                    cin  >> quit;
                    cout << "\n";
                }

            while ((quit != 'q') &&
                   (quit != 'Q') &&
                   (quit != 'c') &&
                   (quit != 'C'));
        }

    cout << endl;
    system("PAUSE");             // Pause program prior to exit
    return EXIT_SUCCESS;         // Exit
}

Edited 3 Years Ago by pyTony: fixed formating

Comments
thanks for learning how to use code tags

Who knows how much, if anything this will help, but the user can select a value for a variable called choice which can range from 1-3 and is used as an index in an array that has three elements which can have indexe ranging from 0-2, so when user selects 3 it causes an out of bounds problem when used as an index.

I understand what you are saying. I noticed that when I enter 2 for the 15yrs 5.50%, I get the result for 30yrs and 5.75%. I tried to modify this switch to show case 0 but that did not seem to work. At least now it displays that I entered in what line I intended, it is just that I am still getting the result for loan #3. WHen I enter loan #3, I get the $1.#INF as a monthly payment. I really am not making much progress on this.

/*
Write the program as a procedural C++ program. Allow the user to input 
the amount of a mortgage and then select from a menu of mortgage loans: 

7 year at 5.35% 
15 year at 5.5% 
30 year at 5.75% 

Use an array for the different loans. Display the mortgage payment amount. 
Then, list the loan balance and interest paid for each payment over the term 
of the loan. On longer-term loans, the list will scroll off the screen. 
Do not allow the list to scroll off the screen, but rather display a partial 
list and then allow the user to continue the list. Allow the user to loop back 
and enter new data or quit. Insert comments in the program to document the 
program.
*/

#include <math.h>                // Math Constant
#include <cstdlib>               // Standard Library
#include <iostream>              // Defines facilities for basic I/O operations
using namespace std;             // Namespace standard library

int main(int argc, char *argv[]) // Main, argc and *argv give the number and value of the program's command-line arguments
{
    cout << "   " << endl;       // Empty line before assignment title
    cout << "   ///////////////////////////////////////////////////////////////////////// " << endl;
    cout << "   //                                                                     // " << endl;
    cout << "   //  Calculate and display the mortgage payment amount using the amount // " << endl;
    cout << "   //  of the mortgage, the term of the mortgage, and the interest rate   // " << endl;
    cout << "   //  of the mortgage. The user will enter the amount of the mortgage    // " << endl;
    cout << "   //  and will select from a menu the amount of years and interest rate. // " << endl;
    cout << "   //  The mortgage payment will display and the ammortization table will // " << endl;
    cout << "   //  display the loan balance and interest paid. The user will be       // " << endl;
    cout << "   //  allowed to either continue the ammortization table, enter new      // " << endl;
    cout << "   //  loan data or quit the program altogether.                          // " << endl;
    cout << "   //                                                                     // " << endl;
    cout << "   ///////////////////////////////////////////////////////////////////////// " << endl;
    cout << "   " << endl << endl << endl << endl << endl << endl;

    double loanAmount,           // Amount of the Loan
           monthlyPayment,       // Monthly Payment of the Loan
           loanBalance,          // Balance of the Loan
           intPaid,              // Interest Paid on the Loan
           amountPaid,           // Amount Paid on the Loan
           intRate[3] = {5.25, 
                         5.50, 
                         5.75};  // Interest Rate of the Loan

    int numPayments,             // Number of Payments 
        paymentCounter, 
        dividelist = 0,          // Display partial list
        length[3] = {7, 15, 30};   // Length of the Loan

    char listmore;
    char quit;                   // Prompt user to quit
         quit = 'C';             // Quit variable

    while (quit != 'Q' && quit != 'q')
        {
            cout << "\t       Enter Loan Amount (*NUMBERS ONLY*): ";
            cin >> loanAmount;
            cout << "\t       Select Line Number for Length of Loan and Interest Rate  " << endl;
            cout << "\t       1.  7 Years at 5.35% " << endl; 
            cout << "\t       2. 15 Years at 5.50% " << endl;  
            cout << "\t       3. 30 Years at 5.75% " << endl;
            cout << "\t       Enter Line Number Here: ";
            
            int choice;
            cin >> choice;
            cout << endl;
            
            int i;
            for (i = 0; i < 1; i++)
                switch (choice)
                {
                       case 0: 
                            cout << endl; 
                            break;
                       case 1: 
                            cout << "\t       You Selected  7 Years at 5.35%" << endl; 
                            break;
                       case 2: 
                            cout << "\t       You Selected 15 Years at 5.50%" << endl; 
                            break;
                       case 3: 
                            cout << "\t       You Selected 30 Years at 5.75%" << endl; 
                            break;
                       default: 
                            cout << "\t       Invalid Line Number" << endl;
                            /*
                            // Still having minor issues with invalid number
                            if (choice != 1,2,3)
                            {
                            cout << "\t       Enter Line Number Here: ";
                            int choice;
                            cin >> choice;
                            cout << endl; 
                            if (choice != 1,2,3)
                               {
                               cout << "\t       You cannot follow directions. " << endl; 
                               case 9:
                               cout << "\t       Program will now END " << endl;
                               break; 
                               }
                            }*/
                }
                   
            cout << endl;

            monthlyPayment = loanAmount * (intRate[choice] / 1200) /
                             (1 - pow(1 + (intRate[choice] / 1200), - 1 * (length[choice] * 12)));

            cout << "\t       Monthly Payments: $" << monthlyPayment << endl << endl;
            double numPayments = length[choice] * 12;
            dividelist = 0;

            for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter)
                {
                    // Math Formula for intPaid to set up value for amountPaid
                    intPaid = loanAmount * (intRate[choice] / 1200);
                    amountPaid = monthlyPayment - intPaid;
                    loanBalance = loanAmount - amountPaid;

                    if (loanBalance < 0)loanBalance = 0;
                    loanAmount = loanBalance;

                    if (dividelist == 0)
                        {
                            cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl;
                            cout << "\t\t____________" << "\t\t_____________" << endl << endl;
                        }

                    cout << "\t\t$" << loanBalance << "\t\t\t$" << intPaid << endl;
                    ++dividelist;

                    // User to Continue, enter New Data or Quit
                    if (dividelist == 12)
                        {
                            cout << endl << endl;
                            cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> ";
                            cin >> listmore;
                            if ((listmore == 'C')||(listmore == 'c'))dividelist = 0;
                            else if ((listmore == 'N')||(listmore == 'n'))
                                break; // Halt Current Program if New Data is entered, verify and restart
                            else if ((listmore == 'Q')||(listmore == 'q'))
                                return 0; // End Program
                        }
                }

            do      // This loops valididates user input to enter New Data
                {
                    cout << "\tEnter 'C' to Continue, 'Q' to Quit> ";
                    cin  >> quit;
                    cout << "\n";
                }
                
            while ((quit != 'q') &&
                   (quit != 'Q') &&
                   (quit != 'c') &&
                   (quit != 'C'));
        }
        
    cout << endl;
    system("PAUSE");             // Pause program prior to exit
    return EXIT_SUCCESS;         // Exit
}

Allow the user to select choice based on 1, 2, or 3 but when using choice as the index to an array, then use choice - 1 as the index, not choice itself: length[choice - 1] or intRate[choice - 1].

I figured it out!!! OMG! Finally! Now to beautify the output

#include <math.h>                // Math Constant
#include <cstdlib>               // Standard Library
#include <iostream>              // Defines facilities for basic I/O operations
using namespace std;             // Namespace standard library

int main(int argc, char *argv[]) // Main, argc and *argv give the number and value of the program's command-line arguments
{
    cout << "   " << endl;       // Empty line before assignment title
    cout << "   ///////////////////////////////////////////////////////////////////////// " << endl;
    cout << "   //                                                                     // " << endl;
    cout << "   //  Calculate and display the mortgage payment amount using the amount // " << endl;
    cout << "   //  of the mortgage, the term of the mortgage, and the interest rate   // " << endl;
    cout << "   //  of the mortgage. The user will enter the amount of the mortgage    // " << endl;
    cout << "   //  and will select from a menu the amount of years and interest rate. // " << endl;
    cout << "   //  The mortgage payment will display and the ammortization table will // " << endl;
    cout << "   //  display the loan balance and interest paid. The user will be       // " << endl;
    cout << "   //  allowed to either continue the ammortization table, enter new      // " << endl;
    cout << "   //  loan data or quit the program altogether.                          // " << endl;
    cout << "   //                                                                     // " << endl;
    cout << "   ///////////////////////////////////////////////////////////////////////// " << endl;
    cout << "   " << endl << endl << endl << endl << endl << endl;

    double loanAmount,           // Amount of the Loan
           monthlyPayment,       // Monthly Payment of the Loan
           loanBalance,          // Balance of the Loan
           intPaid,              // Interest Paid on the Loan
           amountPaid,           // Amount Paid on the Loan
           intRate[3] = {5.25, 
                         5.50, 
                         5.75};  // Interest Rate of the Loan

    int numPayments,             // Number of Payments 
        paymentCounter, 
        dividelist = 0,          // Display partial list
        length[3] = {7, 15, 30};   // Length of the Loan

    char listmore;
    char quit;                   // Prompt user to quit
         quit = 'C';             // Quit variable

    while (quit != 'Q' && quit != 'q')
        {
            cout << "\t       Enter Loan Amount (*NUMBERS ONLY*): ";
            cin >> loanAmount;
            cout << "\t       Select Line Number for Length of Loan and Interest Rate  " << endl;
            cout << "\t       1.  7 Years at 5.35% " << endl; 
            cout << "\t       2. 15 Years at 5.50% " << endl;  
            cout << "\t       3. 30 Years at 5.75% " << endl;
            cout << "\t       Enter Line Number Here: ";
            
            int choice;
            cin >> choice;
            cout << endl;
            
            int i;
            for (i = 0; i < 1; i++)
                switch (choice)
                {
                       case 0: cout << endl; 
                            break;
                       case 1:
                            length[1] = 7;
                            intRate[1] = 5.35; 
                            cout << "\t       You Selected  7 Years at 5.35%" << endl; 
                            break;
                       case 2: 
                            length[2] = 15;
                            intRate[2]= 5.50; 
                            cout << "\t       You Selected 15 Years at 5.50%" << endl; 
                            break;
                       case 3: 
                            length[3] = 30;
                            intRate[3] = 5.75; 
                            cout << "\t       You Selected 30 Years at 5.75%" << endl; 
                            break;
                       default: 
                            cout << "\t       Invalid Line Number" << endl;
                            /*
                            // Still having minor issues with invalid number
                            if (choice != 1,2,3)
                            {
                            cout << "\t       Enter Line Number Here: ";
                            int choice;
                            cin >> choice;
                            cout << endl; 
                            if (choice != 1,2,3)
                               {
                               cout << "\t       You cannot follow directions. " << endl; 
                               case 9:
                               cout << "\t       Program will now END " << endl;
                               break; 
                               }
                            }*/
                }
                   
            cout << endl;

            monthlyPayment = loanAmount * (intRate[choice] / 1200) /
                             (1 - pow(1 + (intRate[choice] / 1200), - 1 * (length[choice] * 12)));

            cout << "\t       Monthly Payments: $" << monthlyPayment << endl << endl;
            double numPayments = length[choice] * 12;
            dividelist = 0;

            for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter)
                {
                    // Math Formula for intPaid to set up value for amountPaid
                    intPaid = loanAmount * (intRate[choice] / 1200);
                    amountPaid = monthlyPayment - intPaid;
                    loanBalance = loanAmount - amountPaid;

                    if (loanBalance < 0)loanBalance = 0;
                    loanAmount = loanBalance;

                    if (dividelist == 0)
                        {
                            cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl;
                            cout << "\t\t____________" << "\t\t_____________" << endl << endl;
                        }

                    cout << "\t\t$" << loanBalance << "\t\t\t$" << intPaid << endl;
                    ++dividelist;

                    // User to Continue, enter New Data or Quit
                    if (dividelist == 12)
                        {
                            cout << endl << endl;
                            cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> ";
                            cin >> listmore;
                            if ((listmore == 'C')||(listmore == 'c'))dividelist = 0;
                            else if ((listmore == 'N')||(listmore == 'n'))
                                break; // Halt Current Program if New Data is entered, verify and restart
                            else if ((listmore == 'Q')||(listmore == 'q'))
                                return 0; // End Program
                        }
                }

            do      // This loops valididates user input to enter New Data
                {
                    cout << "\tEnter 'C' to Continue, 'Q' to Quit> ";
                    cin  >> quit;
                    cout << "\n";
                }
                
            while ((quit != 'q') &&
                   (quit != 'Q') &&
                   (quit != 'c') &&
                   (quit != 'C'));
        }
        
    cout << endl;
    system("PAUSE");             // Pause program prior to exit
    return EXIT_SUCCESS;         // Exit
}

Even though I really did not get any help, I will post my final results of my code which includes a nicely formated output and corrected switch/case and arrays.

#include <cmath>                 // Math Constant
#include <cstdlib>               // Standard Library
#include <iostream>              // Defines facilities for basic I/O operations
#include <iomanip>               // Defines facilities for basic I/O operations 
                                 // cout << fixed << showpoint << setprecision(2)
using namespace std;             // Namespace standard library

int main(int argc, char *argv[]) // Main, argc and *argv give the number and value of the program's command-line arguments
{
    cout << "   " << endl;       // Empty line before assignment title
    cout << "   ///////////////////////////////////////////////////////////////////////// " << endl;
    cout << "   //                                                                     // " << endl;
    cout << "   //  Calculate and display the mortgage payment amount using the amount // " << endl;
    cout << "   //  of the mortgage, the term of the mortgage, and the interest rate   // " << endl;
    cout << "   //  of the mortgage. The user will enter the amount of the mortgage    // " << endl;
    cout << "   //  and will select from a menu the amount of years and interest rate. // " << endl;
    cout << "   //  The mortgage payment will display and the ammortization table will // " << endl;
    cout << "   //  display the loan balance and interest paid. The user will be       // " << endl;
    cout << "   //  allowed to either continue the ammortization table, enter new      // " << endl;
    cout << "   //  loan data or quit the program altogether.                          // " << endl;
    cout << "   //                                                                     // " << endl;
    cout << "   ///////////////////////////////////////////////////////////////////////// " << endl;
    cout << "   " << endl << endl << endl << endl << endl << endl;

    double loanAmount,           // Amount of the Loan
           monthlyPayment,       // Monthly Payment of the Loan
           loanBalance,          // Balance of the Loan
           intPaid,              // Interest Paid on the Loan
           amountPaid,           // Amount Paid on the Loan
           intRate[3] = {5.25, 
                         5.50, 
                         5.75};  // Interest Rate of the Loan

    int numPayments,             // Number of Payments 
        paymentCounter, 
        dividelist = 0,          // Display partial list
        length[3] = {7, 15, 30}; // Length of the Loan

    char listmore;
    char quit;                   // Prompt user to quit
         quit = 'C';             // Quit variable

    while (quit != 'Q' && quit != 'q')
        {
            cout << "\t       Enter Loan Amount (*NUMBERS ONLY*): ";
            cin >> loanAmount;
            cout << "\t       Select Line Number for Length of Loan and Interest Rate  " << endl;
            cout << "\t       1.  7 Years at 5.35% " << endl; 
            cout << "\t       2. 15 Years at 5.50% " << endl;  
            cout << "\t       3. 30 Years at 5.75% " << endl;
            cout << "\t       Enter Line Number Here: ";
            
            int choice;
            cin >> choice;
            cout << endl << endl << endl;
            
            int i;
            for (i = 0; i < 1; i++)
                switch (choice)
                {
                       case 0: cout << endl; 
                            break;
                       case 1:
                            length[1] = 7;
                            intRate[1] = 5.35; 
                            cout << "\t       You Selected 7 Years and 5.35%" << endl; 
                            break;
                       case 2: 
                            length[2] = 15;
                            intRate[2]= 5.50; 
                            cout << "\t       You Selected 15 Years at 5.50%" << endl; 
                            break;
                       case 3: 
                            length[3] = 30;
                            intRate[3] = 5.75; 
                            cout << "\t       You Selected 30 Years at 5.75%" << endl; 
                            break;
                       default: 
                            cout << "\t       Invalid Line Number" << endl;
                }
                   
            cout << endl;

            monthlyPayment = loanAmount * (intRate[choice] / 1200) /
                             (1 - pow(1 + (intRate[choice] / 1200), - 1 * (length[choice] * 12)));

            cout << "\t       Monthly Payments: $" << monthlyPayment << endl << endl;
            double numPayments = length[choice] * 12;
            dividelist = 0;

            for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter)
                {
                                // Math Formula for intPaid to set up value for amountPaid
                    intPaid = loanAmount * (intRate[choice] / 1200);
                    amountPaid = monthlyPayment - intPaid;
                    loanBalance = loanAmount - amountPaid;

                    if (loanBalance < 0)loanBalance = 0;
                    loanAmount = loanBalance;

                    if (dividelist == 0)
                        {
                            cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl;
                            cout << "\t\t____________" << "\t\t_____________" << endl << endl;
                        }
                    
                    cout << fixed << showpoint << setprecision(2) // Sets the number of digits printed to the right of the decimal point. 
                         << "\t\t$" << loanBalance 
                         << "\t\t$" << intPaid << endl;
                    ++dividelist;

                                 // User to Continue, enter New Data or Quit
                    if (dividelist == 12)
                        {
                            cout << endl << endl;
                            cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> ";
                            cin >> listmore;
                            if ((listmore == 'C')||(listmore == 'c'))dividelist = 0;
                            else if ((listmore == 'N')||(listmore == 'n'))
                                break; // Halt Current Program if New Data is entered, verify and restart
                            else if ((listmore == 'Q')||(listmore == 'q'))
                                return 0; // End Program
                        }
                }

            do                  // This loops valididates user input to enter New Data
                {
                    cout << "\tEnter 'C' to Continue, 'Q' to Quit> ";
                    cin  >> quit;
                    cout << "\n";
                }
                
            while ((quit != 'q') &&
                   (quit != 'Q') &&
                   (quit != 'c') &&
                   (quit != 'C'));
        }
        
    cout << endl;
    system("PAUSE");             // Pause program prior to exit
    return EXIT_SUCCESS;         // Exit
}
This article has been dead for over six months. Start a new discussion instead.