在< = token之前的预期表达式

时间:2014-04-17 04:39:53

标签: c arrays linux compiler-construction

当我在linux中编译它时,我收到一个错误:

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

它会扫描文件a.txt,并且应该输出到b.txta.txt的内容是:

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

5 个答案:

答案 0 :(得分:2)

遵循c语法,您应该像下面一样修改它。 :) 比较运算符应该在两个变量或数字之间使用。

    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)

答案 1 :(得分:2)

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; 
}

答案 2 :(得分:1)

你需要像这样编写你的陈述

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

您应该为易读性添加额外的括号

答案 3 :(得分:1)

你可能会考虑for()循环的这个逻辑:

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;
}

答案 4 :(得分:0)

在行下面更新

    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)