I created two while blocks, but only one of them works although I think they both should. I think I may have made syntax errors. I'm not really sure. Any help is appreciated.
Second loop works fine, but I would swap two first lines (first calculate interest, then accept payment). First does not works as intended: If I enter 1000 balance and 1 payment It will give me some answer. But I would never pay off my debt with such interest and monthly payment!
Second loop will just get into infinite loop.
I propose you to use second loop, but add check if balance at the end is larger than in the beginning like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while (balance > 0) {
old_balance = balance;
balance -= payment;
balance = (balance + (balance * rate));
if(old_balance < balance) {
month = 0;
break;
}
month++;
}
if (month == 0)
std::cout << "you will never pay off your debt";
else
std::cout <<//...
Or, better, check for possibility to payoff before loop, like: if ( balance < ((balance - payment) * (rate + 1)) )
Implementing the if statement before loop as he described works even better. I compiled it in Xcode and it worked great.
I think it increases overall readability of the program from my standpoint. I would use that strategy depending on your intended purpose for creating this program.