Question

When I compile this in linux I get an error:

project9v2.c: In function `main`:
project9v2.c:34:33: error: expected expression before `<=` token
project9v2.c:38:33: error: expected expression before `<=` token
project9v2.c:42:33: error: expected expression before `<=` token
project9v2.c:46:33: error: expected expression before `<=` token

It scans the file a.txt and is supposed to output to b.txt. The contents of a.txt are:

97 85 70 84 33 100 283 53 81 69 89 73 65 86 77 556 -1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *inFile, *outFile;
    int current;
    int sum = 0, b, i;
    int A=0, B=0, C=0, D=0, F=0;
    int theGrades[100];


    inFile = fopen("a.txt", "r");
    outFile = fopen("b.txt", "w");

    if (inFile != NULL) 
    {
        b = fscanf(inFile, "%d", &current); 
    }

    while(b != -1)
    {
        theGrades[sum] = current;
        sum++;
        b = fscanf(inFile, "%d", &current);
    }

    for(i=0; i<sum; i++)
    {
        if (theGrades[i] <0)
        {
            break;
        }
        else if (theGrades[i] >=90 && <=100)
        {
            A++;
        }
        else if (theGrades[i] >=80 && <=89)
        {
            B++;
        }
        else if (theGrades[i] >=70 && <=79)
        {
            C++;
        }
        else if (theGrades[i] >=60 && <=69)
        {
            D++;
        }
        else  
        {
            F++;
        }
    }
    fprintf(outFile, "The total number of grades is:" , sum);
    fprintf(outFile, "Number of A’s = %d\n" ,A);
    fprintf(outFile, "Number of B’s = %d\n" ,B);
    fprintf(outFile, "Number of C’s = %d\n" ,C);
    fprintf(outFile, "Number of D’s = %d\n" ,D);
    fprintf(outFile, "Number of F’s = %d\n" ,F);

    fclose(inFile);
    fclose(outFile);
    }
Was it helpful?

Solution

following c syntax, you should modify it like below. :) compare operator should be used between two variable or number.

    else if (theGrades[i] >=90 && theGrades[i]<=100)
    {
        A++;
    }
    else if (theGrades[i] >=80 && theGrades[i]<=89)
    {
        B++;
    }
    else if (theGrades[i] >=70 && theGrades[i]<=79)
    {
        C++;
    }
    else if (theGrades[i] >=60 && theGrades[i]<=69)

OTHER TIPS

if ( theGrades[i] < 0 )
    break;

switch( theGrades[i] / 10 )
{
    case 10: 
    case 9:  ++A; break;
    case 8:  ++B; break;
    case 7:  ++C; break;
    case 6:  ++D; break;
    default: ++F; 
}

you need to write your statements like this

    else if ((theGrades[i] >=90) && (theGrades[i] <=100))

You should add the additional brackets for legibility

You might consider this logic for your for() loop:

for(i=0; i<sum; i++)
{
    if (theGrades[i] > 100)
       break;

    if (theGrades[i] >=90)
    {
        A++;
        continue;
    }

    if (theGrades[i] >=80)
    {
        B++;
        continue;
    }

    if (theGrades[i] >=70)
    {
        C++;
        continue;
    }

    if (theGrades[i] >=60)
    {
        D++;
        continue;
    }
    if (theGrades[i] >= 0)  
    {
        F++;
        continue;
    }

    break;
}

update below line

    else if (theGrades[i] >=90 && theGrades[i] <=100)
    {
        A++;
    }
    else if (theGrades[i] >=80 && theGrades[i] <=89)
    {
        B++;
    }
    else if (theGrades[i] >=70 && theGrades[i] <=79)
    {
        C++;
    }
    else if (theGrades[i] >=60 && theGrades[i] <=69)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top