From 9bf38a1487c760f9ad8b65839baaca55b4b9e574 Mon Sep 17 00:00:00 2001 From: fdlt3817 Date: Fri, 10 Feb 2023 15:12:49 +0100 Subject: [PATCH] Create function to set saving goal --- src/interestCalculator.c | 25 ++++++++++++++++++++++--- src/interestCalculator.h | 1 + 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/interestCalculator.c b/src/interestCalculator.c index 6998887..0764a39 100644 --- a/src/interestCalculator.c +++ b/src/interestCalculator.c @@ -20,6 +20,24 @@ void troubleshoot(int errorCode){ } } +void askForSavingGoal(float principalAmount, float accInterestPerYear){ + char c; + float goal; + float timeForGoal; + printf("\nWould you like to set a saving goal?"); + scanf(" %c",&c); + + if(c=='y'||c=='Y'){ + printf("\nPlease enter your goal amount in €:"); + scanf("%f",&goal); + timeForGoal=(goal-principalAmount)/accInterestPerYear; + printf("In %.1f years you will reach your goal of %f.\n", timeForGoal, goal); + }else{ + //error + } + +} + float initiateInterest(float principalAmount, float interest, float time){ return principalAmount*(1+(interest*time)); } @@ -67,6 +85,7 @@ void calculateYearlyInterest(){ float result= initiateInterest(principalAmount,interestDecimal,timeInYears); printf("\nAmount with the interest is %.2f€.",result); + askForSavingGoal(principalAmount,principalAmount*interestDecimal); } void calculateMonthlyInterest(){ float principalAmount; @@ -123,8 +142,8 @@ void initiateCalculator(){ } } -// int main(){ +int main(){ -// initiateCalculator(); + initiateCalculator(); -// } +} diff --git a/src/interestCalculator.h b/src/interestCalculator.h index 9dfb731..0c9c3b9 100644 --- a/src/interestCalculator.h +++ b/src/interestCalculator.h @@ -5,6 +5,7 @@ void calculateYearlyInterest(); void calculateMonthlyInterest(); +void askForSavingGoal(float principalAmount, float accInterestPerYear); float initiateInterest(float principalAmount, float interest, float time); void troubleshoot(int errorCode); #endif \ No newline at end of file