C ++,错误c2143(语法错误),c2065,c2109

时间:2013-12-18 05:37:14

标签: arrays syntax-error

我是编程新手

我收到这些编译器错误:

错误1错误C2143:语法错误:缺少';'在“类型”之前 错误2错误C2065:'ary':未声明的标识符
错误3错误C2109:下标需要数组或指针类型
错误4错误C2065:'ary':未声明的标识符
错误5错误C2109:下标需要数组或指针类型

这是我的代码:

   #include <stdio.h>
    int main(void)
    {
    int num, sum, i, e, n, avrg, numsnum, avrgu, chek;
    sum = 0; 
    i = 0;
    avrgu = 0;
    chek = 0;
    n = 0;
    numsnum = 0;
    int ary[5];
    e = 0;

    do{
        scanf("%d", &num);
        ary[n] = num;
        n++;
        i++;
        sum += num;
        numsnum++;
    } while (i != 4);

    avrg = (sum) / numsnum;
    for (e = 0; e = 4; e++){
        if (ary[chek] > avrg){
            avrgu++;
        }
        chek++;
    }
    printf("%d numbers are above the AVERAGE of all.\n", avrgu);


    return 0;
    }

提前致谢!

1 个答案:

答案 0 :(得分:1)

我建议您找到一些很好的入门C ++教程。互联网充满了他们,只需在谷歌上快速搜索。这是我很快找到的:http://www.cprogramming.com/tutorial/lesson1.html

上面代码的问题在于您正在索引数组ary的末尾。这发生在你的for循环中:for(e = 0; e = 4; e ++)。你的for循环检查是为e分配值4.您可能要做的是e == 4,但即便如此也是如此。你想要做的是在e < 4时继续循环。

以下是您的代码的修订版本:

// C++ version of stdio.h
#include <iostream>

int main(int argc, char** argv)
{
    // Create an array of five doubles and
    // initialize all the doubles to 0
    double array[5] = {};

    double num = 0;
    double sum = 0;
    double count = 5;
    for (int i = 0; i < count; ++i)
    {
        // Read the user input from the console
        std::cin >> num;
        array[i] = num;
        sum += num;
    }

    double average = sum / count;

    int numbersAboveAverage = 0;
    for (int i = 0; i < count; ++i)
    {
        if (array[i] > average)
        {
            numbersAboveAverage++;
        }
    }

    // Print the numbersAboveAverage value to the console.
    std::cout << numbersAboveAverage
         << " numbers are above the average.\n" << std::endl;

    return 0;
}

我建议你多做一些阅读,并确保你继续编程!最好的学习方法是做。我希望这有帮助,欢呼!