0

レストラン.c

このプログラムを使用すると、レストランのメニューを作成してファイルに保存し、メニューの各項目を評価できます。ファイル関数を使用して、ほとんどすべてのプログラムで表示できるファイルにすべてを出力します。プログラムが nametofile() の行に到達すると、'fprintf(restauraunt, "%s Restauraunt\n\n",name);' プログラムはセグメンテーション違反を起こします。なぜこれを行っているのかわかりません。いくつかの異なるデバッグ方法を試みましたが、どれもうまくいきませんでした。提案がある場合は、以下にコメントしてください。

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

FILE *restauraunt;
char name[20];
char item[20];
char price[20];
int count=0;

void nametofile();
void rate();
void itemtofile();
void counter();
void renamefile();

int main()
{
    int i,j;
    int num;
    printf("Restauraunt Creator\n\n");
    printf("Enter the name of your restauraunt:\n");
    scanf("%s",&name);
    nametofile();
    printf("\nEnter the number of items to be included in your menu:\n");
    scanf("%d", &num);
    /* Cycles through each entry to the menu */
    for(i=0;i<num;i++)
    {
        counter();
        fpurge(stdin);
        printf("\nPlease enter the name of item number %d:\n",count);
        scanf("%s", &item);
        printf("\nPlease enter the price of item number %d:\n",count);
        scanf("%s", &price);
        itemtofile();
        rate();
    }
        renamefile();
}

/*void nametofile()
{
    restauraunt = fopen("restauraunt","w");
    fprintf(restauraunt, "%s Restauraunt\n\n",name);
    fclose(restauraunt);
}*/
/* The function that sends the restaurant name to the file */
void nametofile()
{

    int i;
    i = strlen(name);
    name[i+1] = '\0';
    restauraunt = fopen("restauraunt","w");
    /* the line that gives a segmentation fault */
    fprintf(restauraunt, "%s Restauraunt\n\n",name);
    fclose(restauraunt);
}

/* rates each menu item */
void rate()
{

    int rating;
    srandom((unsigned)time(NULL));
    restauraunt = fopen("restauraunt", "a");
    rating = random() % 5 + 1;
    fprintf(restauraunt,"Your food's rating was:\t%d stars!",rating);
    switch(rating)
    {
        case 1:
        {
            fprintf(restauraunt," Here's why: Your food was not very good tasting and the price was ridiculously high.\n");
            break;
        }
        case 2:
        {
            fprintf(restauraunt," Here's why: Your food was mildly good tasting and the price was too high.\n");
            break;
        }
        case 3:
        {
            fprintf(restauraunt," Here's why: Your food was somewhat good tasting and the price was fair.\n");
            break;
        }
        case 4:
        {
            fprintf(restauraunt," Here's why: Your food was quite good tasting and the price was very nice.\n");
            break;
        }
        case 5:
        {
            fprintf(restauraunt," Here's why: Your food was very delicious and the price was amazingly low.\n");
            break;
        }
    }
}
/* sends each item to the file */
void itemtofile()
{

    restauraunt = fopen("restauraunt","a");
    fprintf(restauraunt, "%s: $%s\nRating:",item,price);
    fclose(restauraunt);
}
/* counts up one each time function is called */
void counter()
{
    count += 1;
}
/* renames the file at the end */
void renamefile()
{
    int x,y;
    char bridge[] = { "menu" };
    name[0] = tolower(name[0]);

    x = strcat(name,bridge);
    y = rename("restauraunt",name);
}
4

1 に答える 1

3

namechar配列です。または他の関数に渡すと、ポインターに減衰するため、演算子scanfは必要ありません。&

scanf("%19s", name);

で文字列を読み取るときscanfは、サイズ制限を超えることをお勧めします。これにより、バッファ オーバーランを回避できます。nameは として宣言されているため、ヌル ターミネータ用にもう 1 つ予約する必要があるchar[20]ため、19 を渡します。char

于 2013-07-02T02:53:38.587 に答える