C 创建新函数时获取错误消息

C 创建新函数时获取错误消息,c,C,我有一个家庭作业,我必须修改代码,并添加一个额外的选项,将一个整数除以二。然而,当我添加我的部分时,我不断收到一条错误消息 代码如下: #include <stdio.h> int main () { /* variable definition: */ int intValue, menuSelect,Results; float floatValue; intValue = 1; // While a positive number while (intValu

我有一个家庭作业,我必须修改代码,并添加一个额外的选项,将一个整数除以二。然而,当我添加我的部分时,我不断收到一条错误消息

代码如下:

#include <stdio.h>

int main ()

{

/* variable definition: */ 

int intValue, menuSelect,Results;
float floatValue;

intValue = 1; 

// While a positive number

while (intValue > 0)

{   

 printf ("Enter a positive Integer\n: ");

 scanf("%d", &intValue);

 if (intValue > 0)

{

 printf ("Enter 1 to calculate Square, 2 to Calculate Cube, 3 to divide by 2 \n: ");

 scanf("%d", &menuSelect);

 if (menuSelect == 1)

 {

   // Call the Square Function

   Results = Square(intValue);

   printf("Square of %d is %d\n",intValue,Results);

 }

 else if (menuSelect == 2)

 {

   // Call the Cube function

   Results = Cube(intValue);

   printf("Cube of %d is %d\n",intValue,Results);

 }

 else if (menuSelect == 3)

 {

    //Call the half function

    Results = divide2(floatValue);

    printf("Half of %d is %f\n", intValue,Results);

 }

 else 

   printf("Invalid menu item, only 1 or 2 is accepted\n");

 }     

 }     

return 0;

}

/* function returning the Square of a number */

int Square(int value)

{

return value*value;

} 

/* function returning the Cube of a number */

int Cube(int value)

{
return value*value*value;

}

//Function returning the half of a number

float divide2(float value)

{
return value/2;

}

代码有什么问题?我在运行原始代码时没有收到消息

如果在调用某个函数的点之后定义该函数,则必须添加该函数的原型

例如:

ABC();

void ABC(){
//do some stuff
}
除非我们添加
void ABC(),否则将不起作用

这也可以很好地工作:

void ABC(){
//do something
}

ABC();
下面是有效的代码(我更改了一些变量名,因为您不能对int和float使用
结果
,我还更改了循环以检查数字是否大于或等于0,因为0被认为是正的,而square/cube/half被定义为0)。我将在顶部定义一个函数;因为它们很小,所以很容易在一行中定义,一般来说,对于一行,我会在顶部定义它们,但其余的我只会在顶部创建原型,然后再定义(以便您可以看到这两种样式)


如果您想指定精度,只需将
%.2f
中的“2”更改为您喜欢的任何值。

您应该使用包含以下内容的原型:

int Square(int value);
int Cube(int value);
float Divide2(float value);
如果你不想使用原型。一个选项是把所有的方法放在主函数上,这是不推荐的,但它是有效的


原型是对编译器的引用,它们预先告诉应该加载的函数。

在编写C代码时,想象一下你告诉编译器一次只理解一部分代码。您需要引入代码的其他部分在使用之前使用的函数。在您的特定示例中,
main
Square
之前,因此编译器在第一次看到您的
Square
调用时并不知道该做什么。尝试根据此原则对函数进行排序,看看它是否解决了问题。只需在
main
函数之前为所有函数使用原型,然后再定义它们。在实际调用它们之前添加原型或定义函数,即如果从main()调用,则在main()之前定义它们.通过在主程序之前引入函数,我能够使代码正常工作。谢谢你的帮助<代码>ABC()是非法的,原型将是
intabc(void)或<代码>作废ABC(作废)它不是非法的,它只是不检查是否为函数提供了任何值。
#include <stdio.h>

int square(int value);
int cube(int value);
/* Function returning the half of a number */
float divide2(float value) { return value / 2; }

int main()
{
    int number, menu_select, int_result;
    float float_result;

    number = 1;

    // While a positive number
    while (number >= 0)
    {
            printf("Enter a positive Integer\n: ");
            scanf("%d", &number);

            if (number > 0)
            {
                    printf("Enter 1 to calculate Square, 2 to Calculate Cube, 3 to divide by 2\n: ");
                    scanf("%d", &menu_select);

                    switch (menu_select)
                    {
                    case 1:
                            // Call the square Function
                            int_result = square(number);
                            printf("Square of %d is %d\n", number, int_result);
                            break;
                    case 2:
                            // Call the cube function
                            int_result = cube(number);
                            printf("cube of %d is %d\n", number, int_result);
                            break;
                    case 3:
                            // Call the half function
                            float_result = divide2(number);
                            printf("Half of %d is %f\n", number, float_result);
                            break;
                    default:
                            printf("Invalid menu item, only 1 or 2 is accepted\n");
                    }
            }
    }

    return 0;
}

/* Function returning the square of a number */
int square(int value)
{
    return value * value;
}

/* Function returning the cube of a number */
int cube(int value)
{
    return value * value * value;
}
printf("Here is a float value: %.2f\n", float_var);
int Square(int value);
int Cube(int value);
float Divide2(float value);