I am tyring to write a simple program to displa yloan information. Here are my instructions:

The monthly payment on a loan may be calculated by the following formula
monthly payment=(monthlyrate*(1-monthlyrate)^time/(1+monthlyrate)^time-1)*loanamount
Use pow function in your formula

To raise a number to the power of another number you will have to use the pow function.
You will have to include the following header file
#include <cmath>

You will prompt the user for the following information:

Annual Rate: Note that you will have to take the user entry and divide it by 12 to get the monthly rate and then further divide by 100 to convert it to a percentage. (12 % annual interest would be 1% monthly interest.) You will be using the converted rate in your formula. The user entry is the annual rate but you will use the monthly rate in your formula

N is the number of payments

Loan amount

Calculate and displays a report similar to the following: (Sample data)

Loan Amount: $ 10000.00
Annual Interest Rate: 12
Number of Payments: 36
Monthly Payment: $ 332.14
Amount Paid Back $ 11957.15
Interest Paid: $ 1957.15

Validation:
Loan amount should be between $100 and $500,000
Interest rate should be between 1 % and 20%
Number of payments should be between 5 and 360 months


The report should be formatted and aligned according to the above

Here is my code so far:

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;

int main ()

{ 
                                                   
double doublerate, rate, payment, paid, interest, annualrate,amount; // Declaring variables
int number;
paid=amount/number;
interest=amount*annualrate;
rate=annualrate/12;   
doublerate=rate+1.0;              
payment=((pow(doublerate, number)*rate)/pow (doublerate, number)-1)*amount;


 

cout << "Loan Interest Program";                         // Program Name
cout << "\n\n\nPlease enter your annual rate\n";         //Asks for user input 
cin >> annualrate;                                       //Gets user info
do {
cout << "\nPlease enter the number of payments\n";       //Asks for user input
cin >> number;                                           // Gets user info 
      do {
         cout << "\nPlease enter loan amount\n";         //Asks for user input
         cin >> amount;                                  //Gets user info
              do {
                  printf("\nLoan Amount:                  $%-7.2f", amount);
                  
                  printf("\nAnnual Interest Rate:          %-2.2f", annualrate);

                  cout << "%";
  		          
                  printf("\nNumber of Payments:              %-2d", number);
                  
  	             printf ("\nMonthly Payment:	          $%-3.2f", payment);
    
                  printf("\nAmount Paid Back	         $%-6.2f"), paid;
                
                  printf ("\nInterest  Paid:		      $%-6.2f", interest);
                
                  cout <<"\n\n";
             
             
             
             } while (amount >100 && amount <500,000);
     } while (number <360 && number >5);
}while (rate <20 && rate >1);
     
      

}

I know it's weird, but my instructor wants us to use C and C++, and wants us to use pow and a do/while loop.

Dani AI

Generated

Several simple bugs explain the compiler errors and the wrong numbers. Calculations are being done before inputs are read, there are stray semicolons after if(...) which make the conditionals do nothing, numeric literals with commas (for example 500,000) are parsed incorrectly, and some printf calls close the call before passing the value — so the value never reaches the format string. As pointed out, move the calculations until after you read and validate the inputs; was right to suggest reviewing basic if/else syntax.

Use the standard amortization formula rather than the parentheses-shuffled expression in your code. With r = monthly rate as a decimal, P = principal and n = number of payments:
payment = r * P / (1 - pow(1 + r, -n))
Compute r from the user entry as r = annualRate / 12.0 / 100.0. After you compute payment, set amountPaidBack = payment * n and interestPaid = amountPaidBack - P.

Because the instructor requires do/while and pow, validate each input with a loop that repeats until the value is inside the allowed range (annual rate 1–20, payments 5–360, amount 100–500000). Do not put a trailing semicolon after if(...) or do — that creates an empty statement. Also avoid commas in numeric literals; write 500000, not 500,000.

For output, prefer a single approach: use <iomanip> with cout << fixed << setprecision(2) and setw to align columns, or use printf but make sure format specifiers match types and that arguments are passed inside the parentheses. Test with the sample (10000, 12%, 36) and confirm monthly ≈ 332.14, total ≈ 11957.15, interest ≈ 1957.15. This sequence satisfies the pow and do/while requirements and will fix the errors you saw.

Recommended Answers

All 4 Replies

> paid=amount/number;
>interest=amount*annualrate;
You need to do these calculations AFTER you have input values into the program, not before.

Ok, now I have this:

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
   double doublerate, rate, payment, paid, interest, annualrate,amount; // Declaring variables
   int number;


   

   cout << "Loan Interest Program"; 
   cout << "\n\n\nPlease enter your annual rate\n"; 
   cin >> annualrate; 
   
   
       
  if (rate <20 && rate >1);
        {
      cout << "\nPlease enter the number of payments\n";
      cin >> number;
       } 
       if(number <360 && number >5); 
   
                 {
                 cout << "\nPlease enter loan amount\n"; 
                 cin >> amount; 
                 }
        
         
                  if(amount >100 && amount <500,000)
                         {   paid=amount/number;
                             interest=amount*annualrate;
                             rate=annualrate/12;
                             doublerate=rate+1.0;
                             payment=((pow(doublerate, number)*rate)/pow (doublerate, number)-1)*amount;
        
                             printf("\nLoan Amount: $%-7.2f", amount);
                             printf("\nAnnual Interest Rate: %-2.2f", annualrate);
                             cout << "%";
                             printf("\nNumber of Payments: %-2d", number);
                             printf ("\nMonthly Payment: $%-3.2f", payment);
                             printf("\nAmount Paid Back $%-6.2f"), paid;
                             printf ("\nInterest Paid: $%-6.2f", interest);
                             cout <<"\n\n";
                           } 
                   else 
                              
                                  cout << "Amount must be between $100 and $500,000.";
                              
         else 
                              
                                  cout << "Number must be between 5and 360.";
          
                              
     else 
                              
                                  cout << "Rate must be between 1 and 20.";                       
                          
                          
   
   }

I am getting these messages:
expected primary-expression before "else"

expected `;' before "else"

Member Avatar for Member #46692

Do you know how a basic if else clause works. If not, google it.

If you're not going to use braces round your else statements, then you need to be a lot better at indentation.

> if (rate <20 && rate >1);
Watch that ; at the end

> if(amount >100 && amount <500,000)
Comma is also an operator. It is not a digit group separator.

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.