基本上,我一直在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;
}
您得到的錯誤是什麼? – 2012-03-20 03:27:57
請發佈一些編譯器警告/錯誤。 – Mosby 2012-03-20 03:28:40
您可以發佈您在編譯器中獲得的錯誤消息嗎?這將有助於縮小問題的範圍。 – Gangadhar 2012-03-20 03:29:03