【问题标题】:calculating volume of cube [closed]计算立方体的体积
【发布时间】:2013-08-09 08:47:36
【问题描述】:
 printf("The Volume of the cuboid with a height of%d", height);
 printf("and width of%d\n", width);
 printf("and depth of%d\n", depth);
 printf("Gives you the Volume of %lf\n", vcuboid);

"volumeofcuboid.c:5: 错误:此处未声明“height”(不在函数中)volumeofcuboid.c:5:错误:此处未声明“width”(不在函数中)volumeofcuboid.c:5:错误:此处未声明“深度”(不在函数中)"

【问题讨论】:

  • 你应该编译它......
  • #!/bin/bash 放在开头可能无济于事。
  • 天哪,这是大错特错。 vcuboid=((height*width*depth)); 不。
  • @KevinDTimm 我认为这会使编译器崩溃,编译器无法修复愚蠢的问题。
  • 当您发布问题而不了解基础知识时,人们往往会有点刻薄,但我仍然记得当您是初学者时的感觉。去掉 shebang 行 (#!/bin/bash) - 这是 shell 脚本语法,而不是 C。您需要使用 gcc 将其编译成可执行文件,然后您可以运行它。您所拥有的称为 C 源代码 - 您不能直接运行它。您还将vcuboid 声明为一个函数,它应该是double(但您可以改用int)。

标签: c linux


【解决方案1】:

对于初学者:

  1. #!/bin/bash

C 不是 shell 脚本。即使您使用的是 cshell,也不会。

  1. vcuboid (height,width,depth);

不是一个有效的函数原型,而且你从来没有提供一个实际的函数。

  1. main ()

main() 的定义无效,应为 int main(void)

  1. vcuboid=((height*width*depth));

vcuboid 尚未定义。

只是,不。

  1. exit(0);

return 0;

【讨论】:

    【解决方案2】:

    查看 Paul Griffiths 的答案以更正您的代码。 如果它不起作用,请使用此代码更正您的代码。

    #include <stdio.h>
    #include <stdlib.h>
    
    int get_volume(int h,int w,int d);
    
    int main(void) {
      int height = 0, width = 0, depth = 0, volume;
      printf("Height : ");
      scanf("%d", &height);
      printf("Width : ");
      scanf("%d", &width);
      printf("Depth : ");
      scanf("%d", &depth);
    
      volume = get_volume(height,width,depth);
    
      printf("Volume : %d * %d * %d = %d\n", height, width, depth, volume);
      return 0;
    }
    
    
    int get_volume(int h,int w,int d) {
      return h * w * d;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多