1

When I run the program with either one or more customers. It seems to be that Only with the first customer data, the code I have does not do the calculations correctly for the first customer when trying to get the monthly payments and interest but the calculations are done correctly for the rest of the customers data inputted. What am I missing? Thank you.

#include <iostream>
#include <string> 
#include <cmath>
#include <iomanip>  
#include <vector>
using namespace std;



int main()
{
   //Variables
   vector <double> Loanlgth, Loanamt, interestRate;
   vector <double> monthlyPay(100), loanTotal(100), creditScore(100), totalInterest;
   char yes;
   const int INPUT_CUSTOMER = 1, DISPLAY_LOAN = 2, EXIT_PROGRAM = 3;
   vector <string> customerName;
   int option;
   int numCustomers = 0;
   int index = 0;
   //Program
   cout << "Thank you for choosing The Bank of UA for your loan requirements!\n\n";
   do
   {
        cout << "UA Bank menu:\n\n"
        << "1. Enter your information\n"
        << "2. See your loan requirements\n"
        << "3. Exit program\n\n"
        << "Choose an option: ";
        cin >> option;

        while (option < INPUT_CUSTOMER || option > EXIT_PROGRAM)
        {
            cout << "Please enter a valid menu option: ";
            cin >> option;
        }

        if (option == 1) //Customer enters their information
        {
            cout << "Please enter the number of customers you would like\n"
               << " to enter loan information for: ";
            cin >> numCustomers;

            for (index = 0; index < numCustomers; index++)      
            {
                string tempName;
                double tempLoanamt, tempLoanlgth, tempcreditScore, tempinterestRate, tempinterest;

                cout << "Please enter your name: ";
                cin >> tempName;
                customerName.push_back(tempName);
                cout << "Please enter the loan amount: $";
                cin >> tempLoanamt;
                Loanamt.push_back(tempLoanamt);
                cout << "Please enter the length of the loan in months: ";
                cin >> tempLoanlgth;
                Loanlgth.push_back(tempLoanlgth);
                cout << "What is your current credit score? ";
                cin >> tempcreditScore;
                creditScore.push_back(tempcreditScore);

                if (tempcreditScore <= 600)
                    tempinterestRate = .12;
                interestRate.push_back(tempinterestRate);
                if (tempcreditScore > 600)
                    tempinterestRate = .05;
                interestRate.push_back(tempinterestRate);

                //Calculations
                tempinterest = Loanamt[index] * interestRate[index];
                totalInterest.push_back(tempinterest);
                loanTotal[index] = (Loanamt[index] + totalInterest[index]);
                monthlyPay[index] = loanTotal[index] / Loanlgth[index];
            }
        }
        if (option == 2) // Displays monthly payment
        {
            cout << fixed << setprecision(2);
            for (index = 0; index < numCustomers; index++)
                cout << customerName[index] << " your total loan is " << loanTotal[index] << "\n"
                   << "with a monthly payment of $" << monthlyPay[index] << "\n"
                    << "for " << Loanlgth[index] << " months with an interest\n"
                    << "rate of " << interestRate[index] << endl;
        }

    } while (option != EXIT_PROGRAM);

    system("pause");

    return 0;
}
kuro
  • 3,214
  • 3
  • 15
  • 31
  • 1
    I think the `interestRate` vector is being populated twice for every customer. You may have wanted to put the `push_back()` inside the `if` blocks. – kuro Apr 04 '20 at 04:16
  • 2
    Recommendation: instead of many `vector`s all with a single piece of customer data, aggregate all of the customer data into a structure and have one `vector` of that structure. This makes it MUCH easier to keep track of. – user4581301 Apr 04 '20 at 04:18
  • I am taking a beginner programming class and we are not at the structure part. I have to work the code I currently have. – Fernando Avila Apr 04 '20 at 04:41

1 Answers1

1

Currently, interestRate is populated wrongly. Initially, it contains a garbage value because it is not initialized and if the first condition is not true, the garbage value is pushed, otherwise .12. Next, if the second condition is true, .05 is pushed, otherwise the values from the above flow. So, these combinations of garbage and assigned values are causing values to be pushed twice.

Here's your code:

if (tempcreditScore <= 600)
    tempinterestRate = .12;
interestRate.push_back(tempinterestRate);

if (tempcreditScore > 600)
    tempinterestRate = .05;
interestRate.push_back(tempinterestRate);

You can correct this in a number of ways:

// push after the calculation is complete

if (tempcreditScore <= 600)
    tempinterestRate = .12;

if (tempcreditScore > 600)
    tempinterestRate = .05;

interestRate.push_back(tempinterestRate);

or, with if-else (preferable):

if (tempcreditScore <= 600)
    tempinterestRate = .12;
else
    tempinterestRate = .05;

interestRate.push_back(tempinterestRate);

or, with ternary operator ?: (conciseness and you can make it const):

const double tempinterestRate = (tempcreditScore <= 600 ? .12 : .05);

interestRate.push_back(tempinterestRate);

Apart from this, there are a number of points:

  • The naming convention is inconsistent throughout the code.
  • The variables must be initialized because the uninitialized ones may lead to Undefined Behavior.
  • In the absence of an aggregate type such as struct or class and with multiple pieces of information to store separately in vectors, it is better to keep all the push_backs exactly to once per iteration.
  • Magic numbers can be avoided for 600, .12 and .5.
  • The magic numbers for option comparison in if conditions can be removed with their proper constant equivalents i.e. INPUT_CUSTOMER and DISPLAY_LOAN. And, if-else can be used instead of if-if.
  • The scoping could be improved by moving the variables and objects closer to where they are used.
  • The vertical blank spaces can improve readability for relevant code blocks.
  • And, Why is "using namespace std;" considered bad practice?

There might be some other points that you can observe in the following updated code (live):

#include <iostream>
#include <string> 
#include <iomanip>  
#include <vector>

int main()
{
    // Variables
    std::vector<std::string> customerNames;
    std::vector<double> loanLengths, loanAmounts, interestRates;
    std::vector<double> monthlyPays, loanTotals, creditScores, totalInterests;

    // Program
    std::cout << "Thank you for choosing The Bank of UA for your loan requirements!\n\n";

    const int INPUT_CUSTOMER = 1, DISPLAY_LOAN = 2, EXIT_PROGRAM = 3;
    int option = EXIT_PROGRAM;

    do
    {
        std::cout << "UA Bank menu:\n\n"
                  << "1. Enter your information\n"
                  << "2. See your loan requirements\n"
                  << "3. Exit program\n\n"
                  << "Choose an option: ";
        std::cin >> option;

        while (option < INPUT_CUSTOMER || option > EXIT_PROGRAM)
        {
            std::cout << "Please enter a valid menu option: ";
            std::cin >> option;
        }

        if (option == INPUT_CUSTOMER) //Customer enters their information
        {
            int numCustomers = 0;
            std::cout << "Please enter the number of customers you would like\n"
                      << " to enter loan information for: ";
            std::cin >> numCustomers;

            for ( int index = 0; index < numCustomers; index++ )
            {
                std::string name;
                double loanAmount = 0.0, loanLength = 0.0, creditScore = 0.0;

                std::cout << "Please enter your name: ";
                std::cin >> name;
                customerNames.push_back( name );

                std::cout << "Please enter the loan amount: $";
                std::cin >> loanAmount;
                loanAmounts.push_back( loanAmount );

                std::cout << "Please enter the length of the loan in months: ";
                std::cin >> loanLength;
                loanLengths.push_back( loanLength );

                std::cout << "What is your current credit score? ";
                std::cin >> creditScore;
                creditScores.push_back( creditScore );

                double interestRate = 0.0;
                if (creditScore <= 600)
                    interestRate = .12;
                else
                    interestRate = .05;

                // Ternary operator (?:) may also be used here
                // const double interestRate = creditScore <= 600 ? .12 : .05;

                interestRates.push_back(interestRate);

                //Calculations
                const double tempTotalInterest = loanAmounts[index] * interestRates[index];
                totalInterests.push_back( tempTotalInterest );

                const double tempTotalLoan = loanAmounts[index] + totalInterests[index];
                loanTotals.push_back( tempTotalLoan );

                const double tempMonthlyPay = loanTotals[index] / loanLengths[index];
                monthlyPays.push_back( tempMonthlyPay );
            }
        }
        else if (option == DISPLAY_LOAN) // Displays monthly payment
        {
            std::cout << std::fixed << std::setprecision(2);

            for (int index = 0; index < customerNames.size(); index++)
            {
                std::cout << "\n---\n";
                std::cout << customerNames[index] << " your total loan is " << loanTotals[index]
                          << "\nwith a monthly payment of $" << monthlyPays[index]
                          << "\nfor " << loanLengths[index] << " months with an interest"
                          << "\nrate of " << interestRates[index] << std::endl;
                std::cout << "---\n";
            }
        }
    } while (option != EXIT_PROGRAM);

    return 0;
}
Azeem
  • 11,148
  • 4
  • 27
  • 40