C++

Jordan W.

asked • 10/13/21

last itenration of while loop

im trying to display the last iteration of a while loop outside of the loop itself as in once the while loop executes im trying to grab the last iteration the while loop performed and display it here is a portion of the loop i mean



while (balance > 0)

{

cout << setw(6) << month;


if (balance < monthlyPayment)

{

monthlyPayment = balance;


principal = (monthlyPayment - (balance * interestRatePer));

interestPaid = monthlyPayment - principal;

balance = 0;

}

else

{

principal = monthlyPayment - (balance * interestRatePer);

interestPaid = monthlyPayment - principal;

balance += (balance * interestRatePer);

balance -= monthlyPayment;

}

totalInterest += interestPaid;

cout << fixed << setprecision(2);

cout << left << setw(20) << "" << "$" << right << setw(5) << interestPaid;


cout << left << setw(16) << "" << "$" << right << setw(5) << principal;

cout << left << setw(15) << "" << "$" << right << setw(5) << balance;

cout << endl;

month++;

}

cout << left << "total interest paid: $" << totalInterest;

return 0;


1 Expert Answer

By:

Nicholas T. answered • 04/07/22

Tutor
4.9 (228)

Student Experienced in C++ Assignments

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.