创建新函数时收到错误消息

时间:2016-03-21 03:37:25

标签: c

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

以下是代码:

#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;

}

我得到的错误是:

prog.c: In function 'main':
prog.c:38:18: warning: implicit declaration of function 'Square'       [-Wimplicit-function-declaration]
    Results = Square(intValue);
              ^
prog.c:50:18: warning: implicit declaration of function 'Cube' [-Wimplicit-function-declaration]
    Results = Cube(intValue);
              ^
prog.c:62:17: warning: implicit declaration of function 'divide2' [-Wimplicit-function-declaration]
   Results = divide2(floatValue);
             ^
prog.c:64:14: warning: format '%f' expects argument of type 'double', but argument 3 has type 'int' [-Wformat=]
   printf("Half of %d is %f\n", intValue,Results);
          ^
prog.c: At top level:
prog.c:101:7: error: conflicting types for 'divide2'
float divide2(float value)
   ^
prog.c:62:17: note: previous implicit declaration of 'divide2' was here
   Results = divide2(floatValue);
             ^

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

3 个答案:

答案 0 :(得分:2)

如果您在调用函数之后定义函数原型,则必须添加函数原型

示例:

ABC();

void ABC(){
//do some stuff
}

除非我们在调用ABC();

之前添加void ABC();,否则无效

这也可以正常工作:

void ABC(){
//do something
}

ABC();

答案 1 :(得分:1)

您应该在包含的下方使用原型:

int Square(int value);
int Cube(int value);
float Divide2(float value);

如果您不想使用原型。一个选项将所有方法都放在main函数中,这是不推荐的,但是它可以工作。

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

答案 2 :(得分:0)

这是可行的代码(我更改了一些变量名称,因为你不能对{和1}使用results我也改变了循环来检查数字是否大于 OR 等于0,因为0被认为是正数,而square / cube / half被定义为0)。我将在顶部定义一个函数;因为它们很小,所以它们很容易单行,通常对于单行,我将它们定义在顶部,但其余的我只是在顶部进行原型设计,然后定义(这样你就可以看到两种风格)。

#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;
}

我还将您的if / else系列更改为单个switch,以使代码更整洁,因此您可以看到{{1}的强大功能}。虽然我从未亲自使用它们,但这些功能似乎是switch的理想候选者。

您可能还想为浮动值指定inline的字段,因此它可能看起来像printf,例如0.50。而不是0.500000。以下是如何在小数点后指定两位数字:

printf("Here is a float value: %.2f\n", float_var);

只需更改&#34; 2&#34;如果您想指定精确度,请%.2f添加到您喜欢的任何内容中。