-
Notifications
You must be signed in to change notification settings - Fork 0
Description
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char date[11]; // YYYY-MM-DD
char category[20];
float amount;
char description[50];
} Expense;
void addExpense() {
FILE *fp = fopen("expenses.txt", "a");
Expense e;
printf("\nEnter Date (YYYY-MM-DD): ");
scanf("%s", e.date);
printf("Enter Category: ");
scanf("%s", e.category);
printf("Enter Amount: ");
scanf("%f", &e.amount);
getchar(); // flush newline
printf("Enter Description: ");
fgets(e.description, sizeof(e.description), stdin);
strtok(e.description, "\n"); // remove \n
fprintf(fp, "%s %s %.2f %s\n", e.date, e.category, e.amount, e.description);
fclose(fp);
printf("Expense added!\n");
}
void viewExpenses() {
FILE *fp = fopen("expenses.txt", "r");
Expense e;
printf("\n--- All Expenses ---\n");
printf("Date | Category | Amount | Description\n");
printf("--------------------------------------------------------\n");
while (fscanf(fp, "%s %s %f %[^\n]", e.date, e.category, &e.amount, e.description) == 4) {
printf("%-10s | %-12s | %7.2f | %s\n", e.date, e.category, e.amount, e.description);
}
fclose(fp);
}
int extractMonth(char date[]) {
int month;
sscanf(date, "%*d-%d-%*d", &month);
return month;
}
int extractYear(char date[]) {
int year;
sscanf(date, "%d-%*d-%*d", &year);
return year;
}
void showDailySummary(char targetDate[]) {
FILE *fp = fopen("expenses.txt", "r");
Expense e;
float total = 0;
printf("\n--- Daily Summary for %s ---\n", targetDate);
printf("Category | Amount | Description\n");
printf("----------------------------------------\n");
while (fscanf(fp, "%s %s %f %[^\n]", e.date, e.category, &e.amount, e.description) == 4) {
if (strcmp(e.date, targetDate) == 0) {
printf("%-12s | ₹%7.2f | %s\n", e.category, e.amount, e.description);
total += e.amount;
}
}
printf("----------------------------------------\n");
printf("Total: ₹%.2f\n", total);
fclose(fp);
}
void showMonthlySummary(int month, int year) {
FILE *fp = fopen("expenses.txt", "r");
Expense e;
float total = 0;
printf("\n--- Monthly Summary for %02d/%d ---\n", month, year);
printf("Date | Category | Amount | Description\n");
printf("--------------------------------------------------------\n");
while (fscanf(fp, "%s %s %f %[^\n]", e.date, e.category, &e.amount, e.description) == 4) {
if (extractMonth(e.date) == month && extractYear(e.date) == year) {
printf("%-10s | %-12s | ₹%7.2f | %s\n", e.date, e.category, e.amount, e.description);
total += e.amount;
}
}
printf("--------------------------------------------------------\n");
printf("Total Spent in %02d/%d: ₹%.2f\n", month, year, total);
fclose(fp);
}
int main() {
int choice;
do {
printf("\n--- Daily Expense Tracker ---\n");
printf("1. Add Expense\n");
printf("2. View All Expenses\n");
printf("3. Daily Summary\n");
printf("4. Monthly Summary\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
addExpense();
} else if (choice == 2) {
viewExpenses();
} else if (choice == 3) {
char date[11];
printf("Enter Date (YYYY-MM-DD): ");
scanf("%s", date);
showDailySummary(date);
} else if (choice == 4) {
int month, year;
printf("Enter Month and Year (MM YYYY): ");
scanf("%d %d", &month, &year);
showMonthlySummary(month, year);
} else if (choice == 0) {
printf("Exiting. Goodbye!\n");
} else {
printf("Invalid choice. Try again.\n");
}
} while (choice != 0);
return 0;
}