使用以下输入运行代码后,会发生运行时错误:
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);
}