预期的表达在<= token

问题描述 投票:0回答:5

当我在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.txt
a.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);
    }
arrays c linux
5个回答
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)
    

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

1
投票

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

您应该添加其他括号以使其可读性

您可能会考虑()循环的此逻辑:

1
投票
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; }

updatepline

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)


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.