#include <stdio.h>
#include <string.h>

typedef struct
{
	char item[50];
	int date[15];
	float price[5];
	int amount[10];
	float total[10];
}EXPENSES;

EXPENSES aExpenses;

int getOption();
void input();
void viewAll();
void del();
void edit();

int main (void)
{
	int done = 0;
	int option;
	
	//while (!done)
		option = getOption();

	if (option == 5)
		done = 1;

	else
	{
		switch (option)
		{
		case 1: input;
			break;
		case 2: viewAll;
			break;
		case 4: del;
			break;
		case 5: edit;
			break;

		default : printf("Invalid selection");
		}
	}
	return 0;
}
int getOption()
{
	int option;
	
	printf("\t====*MAIN MENU*====\n");
	printf("\t1) Enter New data\n");
	printf("\t2) View all data\n");
	printf("\t3) Edit data\n");
	printf("\t4) delete data\n");
	printf("\t5) exit\n\n");

	printf("enter your option: ");
	scanf("d",&option);

	return option;
}

void input(void)
{
	char item;
	int date;
	float price;
	int amount;
	float total;
	FILE* p;

	p = fopen ("data.txt", "w");
	fprintf(p,"%c\n", item);
	fprintf(p,"%d\n", date);
	fprintf(p,"%f\n", price);
	fprintf(p,"%d\n", amount);
	fprintf(p,"%f\n", total);
	fclose(p);
	
	return;
}
void viewAll()
{
	char item;
	int date;
	float price;
	int amount;
	float total;
	FILE* p;
	
	p = fopen ("data.txt", "r");
	fscanf(p,"%c %d %f %d %f", &item, &date, &price, &amount, &total);
	fclose(p);

	return;
}
void del()
{
	char item;
	int date;
	float price;
	int amount;
	float total;
	FILE* p;

	p = fopen ("data.txt", "a");
	fprintf(p,"%c\n", item);
	fprintf(p,"%d\n", date);
	fprintf(p,"%f\n", price);
	fprintf(p,"%d\n", amount);
	fprintf(p,"%f\n", total);
	fclose(p);

	return;
}
void edit()
{
	char item;
	int date;
	float price;
	int amount;
	float total;
	FILE* p;

	p = fopen ("data.txt", "w");
	fprintf(p,"%c\n", item);
	fprintf(p,"%d\n", date);
	fprintf(p,"%f\n", price);
	fprintf(p,"%d\n", amount);
	fprintf(p,"%f\n", total);
	fclose(p);
	
	return;
}

this program can run but when i execute it, it gives me the wrong output

Member Avatar for iamthwee

You need to explain exactly what you mean by wrong output and show us what your file data.txt looks like.

Consider using a compiler with more warnings enabled.

gcc -W -Wall -ansi -pedantic -O2 foo.c
foo.c: In function `main':
foo.c:26: error: parse error before '/' token
foo.c: In function `getOption':
foo.c:62: warning: too many arguments for format
foo.c: In function `main':
foo.c:36: warning: statement with no effect
foo.c:38: warning: statement with no effect
foo.c:40: warning: statement with no effect
foo.c:42: warning: statement with no effect

This is line 62 - scanf("d",&option);
You forgot the %

This is line 36 - case 1: input;
You forgot the () to turn the function name into a function call.

review edit function

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.