2014-11-24 120 views
0

我對這段代碼有幾個問題。我在代碼的最後列出了錯誤。編譯C代碼時出錯

#include <stdio.h> 

int main() 

{ 
void addition(double number1, double number2); /* create the functions */ 
void subtraction(double number1, double number2); 
void division(double number1, double number2); 
void multiplication(double number1, double number2); 
int inputfunc=1; 
double inputnum1=0; 
double inputnum2=0; 
int number1; 
int number2; 
int answer; 

while (inputfunc >= 1 && inputfunc <= 4) /* If function to be performed are those below then continue performing loop */ 

{ 
printf("Press 1 to add two numbers.\n"); 
printf("Press 2 to subtract two numbers.\n"); 
printf("Press 3 to multiply two numbers.\n"); 
printf("Press 4 to divide two numbers.\n"); 
printf("Press 5 to exit.\n"); 
printf("Enter your choice\n"); 
scanf_s("%d", &inputfunc); 

if(inputfunc == 5) /* Exit program if requested via 5 function */ 
return(0); 

printf("Enter both numbers with a space in between."); 
scanf_s("%lf %lf", &inputnum1, &inputnum2); 

void(*func[4])(double, double)={&addition, &subtraction, &division, &multiplication}; 
    (*func[inputfunc-1])(inputnum1, inputnum2); 
    return(0); 
    } 

} 

void addition(double number1, double number2) 
{ 
    double answer; 
    answer=number1+number2; 
    printf("Addition of the two numbers = %lf + %lf = %lf\n", number1, number2, answer); 
    return; 
} 

void subtraction(double number1, double number2) 
{ 
    double answer; 
    answer=number1-number2; 
    printf("By subtracting the two numbers results are %lf - %lf = %lf\n", number1, 
    number2, answer); 
    return; 
} 

void multiplication(double number1, double number2) 
{ 
    double answer; 
    answer=number1*number2; 
    printf("By multiplying the two numbers results are %lf * %lf = %lf\n", number1, 
     number2, answer); 
    return; 
} 

void division(double number1, double number2) 
{ 
    double answer; 
    answer=number1/number2; 
    printf("By dividing the two numbers results are %lf/%lf = %lf\n", number1, 
     number2, answer); 
    return ; 
} 

錯誤C2143:語法錯誤:缺少 ';'之前的「type」 錯誤C2065:FUNC':未聲明的標識符 錯誤C2109:下標要求數組或指針類型

+2

請縮進你的代碼,因爲如果你用腳寫代碼就會發生類似這樣的錯誤。 – bitcell 2014-11-24 07:13:51

+1

搞笑:-)順便說一句,@DonCarter,你可以請upvote那些誰幫助你?這是標準做法,不僅要獎勵他們,而且要向其他人表明實際解決問題的同樣的問題。你可以upvote,也可以接受一個答案, – Mawg 2014-11-24 09:15:39

+0

與一些UniCell不同,我沒有天生的編寫代碼的能力。我發佈的代碼是Word中的複製和粘貼,它並不總是在寫入時進行轉置。初學者應該有更多的耐心。我只在這裏待了三個星期! – 2014-11-25 01:29:08

回答

0

在我已經從您的代碼貼在下面行,您正在結束與所述第二主要方法支撐。 這不是一個好主意,因爲你在代碼下面有代碼。

void(*func[4])(double, double)={&addition, &subtraction, &division, &multiplication}; 
(*func[inputfunc-1])(inputnum1, inputnum2); 
return(0); 
} //end while 

} //end main method 
0

代碼編譯和工作 - 看it in action here

幾點:

  • 這是C代碼,不C#
  • return(0);置於while循環中 - 這將防止它要求用戶進行多次運行。將此移動到while循環結束的下方。
  • 陣列保持的各種函數指針到算術運算符的聲明不應該被定位在while循環內 - 它不每次迭代之間變化 - 即移動void(*func[4])(double, double) = { &addition, &subtraction, &division, &multiplication };上述while
  • 更好縮進將使代碼更具可讀性
  • int number1; int number2; int answer;的最上面的聲明是多餘的,應該刪除(特別是因爲它們在4個算術函數中用作不同類型的本地變量名稱)。

我已到代碼段中的上述變化(scanf_s用簡單scanf替換爲IdeOne不使用MS編譯器)。

-1
// the scanf_s function is the secure function for string input, using scanf 

// this version compiles without any warnings, etc under linux gcc 

// this version checks for input errors 
// (except the actual value of the variable inputFunc) 
// I think the use of a switch() statement would be much more robust 
// rather than the use of the function ptr table, although not quite as flexable 

// Notice the function prototypes are outside of any function 
// so the compiler will create the proper code 

#include <stdio.h> 
#include <stdlib.h> // contains exit() and EXIT_FAILURE 

// function prototypes 
void addition(double number1, double number2); 
void subtraction(double number1, double number2); 
void division(double number1, double number2); 
void multiplication(double number1, double number2); 


void(*func[4])(double, double)={&addition, &subtraction, &division, &multiplication}; 

int main() 
{ 

    int inputFunc=1; 
    double inputnum1=0; 
    double inputnum2=0; 

    /* If function to be performed are those 
     below then continue performing loop */ 

    // note: 
    // if the inputFunc is (for instance) 6 then this while loop is exited 
    // however, there was no return statement 
    // for that execution path 

    while (inputFunc >= 1 && inputFunc <= 4) 
    { 
     printf("Press 1 to add two numbers.\n"); 
     printf("Press 2 to subtract two numbers.\n"); 
     printf("Press 3 to multiply two numbers.\n"); 
     printf("Press 4 to divide two numbers.\n"); 
     printf("Press 5 to exit.\n"); 
     printf("Enter your choice\n"); 

     if(1 != scanf(" %d", &inputFunc)) 
     { 
      perror("scanf_s"); 
      exit(EXIT_FAILURE) ; 
     } 

     // implied else, scanf for which command was successful 

     // note: there should be some checking here 
     //  to assure that the input was in the valid range '1...5' 

     if(inputFunc == 5) 
     { /* then, Exit while loop if requested via 5 function */ 
      // note: good program practice is to put the return 
      //  at the bottom of the function 
      break; 
     } 

     printf("Enter both numbers with a space in between."); 
     if(2 != scanf(" %lf %lf", &inputnum1, &inputnum2)) 
     { 
      perror("scanf for 2 input numbers"); 
      exit(EXIT_FAILURE); 
     } 

     // implied else, scanf for two input numbers successful 

     // exec the desired function 
     (*func[inputFunc-1])(inputnum1, inputnum2); 
    } 
    return(0); // to avoid compiler warning 
} 

void addition(double number1, double number2) 
{ 
    double answer; 
    answer=number1+number2; 
    printf("Addition of the two numbers = %lf + %lf = %lf\n", number1, number2, answer); 
    return; 
} 

void subtraction(double number1, double number2) 
{ 
    double answer; 
    answer=number1-number2; 
    printf("By subtracting the two numbers results are %lf - %lf = %lf\n", 
     number1, 
     number2, 
     answer); 
} 

void multiplication(double number1, double number2) 
{ 
    double answer; 
    answer=number1*number2; 
    printf("By multiplying the two numbers results are %lf * %lf = %lf\n", 
     number1, 
     number2, 
     answer); 
} 

void division(double number1, double number2) 
{ 
    // note: this should check that number2 is NOT 0, to avoid divide by zero error 
    double answer; 
    answer=number1/number2; 
    printf("By dividing the two numbers results are %lf/%lf = %lf\n", 
     number1, 
     number2, 
     answer); 
} 
+0

這工作很好,謝謝你和其他人的投入。我已經並將繼續閱讀您的回覆,以便避免將來出現此類錯誤。再次感謝! – 2014-11-25 01:22:38