ok, i'm new to c++, please bear with me........

i have a program... it asks the user to enter the price, then it asks to enter the interest rate from, and then interest rate to, and then the loan period (> 2 years)

The formula used is:

result = price * pow(1+annual rate of interest/100/365, loan period *365)

it should be desplayed like this:

              8%             9%               10%
Year 2    result           result            result
year 3    result           result            result
.
. 
year 4    result           result            result

the preceding was if: interest rate from was 8, and interest rate to was 10, and if the user entered 4 for the loan period.......so it basically needs to calculate the result for each percentage and for each year ofcourse......how do i do this using for loop....thanks alot for your help..........this is what i've done so far:

int year, price, intratefrom, intrateto, x, y, loanp;
    double CPL;

    printf("\n");
    printf("Car Loan Financing\n\n\n");
    printf("Please enter price of car:");
    scanf("%d", &price);
    printf("\nPlease enter interest rate (integer %) from          :");
    scanf("%d", &intratefrom);
    printf("Please enter interest rate (integer %) to            :");
    scanf("%d", &intrateto);
    printf("Please enter loan period, from 2 years to (integer) :");
    scanf("%d", &loanp);
    printf("\n\n");

    SO THIS IS WHERE I"M STUCK, I"VE TRIED USING FOR BUT IT JUST DESNT DISPLAY RIGHT(CPL assigned is the result)  ACTALLY ALL THIS CALCULATION AND DISPLAY should be DONE IN THE FUNCTION which i named financing(), i dont know how to do that in the new function without using global variables :( :

             for( x = intratefrom; x <= intrateto; x++){


        for(year = 2; year <= loanp; year++){



        }
    }
    financing(); //call the function financing
}

Dani AI

Generated

For : the task is best solved by a small function that takes the inputs (price, rate-from, rate-to, loan-years), then uses nested loops — one loop for years (start at 2 up to the loan period) and an inner loop for each integer rate in the given range. Compute the compound result for each (year, rate) pair and print a row per year with a column per rate. Pass all values as parameters (avoid globals) and use double for financial values so fractional results are preserved.

Common pitfalls to avoid:

  • Integer division: convert the rate and denominators to double (for example, daily = rate / 100.0 / 365.0).
  • Returning pointers to local arrays: as suggested, returning a pointer to a stack array is undefined behavior. Prefer printing inside the function, returning a std::vector (by value), or filling a caller-supplied container by reference.
  • Formatting: use std::setw, std::fixed and std::setprecision to align columns and control decimal places.

Example (modern, safe C++ approach):

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>

void print_table(double price, int rate_from, int rate_to, int loan_years) {
    std::cout << std::setw(8) << "Year";
    for (int r = rate_from; r <= rate_to; ++r)
        std::cout << std::setw(12) << (std::to_string(r) + "%");
    std::cout << '\n';

    for (int y = 2; y <= loan_years; ++y) {
        std::cout << std::setw(8) << y;
        for (int r = rate_from; r <= rate_to; ++r) {
            double daily = r / 100.0 / 365.0;
            double amount = price * std::pow(1.0 + daily, y * 365.0);
            std::cout << std::setw(12) << std::fixed << std::setprecision(2) << amount;
        }
        std::cout << '\n';
    }
}

For the compound calculation reference see std::pow. Keep input validation (rates and years) and rounding rules explicit in the calling code.

Well here is an idea.. didn't test just wrote it from the head.. It might be buggy.

#include <stdio.h>
#include <stdlib.h>

int * intcalc(int,int,int,int);
int * intcalc (int price, int rate1, int rate2, int period)
{
int result[2]={0,0};//declares and initializes
for (int i=0; i<(period+1); i++)
{
result[0] =0;//whatever the way you calc for each year (rate1)
}
for (int i=0; i<(period+1); i++)
{
result[1] =0;//whatever the way you calc for each year (rate2)
}
return result;
}

void main ()
{
for (;;)
{
int rate1, rate2, period, price;

printf ("Enter data:\n");
fflush (stdin);  //flushing scanf buffer.. don't have to do it
scanf ("%d",&price);
scanf ("%d",&rate1);
scanf ("%d",&rate2);
scanf ("%d",&period);
int * result = intcalc(price, rate1, rate2,period);
printf ("\nresult1=%d, result2=%d\n",result[0], result[1]);
system("PAUSE");
}
}
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.