次の入力を使用してコードを実行すると、ランタイム エラーが発生します。
id : 123
name : stackoverflow
quantity : 123
price : 123
これを解決するには助けが必要です。
以前は、アンパサンド/& を次の場所に置きました。
fprintf(fp, "%d %s %d %d\n\n", a.id, a.name, a.quantity, a.price);
そして面白い数字が出てきました:
2686724 stackoverflow 2686688 2686720
コード:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <conio.h>
struct product
{
int quantity, reorder, i;
char name[20];
float price, id;
};
int main()
{
FILE * fp;
int i=0;
struct product a;
system("cls");
char checker;
int counter;
do
{
fp = fopen("addproduct.txt","a+t");
system("cls");
printf("Enter product ID : ");
scanf(" %d", &a.id);
printf("Enter product name : ");
scanf(" %s", a.name);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
printf("Enter product price : ");
scanf(" %d", &a.price);
fprintf(fp, "%d %s %d %d\n\n", a.id, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
fclose(fp);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
}
while(checker=='Y');
return(0);
}