I have edited my code because of fear that someone from the same course as me would copy my code and hand it in. Thanks for the answer you have given me, its a great help.
My program should allow me to add the new item into the system along with its name, price and quantity. But during execution, my code seems to damage the file. I have no idea what's wrong and would not like to write a more complicated code. The program just skip the quantity and return to the menu. Not allowing me to input the quantity .
#include <stdlib.h>
#include <string,h>
struct Item//declaring a structure
{
char code[25];//variables inside a structure
char name[25];
double price;
int quantity;
};
int main (void)
{
struct Item item;
FILE *fgst;
printf("-------------------------------n");
printf(" ADD PRODUCTn");
printf("-------------------------------n");
fgst = fopen("gst.txt", "r");
if(fgst==NULL)
{
printf("File cannot be foundn");
}
else //else statement
{
printf("Add Code:n");
scanf("%s", item.code);
printf("name:n");
scanf("%s", item.name);
printf("price:n");
scanf("%.2f",&item.price);
printf("quantity:n");
scanf("%d", &item.quantity);
fprintf(fgst,"%s;%s;%.2f;%dn",item.code,item.name,item.price,item.quantity);
fclose(fgst);
}//end else statement
break;
}
The content of the one of the file is as follow:
AS520;Jelly tartar;5.35;42
Aucun commentaire:
Enregistrer un commentaire