Hi,
I wrote this very simple C console app. It could've been written simpler way; however, I used pointers instead of normal variable to manipulate data (since I'm enjoying them atm). I was wondering if there was any memory leaks in my code. As far as I understand, I didn't use any heap allocated memory, so there shouldn't be any memory leaks. Since, I'm new I might be wrong. Any suggestions on improving the code or coding in general would be really appreciated. Thanks.
Code:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int getCommand()
{
int commandValue;
bool done = false;
do
{
printf("Enter command: ");
scanf(" %d", &commandValue);
if (commandValue >= 0 && commandValue < 5) done = true;
} while (!done);
return commandValue;
}
void displayMainMenu()
{
printf("*** ACME checkbook-balancing program ***\nCommands: 0=clear, 1=credit, 2=debit, 3=balance, 4=exit\n\n");
}
void exitApp()
{
exit(0);
}
void addBalance(float* balance, float amount)
{
*balance += amount;
}
void withdrawBalance(float* balance, float amount)
{
*balance -= amount;
}
void getAmount(char* message, float* amount)
{
printf("Enter amount of %s: ", message);
scanf("%f", amount);
}
void printBalance(float* balance)
{
printf("Current balance: $%.2f\n", *balance);
}
void clearBalance(float* balance)
{
*balance = 0.0f;
}
int main()
{
displayMainMenu();
bool continueApp = true;
float balance, amount;
while (continueApp)
{
int option = getCommand();
switch (option)
{
case 0:
clearBalance(&balance);
break;
case 1:
getAmount("credit", &amount);
addBalance(&balance, amount);
break;
case 2:
getAmount("debit", &amount);
withdrawBalance(&balance, amount);
break;
case 3:
printBalance(&balance);
break;
case 4:
continueApp = false;
break;
}
}
exitApp();
}