Skip to content

Commit e74beb9

Browse files
authored
Merge pull request #3 from SanviSeetha4481/enhance-expense-tracker
Add file persistence and input validation to Expense Tracker
2 parents ce7bf31 + 2d4fb98 commit e74beb9

1 file changed

Lines changed: 261 additions & 26 deletions

File tree

expense.c

Lines changed: 261 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,285 @@
11
#include <stdio.h>
2+
#include <string.h>
23

4+
#define MAX_EXPENSES 20
5+
#define FILE_NAME "expenses.txt"
6+
7+
/* Structure to store expense details */
38
struct Expense {
4-
char name[50]; // name of expense
5-
float amount; // money spent
9+
char name[50]; // Name of the expense
10+
float amount; // Amount spent
611
};
712

13+
/* Function prototypes */
14+
void loadFromFile(struct Expense exp[], int *count, float *total);
15+
void saveToFile(struct Expense exp[], int count);
16+
void addExpense(struct Expense exp[], int *count, float *total);
17+
void viewExpenses(struct Expense exp[], int count);
18+
void viewTotal(float total);
19+
820
int main() {
9-
struct Expense exp[20];
21+
struct Expense exp[MAX_EXPENSES];
1022
int count = 0;
11-
float total = 0; // total expense value
23+
float total = 0.0;
1224
int choice;
1325

26+
/* Load existing expenses from file */
27+
loadFromFile(exp, &count, &total);
28+
1429
while (1) {
1530
printf("\n--- EXPENSE TRACKER ---\n");
1631
printf("1. Add Expense\n");
1732
printf("2. View Expenses\n");
1833
printf("3. View Total\n");
1934
printf("4. Exit\n");
20-
scanf("%d", &choice);
35+
printf("Enter your choice: ");
2136

22-
if (choice == 1) {
23-
// Add new expense
24-
printf("Enter name: ");
25-
getchar();
26-
fgets(exp[count].name, 50, stdin);
37+
if (scanf("%d", &choice) != 1) {
38+
printf("Invalid input. Please enter a number.\n");
39+
while (getchar() != '\n'); // clear input buffer
40+
continue;
41+
}
2742

28-
printf("Enter amount: ");
29-
scanf("%f", &exp[count].amount);
43+
switch (choice) {
44+
case 1:
45+
addExpense(exp, &count, &total);
46+
saveToFile(exp, count);
47+
break;
3048

31-
total += exp[count].amount;
32-
count++;
33-
}
34-
else if (choice == 2) {
35-
// Show all expenses
36-
for (int i = 0; i < count; i++) {
37-
printf("%s - %.2f\n", exp[i].name, exp[i].amount);
38-
}
49+
case 2:
50+
viewExpenses(exp, count);
51+
break;
52+
53+
case 3:
54+
viewTotal(total);
55+
break;
56+
57+
case 4:
58+
printf("Exiting program. Goodbye!\n");
59+
return 0;
60+
61+
default:
62+
printf("Invalid choice. Please select between 1 and 4.\n");
3963
}
40-
else if (choice == 3) {
41-
// Show total spent so far
42-
printf("Total Expense: %.2f\n", total);
64+
}
65+
}
66+
67+
/* Load expenses from file */
68+
void loadFromFile(struct Expense exp[], int *count, float *total) {
69+
FILE *fp = fopen(FILE_NAME, "r");
70+
if (fp == NULL) {
71+
// File does not exist, start fresh
72+
return;
73+
}
74+
75+
while (*count < MAX_EXPENSES &&
76+
fscanf(fp, "%49[^,],%f\n", exp[*count].name, &exp[*count].amount) == 2) {
77+
*total += exp[*count].amount;
78+
(*count)++;
79+
}
80+
81+
fclose(fp);
82+
}
83+
84+
/* Save expenses to file */
85+
void saveToFile(struct Expense exp[], int count) {
86+
FILE *fp = fopen(FILE_NAME, "w");
87+
if (fp == NULL) {
88+
printf("Error saving data to file.\n");
89+
return;
90+
}
91+
92+
for (int i = 0; i < count; i++) {
93+
fprintf(fp, "%s,%.2f\n", exp[i].name, exp[i].amount);
94+
}
95+
96+
fclose(fp);
97+
}
98+
99+
/* Add a new expense */
100+
void addExpense(struct Expense exp[], int *count, float *total) {
101+
if (*count >= MAX_EXPENSES) {
102+
printf("Expense limit reached.\n");
103+
return;
104+
}
105+
106+
printf("Enter expense name: ");
107+
getchar(); // consume leftover newline
108+
fgets(exp[*count].name, sizeof(exp[*count].name), stdin);
109+
exp[*count].name[strcspn(exp[*count].name, "\n")] = '\0'; // remove newline
110+
111+
printf("Enter amount: ");
112+
if (scanf("%f", &exp[*count].amount) != 1 || exp[*count].amount <= 0) {
113+
printf("Invalid amount. Please enter a positive value.\n");
114+
while (getchar() != '\n');
115+
return;
116+
}
117+
118+
*total += exp[*count].amount;
119+
(*count)++;
120+
121+
printf("Expense added successfully.\n");
122+
}
123+
124+
/* Display all expenses */
125+
void viewExpenses(struct Expense exp[], int count) {
126+
if (count == 0) {
127+
printf("No expenses recorded.\n");
128+
return;
129+
}
130+
131+
printf("\n%-20s | %10s\n", "Expense", "Amount");
132+
printf("--------------------------------\n");
133+
134+
for (int i = 0; i < count; i++) {
135+
printf("%-20s | %10.2f\n", exp[i].name, exp[i].amount);
136+
}
137+
}
138+
139+
/* Display total expense */
140+
void viewTotal(float total) {
141+
printf("Total Expense: %.2f\n", total);
142+
}
143+
#include <stdio.h>
144+
#include <string.h>
145+
146+
#define MAX_EXPENSES 20
147+
#define FILE_NAME "expenses.txt"
148+
149+
/* Structure to store expense details */
150+
struct Expense {
151+
char name[50]; // Name of the expense
152+
float amount; // Amount spent
153+
};
154+
155+
/* Function prototypes */
156+
void loadFromFile(struct Expense exp[], int *count, float *total);
157+
void saveToFile(struct Expense exp[], int count);
158+
void addExpense(struct Expense exp[], int *count, float *total);
159+
void viewExpenses(struct Expense exp[], int count);
160+
void viewTotal(float total);
161+
162+
int main() {
163+
struct Expense exp[MAX_EXPENSES];
164+
int count = 0;
165+
float total = 0.0;
166+
int choice;
167+
168+
/* Load existing expenses from file */
169+
loadFromFile(exp, &count, &total);
170+
171+
while (1) {
172+
printf("\n--- EXPENSE TRACKER ---\n");
173+
printf("1. Add Expense\n");
174+
printf("2. View Expenses\n");
175+
printf("3. View Total\n");
176+
printf("4. Exit\n");
177+
printf("Enter your choice: ");
178+
179+
if (scanf("%d", &choice) != 1) {
180+
printf("Invalid input. Please enter a number.\n");
181+
while (getchar() != '\n'); // clear input buffer
182+
continue;
43183
}
44-
else if (choice == 4) {
45-
break;
184+
185+
switch (choice) {
186+
case 1:
187+
addExpense(exp, &count, &total);
188+
saveToFile(exp, count);
189+
break;
190+
191+
case 2:
192+
viewExpenses(exp, count);
193+
break;
194+
195+
case 3:
196+
viewTotal(total);
197+
break;
198+
199+
case 4:
200+
printf("Exiting program. Goodbye!\n");
201+
return 0;
202+
203+
default:
204+
printf("Invalid choice. Please select between 1 and 4.\n");
46205
}
47206
}
207+
}
48208

49-
return 0;
209+
/* Load expenses from file */
210+
void loadFromFile(struct Expense exp[], int *count, float *total) {
211+
FILE *fp = fopen(FILE_NAME, "r");
212+
if (fp == NULL) {
213+
// File does not exist, start fresh
214+
return;
215+
}
216+
217+
while (*count < MAX_EXPENSES &&
218+
fscanf(fp, "%49[^,],%f\n", exp[*count].name, &exp[*count].amount) == 2) {
219+
*total += exp[*count].amount;
220+
(*count)++;
221+
}
222+
223+
fclose(fp);
50224
}
225+
226+
/* Save expenses to file */
227+
void saveToFile(struct Expense exp[], int count) {
228+
FILE *fp = fopen(FILE_NAME, "w");
229+
if (fp == NULL) {
230+
printf("Error saving data to file.\n");
231+
return;
232+
}
233+
234+
for (int i = 0; i < count; i++) {
235+
fprintf(fp, "%s,%.2f\n", exp[i].name, exp[i].amount);
236+
}
237+
238+
fclose(fp);
239+
}
240+
241+
/* Add a new expense */
242+
void addExpense(struct Expense exp[], int *count, float *total) {
243+
if (*count >= MAX_EXPENSES) {
244+
printf("Expense limit reached.\n");
245+
return;
246+
}
247+
248+
printf("Enter expense name: ");
249+
getchar(); // consume leftover newline
250+
fgets(exp[*count].name, sizeof(exp[*count].name), stdin);
251+
exp[*count].name[strcspn(exp[*count].name, "\n")] = '\0'; // remove newline
252+
253+
printf("Enter amount: ");
254+
if (scanf("%f", &exp[*count].amount) != 1 || exp[*count].amount <= 0) {
255+
printf("Invalid amount. Please enter a positive value.\n");
256+
while (getchar() != '\n');
257+
return;
258+
}
259+
260+
*total += exp[*count].amount;
261+
(*count)++;
262+
263+
printf("Expense added successfully.\n");
264+
}
265+
266+
/* Display all expenses */
267+
void viewExpenses(struct Expense exp[], int count) {
268+
if (count == 0) {
269+
printf("No expenses recorded.\n");
270+
return;
271+
}
272+
273+
printf("\n%-20s | %10s\n", "Expense", "Amount");
274+
printf("--------------------------------\n");
275+
276+
for (int i = 0; i < count; i++) {
277+
printf("%-20s | %10.2f\n", exp[i].name, exp[i].amount);
278+
}
279+
}
280+
281+
/* Display total expense */
282+
void viewTotal(float total) {
283+
printf("Total Expense: %.2f\n", total);
284+
}
285+

0 commit comments

Comments
 (0)