-2

I have done this code and I have made a loop where my code will restart from scratch however I want the program to restart while changing the month date and also asking the user how many months they would like so the program will run a certain amount of times, as I have added the month 1 being the first month.

#include<stdio.h>
#include<stdlib.h>
float twag(float amount,float rate)
{
    float si;
    si = (amount * rate) / 100;
    return si;
}

float twag2(float amount,float rate)
{
    float er;
    er = twag(amount, rate) + amount;
    return er;
}

int main() 
{
    char answer;
    do {
        float amount;
        float rate;
        float si;
        float er;

        printf("Month1\n");

        printf("\nEnter intial deposit : ");
        scanf("%f", &amount);

        printf("\nEnter Rate of Interest : ");
        scanf("%f", &rate);

        printf("\nSimple Interest : %.2f \n", twag(amount,rate));

        printf("\nEnd Payment: %.2f \n",twag2(amount,rate));

        if (amount <= 100)
            printf("interest rate should be 10%%\n");

        else if (amount <= 200)
            printf("interest rate should be 50%%\n");

        else if (amount <= 500)
            printf("interest rate should be 80%%\n");

        else if (amount >= 500)
            printf("interest rate should be 90%%\n");
        system("PAUSE");


        printf("\nPress Y to continue to the second month or Press any Key To 
        Exit");
        scanf(" %c", &answer); // 
    }
     while (answer == 'y' || answer == 'Y');

    return 0;
}
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49

1 Answers1

0

Assuming you're applying monthly interest, you at least need to keep record of the current month and year, starting at today's date, so the way to do that is by using localtime

void get_date(int *month, int *day, int *year)
{
    struct tm *current;
    time_t timenow;
    time(&timenow);
    current = localtime(&timenow);
    *month = current->tm_mon+1;
    *day = current->tm_mday;
    *year = current->tm_year+1900;
}

I suggest you read here on why system("PAUSE") is bad, besides that, You defined your loop variables inside the loop, which means they're going to reset each time you loop, and your interest should be constant during the loop, so your code should look like this

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

float twag(float amount,float rate);
float twag2(float amount,float rate);
void get_date(int *month, int *day, int *year);

int main() 
{
    char answer;
    float amount, rate, si, er;
    int day, month, year;

    printf("\nEnter intial deposit : ");
    scanf("%f", &amount);

    printf("\nEnter Rate of Interest : ");
    scanf("%f", &rate);

    get_date(&month, &day, &year);

    do
    {
        // increment date by 1 month
        if (++month > 12) month=1, year++;

        printf("\nDate %d/%d/%d \n", day,month,year);
        printf("\nSimple Interest : %.2f \n", twag(amount,rate));
        printf("\nEnd Payment: %.2f \n",twag2(amount,rate));
        amount = twag2(amount,rate);    

        printf("\nPress Y to continue to the next month or Press any Key To Exit");
        scanf("%c", &answer);
    }
    while (answer == 'y' || answer == 'Y');

    return 0;
}
Khaled.K
  • 5,828
  • 1
  • 33
  • 51