From 8ef7a2647f1a5079d9ee3f1bfa99402935bed09b Mon Sep 17 00:00:00 2001 From: fdlt3817 Date: Fri, 10 Feb 2023 15:16:21 +0100 Subject: [PATCH] Prevent unlogical calculations --- src/interestCalculator.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/interestCalculator.c b/src/interestCalculator.c index 0764a39..58c1072 100644 --- a/src/interestCalculator.c +++ b/src/interestCalculator.c @@ -17,6 +17,9 @@ void troubleshoot(int errorCode){ case 3: printf("Invalid option. Aborting."); break; + case 4: + printf("Your goal cannot be smaller than your principal funds. Aborting."); + break; } } @@ -24,16 +27,21 @@ void askForSavingGoal(float principalAmount, float accInterestPerYear){ char c; float goal; float timeForGoal; - printf("\nWould you like to set a saving goal?"); + printf("\nWould you like to set a saving goal? [y] yes [any] no"); 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); + if(goal>=principalAmount){ + timeForGoal=(goal-principalAmount)/accInterestPerYear; + printf("\nIn %.1f years you will reach your goal of %f.\n", timeForGoal, goal); + }else{ + troubleshoot(4); + return; + } }else{ - //error + return; } }