[英]Errors during compilation of C code
我在与此代码有一些问题。 我在代码末尾包含了错误。
#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:语法错误:缺少';' 在“类型”错误之前C2065:“ func”:未声明的标识符错误C2109:下标需要数组或指针类型
在我从您的代码下面发布的那一行上,您将使用第二个括号结束main方法。 这不是一个好主意,因为您在该括号下有代码。
void(*func[4])(double, double)={&addition, &subtraction, &division, &multiplication};
(*func[inputfunc-1])(inputnum1, inputnum2);
return(0);
} //end while
} //end main method
该代码可以编译和运行- 在此处查看运行情况
一些要点:
return(0);
被放置在while
循环中-这将防止它询问用户多次运行。 将其移动到while
循环的末尾。 void(*func[4])(double, double) = { &addition, &subtraction, &division, &multiplication };
高于while
int number1; int number2; int answer;
的最高声明int number1; int number2; int answer;
int number1; int number2; int answer;
是多余的,应将其删除(尤其是在4个算术函数中将它们用作具有不同类型的局部变量名称时)。 我对代码段进行了上述更改(由于IdeOne不使用MS编译器,所以scanf_s
已替换为简单的scanf
)。
// 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.