C选择否则

时间:2016-04-24 09:31:55

标签: c selection

我试图弄清楚当文本文件中不存在输入信息时如何计算选择,将通知用户。现在,当我运行我的代码并输入一个不在文本文件中的项目时,它只打印出文本文件中的最后一项。经过几个小时思考如何解决这个问题后,我真的陷入了困境。非常感谢您的帮助。

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

int main()
{
    FILE *items;

    char pName[20];
    float pPrice;
    char p1Name[20];
    int found=0, i=9;
    char respond='y';

    items=fopen("Product_Name_Price.txt", "r");

    if(items==NULL)
    {
        fprintf(stderr, "Can't open file Product_Name_Price.txt!\n");
        exit(1);
    }

    printf("File has been successfully opened\n");

    while(tolower(respond) == 'y')
    {
        items=fopen("Product_Name_Price.txt", "r");
        printf("Enter the name of the product you are looking for\n");
        scanf("%s", p1Name);

        while(!feof(items))
        {
            fscanf(items, "%s%f", pName, &pPrice);
            i=strcmp(p1Name, pName);

            if(i == 0)
            {
                found=1;
                break;
            }
            else
            {
                found=0;
            }
        }
        if(found=1)
        {
            printf("%s\t%.2f\n", pName, pPrice);
        }
        else
        {
            printf("No such product information in the database\n");
        }

        printf("Do you want to look for more item? (Y/N)\n");
        scanf(" %c", &respond);
        fclose(items);
    }
}

1 个答案:

答案 0 :(得分:1)

if(found=1)应为if(found == 1)