-2

I'm trying to figure out how to work out the selection for when the input information is not present in the text file, the user will be notified. For now when I run my code and input an item that's not in the text file, it just print out the last item in the text file. I'm really stuck after several hours of thinking how to solve this problem. Your help is greatly appreciated.

#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);
    }
}
  • 2
    `while(!feof(...` is [always a bad idea](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – Paul R Apr 24 '16 at 09:33
  • Why is that so? Sorry I'm really new to programming. @PaulR – Safiuddin Mansor Apr 24 '16 at 09:35
  • See [this question](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) for a full explanation and how to do it right. – Paul R Apr 24 '16 at 09:36
  • 1
    This question was caused by a problem that can no longer be reproduced or a **simple typographical error**. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting. – Box Box Box Box Apr 24 '16 at 09:50

1 Answers1

1

if(found=1) should be if(found == 1)

Mukul Gupta
  • 2,310
  • 3
  • 24
  • 39
  • Wow, such a silly mistake. Thanks a lot @MukulGupta btw do you know how I can use tolower() function to avoid case related problem when comparing the strings? – Safiuddin Mansor Apr 24 '16 at 09:37
  • I don't think there is a case-insensitive string comparison for a char array in C standard. You could iterate over both strings and convert them to lowercase before calling strcmp over them. – Mukul Gupta Apr 24 '16 at 09:47
  • 1
    @SafiuddinMansor Use `-Wall` when you compile and you will have a warning about this. – jdarthenay Apr 24 '16 at 09:50
  • @SafiuddinMansor Also, keep a practice of writing these expressions in reverse, i.e. `if (1 = found)` will give a compiler error. – Shreevardhan Apr 24 '16 at 12:29