2012-03-20 58 views
2

基本上,我一直在C編程上做類,在那個類中我們使用Linux機器來編寫和編譯我們的代碼。在Windows 7上從linux到visual studio 2010的c編程問題

下面的程序是我們第一個任務的一部分,我在課堂上編譯並在Linux上運行它,但沒有問題,但已將它帶回家我不能在我的生活中讓它在Visual Studio 2010中編譯最終的,或者帶有MinGW編譯器的eclipse IDE。

在導致我的代碼失敗的兩個操作系統之間切換是否存在一些典型問題?或者讓我作爲新手,寫了一些不符合VS 2010或Eclipse的醜陋代碼?

嘗試修復我從VS 2010中得到的錯誤消息似乎是徒勞的,所以我傾向於從我的計算機中丟失重要的東西。我也設置VS 2010編譯C代碼,所以我不認爲這是問題。從VS2010

錯誤:

project1a.c(38):錯誤C2143:語法錯誤:缺少 ';'前 '類型'
project1a.c(41):錯誤C2065:I':未聲明的標識符
project1a.c(44):錯誤C2065:userArray':未聲明的標識符
project1a.c(44):錯誤C2065:I':未聲明的標識符
project1a.c(44):錯誤C2109:下標要求數組或指針類型
project1a.c(51):錯誤C2065:userArray':未聲明的標識符

有是'i'的多個實例:這些錯誤之間的未聲明的標識符錯誤

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

int n; 
float total, avg; 

int sumavg(void); 

int main(void) 
{ 
    //First time scan for the value to be assigned to n. 
    printf("Hey, Enter a number or 999 to exit:> "); 
    scanf("%d", &n); 

    //if n == 999 then exit the program 
    while(n != 999) 
    { 
     //enter the sumavg function. 
     sumavg(); 

     //Try to run the program again. 
     printf("Hey, Enter a number or 999 to exit:> "); 
     scanf("%d", &n); 
    } 

    //exit program. 
    return EXIT_SUCCESS;  
} 

int sumavg(void) 
{ 


    //Define a number that will be used for the array size. 
    printf("Hey, now enter %d more numbers:>\n", n); 

    //Define the size of array using the number assigned to the variable "n". 
    int userArray[n], i; 

    //Construct the array, one position at a time using the for loop. 
    for (i = 0; i < n; i++) 
    { 
     //Assign a value to userArray[i] while i < n(the size of the array). 
     scanf("%d", &userArray[i]); 
    } 

    //Calculate the sum by looping through each position in the userArray[i]. 
    for (i = 0; i < n; i++) 
    { 
     //Take the current position in the array and add it to the variable: "total" 
     total += userArray[i]; 
    } 

    //Calculate the average 
    avg = total/n; 

    //Print the sum followed by the average 
    printf("Sum is: %.1lf\n", total); 
    printf("The average is: %.1lf\n", avg); 

    //reset total and avg in case future iterations are performed. 
    total = 0; 
    avg = 0; 
} 
+1

您得到的錯誤是什麼? – 2012-03-20 03:27:57

+0

請發佈一些編譯器警告/錯誤。 – Mosby 2012-03-20 03:28:40

+0

您可以發佈您在編譯器中獲得的錯誤消息嗎?這將有助於縮小問題的範圍。 – Gangadhar 2012-03-20 03:29:03

回答

6

問題當編譯C代碼時,MSVC不支持C99,只支持C90(除了可能用於少數庫的東西)。您至少使用MSVC不支持的兩個C99功能:

  • 最大的問題是「可變長度數組」。解決這個問題通常需要對代碼進行相當多的修改,如果你以任何重要的方式使用它們的話。我會在稍後回顧。

  • 另一個是後「正常」的語句

C99允許聲明在其他類型的語句後的塊發生發生聲明; C90不允許這麼做 - 所有的聲明必須發生在一個程序段的開始處。這樣的話,你聲明userArray例如:

int sumavg(void) 
{ 
    //Define a number that will be used for the array size. 
    printf("Hey, now enter %d more numbers:>\n", n); 

    //Define the size of array using the number assigned to the variable "n". 
    int userArray[n], i; 

    //... 

,是不是在C90許可並在C模式編譯(它不會在編譯C++,因爲這種東西是用C支持時MSVC抱怨它++ )。

要解決這個問題只有一個街區開始後移動你的聲明:

int sumavg(void) 
{ 
    //Define the size of array using the number assigned to the variable "n". 
    int userArray[n], i; 

    //Define a number that will be used for the array size. 
    printf("Hey, now enter %d more numbers:>\n", n); 

    //... 

有時會要求您rejigger初始化,什麼不是。

要解決使用可變長度數組的問題需要更多的工作。在這種情況下,我認爲你可以聲明userArray作爲int*,它使用malloc()分配存儲獲得通過:

int* userArray; 

userArray = malloc(sizeof(int) * n); 

一些其他的東西:

  • 因爲totalavg沒有使用在sumavg()之外,它們應該是局部變量(顯式初始化爲0)
  • 您可能想要將n作爲參數傳遞給sumavg()而不是使用全局變量
  • 您聲明sumavg()返回int,但不返回任何內容。您應該將聲明更改爲void
+0

啊,謝謝你在這個答案中的深度,肯定幫助我理解出了什麼問題! – 2012-03-20 04:32:15

+0

作爲一個在Visual Studio中缺乏現代C標準支持的人,我認爲這是一個非常好的答案。 – 2012-03-20 08:18:51

-1

我猜這是因爲Visual Studio有「有趣」的入口點(即,不只是主要)。 如果您在VS中創建了正確的項目(例如控制檯應用程序),它會爲您創建正確的etrypoint。

您可以重構代碼要麼把你的所有「主」爲切入點,或致電您的主要因爲是從入口點(我建議重命名的main(),以避免混淆)

相關問題