C 选择 if else

标签 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);
    }
}

最佳答案

if(found=1) 应该是 if(found == 1)

关于C 选择 if else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36821406/

相关文章:

c - C语言中的软件中断?

C - 动态整数数组错误(代码 :Blocks/Visual Studio)

检查 Linux 中的端口可用性

c# - 禁用 RichTextBox 或 TextBox 中的选择突出显示

c++ - 如何从gtkmm TreeView 获取选定的行

css - 是否可以在 Firefox 中设置搜索选择的样式?

c - 指向结构数据时 bf561 COREB FreeRTOS 实现中的运行时错误

while(1)之前的代码不运行

r - 根据 2 个不同变量的值从矩阵中选择

vba - VBA : Run-Time error '91' (Code working in template but not “new from template” )