0

次の入力を使用してコードを実行すると、ランタイム エラーが発生します。

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);
}
4

3 に答える 3

0

単純な間違いがあります。ここでの主な問題は、integer と float の衝突です。「id」と「price」を「float」から「int」に変更すると、問題が解決します! また、price と id に float の入力と出力を使用している場合は、%d を %f で変更できます。「id」を「float」と宣言した理由がわかりません:|

于 2016-08-15T17:16:00.960 に答える