Visual Studio只有错误。这是我的项目

时间:2014-12-10 16:00:06

标签: c visual-studio-2010 visual-studio

Visual Studio给了我一个非常非常头疼的问题。代码在Codelite,OS X终端,Xcode等上编译得很好,但Visual Studio C ++ 2010和使用.c源文件,它是一个......妓女。我必须使这个代码在Visual Studio上运行,因为它是我的主要优先事项。我不知道它为什么不起作用。不,我不是要求你为我编写代码,因为正如我所说的那样,使用Visual Studio之外的任何东西都很好。我想知道的是为什么它不能在VS C ++ 2010上运行?

这是我的代码。

#include <stdio.h>
#include <math.h>
void list(); //This function prompts has the visual necessities to prompt the user to select what to do.
int max_age(int a[],int size); //This function uses a for loop statement to search the index and find the biggest value.
int min_age (int a[], int size); //This function uses the exact same method as the one above but to look for the minimum value instead.
float average (int a[], int size); //This function calculates the average age within the sample collected, a.k.a the array
float standardDeviation (int a[], int size); //By using the given calculation in the PDF, this function is sued to calculate the standard deviation.


int main()
{ //A switch statement is used because it is much more natural (for me) to code it.
    int array[30] = {55, 43, 22, 10, 5, 1, 88, 91, 76, 68, 51, 35, 31, 15, 18, 21, 22, 33, 4, 28, 28, 77, 71, 44, 48, 51, 27, 28, 6, 36};
    int input=0;
    do{
        list();
        scanf("%d",&input);
        switch(input){
            case 1:
                printf("The Mean is: %f\n",average(array,30));
                break;
        case 2:
            printf("Maximum age is: %d\n", max_age(array,30));
            break;
        case 3:
            printf("Minimum age is: %d\n", min_age(array,30));
            break;
        case 4:
            printf("Standard Deviation is: %f\n", standardDeviation(array,30));
            break;
        case 5:
            printf("**TERMINATED**\n");
            break;
        default:
            printf("Please enter the correct input[1-5]\n");
    }
 }while(input!=5);
 return 0;
 }


float average(int a[],int size)
{
//This function sums the array and then devides it by 30.0 to find the average age.
int sum=0;
int i;
for(int i=0;i<30;i++)
{
    sum+=a[i];
}
return (sum/30.0);
}


void list()
    {
    printf("Please choose an option\n");
    printf("\t\t 1. Calculate mean value\n");
    printf("\t\t 2. Calculate maximum age\n");
    printf("\t\t 3. Calculate minimum age\n");
    printf("\t\t 4. Calculate Standard Deviation \n");
    printf("\t\t 5. EXIT\n\n\n\t\t");
    return;
    }

int max_age (int a[],int size)
    { //This function is used to find the max age by using a for loop statement that will keep incrementing until the array finishes and we find the max age, where the variable maximum_age will continue to redefine itself until it finds the absolute maximum age given within the array.
        int i;
        int maximum_age= a[0];
        for (i=0; i<size; i++)
            {
                if(a[i]>maximum_age)
                maximum_age = a[i];
    }

        return maximum_age;
}

int min_age (int a[],int size)
{
//To calculate the minimum age, I have used a for loop statement that continues to increment until it finds the absolute minimum value within the given array which is a[i].

    int i;
    int minimum_age= a[0];
    for (i=0; i<size; i++)
    {
        if(a[i]<minimum_age)
        minimum_age = a[i];
    }

    return minimum_age;
}

float standardDeviation(int a[],int size)
{
    int i;
    float up=0.0;
    float mean = average(a,size);
    for (int i = 0; i < size; ++i)
    {
        up = up + pow(a[i]-mean,2);
    }
float result = sqrt(up/(size-1));
return (result);
}

这是我在visual studio上的错误,请记住我在整个错误列表中得到了相同的错误。

错误C2143:语法错误:缺少&#39;;&#39;之前&#39;键入&#39;  错误C2143:语法错误:缺少&#39 ;;&#39;在&#39;键入&#39;

之前

2 个答案:

答案 0 :(得分:1)

似乎MS VS 2010不支持C99,所以所有声明都应该在代码块的开头。

例如

float standardDeviation(int a[],int size)
{
    int i;
    float up=0.0;
    float mean = average(a,size);
    float result;

    //...

    result = sqrt(up/(size-1));
    return (result);
}

像这样的结构

for(int i=0;i<30;i++)

也错了。您不能在循环中使用声明。

考虑到您已经在函数average中定义了i。因此,您可以在循环中使用此变量而不是声明的变量

float average(int a[],int size)
{
//This function sums the array and then devides it by 30.0 to find the average age.
int sum=0;
int i;
for(int i=0;i<30;i++)

答案 1 :(得分:0)

感谢Vlad,我能够通过从for循环中删除声明并删除结果变量并返回参数来解决问题。

这是我修复的一个例子。

float standardDeviation(int a[],int size){
int i;
float up=0.0;
float mean = average(a,size);
for (i = 0; i < size; ++i) // removed the declaration from here
{
    up = up + pow(a[i]-mean,2);
}

return (sqrt(up/(size-1))); //instead of returning a variable, I am returning the argument instead.
}