-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpenseTracker.c
85 lines (71 loc) · 2.33 KB
/
ExpenseTracker.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EXPENSES 50
#define MAX_CATEGORY_LENGTH 20
// Structure to represent an expense
struct Expense {
char category[MAX_CATEGORY_LENGTH];
float amount;
};
// Function prototypes
void recordExpense(struct Expense *expenses, int *numExpenses);
void generateReport(struct Expense *expenses, int numExpenses);
int main() {
struct Expense expenses[MAX_EXPENSES]; // Maximum 50 expenses
int numExpenses = 0;
int choice;
do {
printf("\nExpense Tracker\n");
printf("1. Record Expense\n");
printf("2. Generate Report\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
recordExpense(expenses, &numExpenses);
break;
case 2:
generateReport(expenses, numExpenses);
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 3);
return 0;
}
// Function to record a new expense
void recordExpense(struct Expense *expenses, int *numExpenses) {
if (*numExpenses >= MAX_EXPENSES) {
printf("Cannot record more expenses. Maximum limit reached.\n");
return;
}
printf("Enter expense category: ");
scanf(" %[^\n]", expenses[*numExpenses].category);
printf("Enter expense amount: ");
scanf("%f", &expenses[*numExpenses].amount);
(*numExpenses)++;
printf("Expense recorded successfully.\n");
}
// Function to generate and display expense report
void generateReport(struct Expense *expenses, int numExpenses) {
if (numExpenses == 0) {
printf("No expenses recorded yet.\n");
return;
}
printf("\nExpense Report\n");
printf("------------------\n");
printf("Category\tAmount\n");
printf("------------------\n");
float totalAmount = 0;
for (int i = 0; i < numExpenses; i++) {
printf("%s\t\t%.2f\n", expenses[i].category, expenses[i].amount);
totalAmount += expenses[i].amount;
}
printf("------------------\n");
printf("Total\t\t%.2f\n", totalAmount);
}