Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I got a error on ‘else’ without a previous if

+2 votes
asked Feb 25, 2022 by Jordan Chapman (180 points)
char fileNm[] = "Lab2Part3GroceryData.txt";
    FILE * inGroc = fopen(fileNm, "r");
    if (inGroc == NULL);
        printf("ERROR: Input file %s not found\n",fileNm);
    else
    {
    // Set up header for a table to print output
    tableHeader();
    while (!feof(inGroc))
    {

1 Answer

+3 votes
answered Feb 25, 2022 by Peter Minarik (87,340 points)

There is a semicolon after the if's condition:

if (inGroc == NULL);

Remove it to fix your problem.

The semicolon there says, when the condition of your if statement is true, do nothing.

Make sure you do not have "random" semicolons in your code.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...