#include <string>
#include <iostream>
using namespace std;


int main()
{
    int loanAmount[10];
    int numMonths[10];
    string keepGoing;
    int counter = 0;

    do
    {
        cout << "Please enter a loan amount: ";
		cin >> loanAmount[counter];
        cout << "Please enter the number of months: ";
        cin >> numMonths[counter];
        counter++;
        cout << endl << "Do you want to continue (yes or no)? ";
        cin >> keepGoing;
       
    }while ((keepGoing == "yes") || (keepGoing == "YES"));

    
	cout << endl << "*************************" << endl;
    cout << "There are " << counter << " loans" << endl;

	
	for (int x = 0; x < counter; x++)
	{
		cout << "Loan " << x << " For $" << loanAmount[x] << " for " << numMonths[x]<< " months ";
		cout << "has a payment of $" << (double)loanAmount[x]/(double)numMonths[x] << " per month." << endl;
	}

	cout << "TOTAL MONTHLY PAYMENT: $" << loanAmount[counter] << endl;
	
}

Hello, I am having trouble summing up the monthly payment for the loan amounts the user has entered. Also, whenever it runs it says loan 1 is loan 0 and loan 2 is loan 1, etc...Any ideas on how I can fix this to have an output like this?

"
Please enter a loan amount: 1000
Please enter the number of months: 10
Continue (yes/no)? yes
Please enter a loan amount: 999
Please enter the number of months: 30
Continue (yes/no)? yes
Please enter a loan amount: 1234
Please enter the number of months: 5
Continue (yes/no)? no
************************* <<- YES PRINT THESE ASTERISKS
There are 3 loans.
Loan 1 for $1000 for 10 months has a payment of $100 per month
Loan 2 for $999 for 30 months has a payment of $33.3 per month
Loan 1 for $1234 for 5 months has a payment of $246.8 per month
TOTAL MONTHLY PAYMENT: $380.1"

Thanks for any help.

Dani AI

Generated

Quick summary and what to fix (short version)

  • Two separate issues caused the odd output: indexing and the final total. The loan print loop is using zero-based indexes (so the user sees Loan 0, Loan 1, ...), and the final total line prints an array element at counter — that index is the next free slot (out of the filled range) and therefore contains garbage. Accumulate the per-loan payments into a running total and print that instead. was right to suggest human-friendly numbering and a running total; below shows a safer, clearer approach.

Why the behaviour happens and what else to watch for

  • If you use fixed-size arrays you must check bounds (counter < capacity) before storing. Reading loanAmount[counter] after the loop reads an uninitialized entry. Integer division and division-by-zero are other gotchas: ensure months > 0 and use a floating type for money so you keep cents.

A concise, safer pattern (uses STL, input checks, and one-based numbering)

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

struct Loan { double amount; int months; };

int main() {
    std::vector<Loan> loans;
    std::string ans;
    do {
        Loan L;
        std::cout << "Please enter a loan amount: ";
        if (!(std::cin >> L.amount)) return 0;
        std::cout << "Please enter the number of months: ";
        if (!(std::cin >> L.months) || L.months <= 0) {
            std::cerr << "Invalid months\n"; return 0;
        }
        loans.push_back(L);
        std::cout << "Continue (yes/no)? "; std::cin >> ans;
        for (size_t i = 0; i < ans.size(); ++i) ans[i] = std::tolower(static_cast<unsigned char>(ans[i]));
    } while (ans == "yes");

    double total = 0.0;
    std::cout << std::fixed << std::setprecision(1);
    for (size_t i = 0; i < loans.size(); ++i) {
        double monthly = loans[i].amount / loans[i].months;
        total += monthly;
        std::cout << "Loan " << (i + 1) << " for $" << loans[i].amount << " for " << loans[i].months
                  << " months has a payment of $" << monthly << " per month\n";
    }
    std::cout << "TOTAL MONTHLY PAYMENT: $" << total << '\n';
}

Extra tips: prefer double or an integer cents representation for money, validate user input (std::cin.fail()), and stop pushing once you hit capacity if you must use a fixed-size array. This will eliminate the off-by-one indexing and the spurious final value.

Recommended Answers

All 3 Replies

>>cout << "Loan " << x << " For $"
add 1 to x so you get 1, 2, ... cout << "Loan " << x+1 << " For $" >>cout << "has a payment of $" << (double)loanAmount[x]/(double)numMonths[x] << " per month." << endl;
To get the sum of the above, do this in two steps and add them up on another variable

float total = 0;

...
...
float n = double)loanAmount[x]/(double)numMonths[x];
total += n;
cout <<  "has a payment of $" << n << " per month." << endl;

Now when that loop ends the variable total will have the sum of those amounts.

Thanks a lot Ancient Dragon

Please re-read my suggestion -- I didn't notice that you already had a variable x, so I changed my post accordingly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.