0

餐厅.c

该程序允许您创建餐厅菜单,将其存储在文件中,然后对菜单上的每个项目进行评分。它使用文件函数将所有内容输出到文件中,然后几乎可以通过任何程序查看该文件。当程序到达 nametofile() 'fprintf(restaurant, "%s Restaurant\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

name是一个char数组。当您将它传递给scanf或其他函数时,它会衰减为指针,因此您不需要&运算符:

scanf("%19s", name);

当您使用 读取字符串时scanf,最好通过大小限制:这样可以避免缓冲区溢出。由于name被声明为char[20],因此您传递了 19,因为char需要为空终止符保留一个。

于 2013-07-02T02:53:38.587 回答