2013-11-04 141 views
1

我收到以下錯誤,當我試圖編譯我的項目在Visual Studio 2012:的Visual Studio 2012 「語法錯誤」

1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------ 
1> main.c 
1>e:\main.c(28): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\stdio.h(290) : see declaration of 'scanf' 
1>e:\main.c(30): error C2143: syntax error : missing ';' before 'type' 
1>e:\main.c(31): error C2143: syntax error : missing ';' before 'type' 
1>e:\main.c(33): error C2065: 'answerMin' : undeclared identifier 
1>e:\main.c(33): error C2065: 'answerMax' : undeclared identifier 
1>e:\main.c(35): error C2065: 'answerMax' : undeclared identifier 
1>e:\main.c(36): error C2065: 'answerMin' : undeclared identifier 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

這裏是main.c中的代碼

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

double a ; 
double b ; 
double ComputeMaximum(double, double) ; 
double ComputeMinimum(double, double) ; 

int main(void) 
{ 

    printf("\nPlease enter two numeric values for comparison\n") ; 

    scanf("%d%d", &a, &b); 

    double answerMax = ComputeMaximum(a, b) ; 
    double answerMin = ComputeMinimum(a, b) ; 

    printf("Of %d and %d the minimum is %d and the maximum is %d\n", a, b, answerMin, answerMax) ; 

    printf("%d", answerMax) ; 
    printf("%d", answerMin) ; 

    system("pause"); 
    return 0; 

} 

這裏是ComputeMinimum.c

double ComputeMinimum(double a, double b) 
{ 
    double result = 0 ; 
    (a > b) ? (b = result) : (a = result) ; 
    return result ; 

} 

這裏的代碼被用於ComputeMaximum.c

的代碼
double ComputeMaximum(double a, double b) 
{ 
    double result = 0 ; 
    (a > b) ? (a = result) : (b = result) ; 
    return result ; 

} 
+2

在MS世界的頭,C仍然是C89和所有變量都必須在聲明函數的頂部。忍受這一點,或得到一個真正的C編譯器。 –

+3

無關:您可能需要更長時間觀察ComputeMaximum和ComputeMinimum,因爲除了返回0之外,它們都不執行任何操作(並且在進程中將兩個按值參數中的一個設置爲0)。 – WhozCraig

+0

哎呀,謝謝大衛! –

回答

0
scanf("%d%d", &a, &b); 

必須是

scanf("%lf %lf", &a, &b); 

因爲ab雙打(與值之間的空間)。

同爲

printf("Of %d and %d the minimum is %d and the maximum is %d\n", a, b, answerMin, answerMax) ; 

變化%d%f

還要注意的是,在

double ComputeMinimum(double a, double b) 
{ 
    double result = 0 ; 
    (a > b) ? (b = result) : (a = result) ; 
    return result ; 

} 

result始終爲0,改變

(a > b) ? (result = b) : (result = a) ; 

同爲computeMaximum

當然,你需要包括含有main.c這些功能(編譯器警告這個事實)

+0

謝謝你Alter Mann! –

+0

歡迎您:) –

1

使用C時,必須在任何指令或函數調用之前聲明變量。示例:

int main(void) 
{ 
    double answerMax; 
    double answerMin; 
    ..... 
    system("pause"); 
    return 0; 
} 

關於棄用函數。您可以在項目屬性中的預處理器定義中添加_CRT_SECURE_NO_WARNING。