【问题标题】:unexpected ./ output when coding in C用 C 编码时出现意外的 ./ 输出
【发布时间】:2017-04-26 03:09:37
【问题描述】:

我刚刚开始学习使用 edX 编写 C 语言,我确信这是一个非常简单的错误,我无法发现。

我得到了一个额外的 ./ ,我不知道它是从哪里来的。

这是我得到的输出

How many minutes does your shower last: 3
minutes: 3
bottles: 36./

我在 Cloud 9 上使用 CS50 IDE,我的代码在屏幕截图中

感谢您的帮助。

这是我的代码:

 int main(void)
 {
int showerlength, bottle;

    printf("How many minutes does your shower last: ");
    scanf("%d", &showerlength);
    
    bottle = (showerlength * 12);
    
    printf("minutes: %d\n",showerlength );
    printf("bottles: %d\n",bottle );
    
}

这是我得到的输出

~/workspace/pset1/ $ ./water
How many minutes does your shower last: 3

minutes: 3
bottles: 36./

【问题讨论】:

  • 是的,这没有任何意义。也许您正在使用的 Web IDE 正在打印一些额外的东西?
  • 我在使用 Clang 时没有收到此错误。
  • 虽然不太可能,但可能是缺少回报。尝试在函数末尾添加行 return 0;(在最后一个 printf 之后和右大括号之前)。
  • 尝试将showerlength 初始化为0,然后再将其传递给scanf。它可能有一些导致这种行为的垃圾。除此之外,它可能只是您正在使用的 IDE 的产物?
  • 您好,您检查了scanf() 的返回标志吗?如果为showerlength 输入了一个合适的值,它应该等于1。为了确定额外的./ 来自何处,您能否在%d\n 之间添加一些文本,例如printf("bottles: %d <my extra text>\n",bottle );

标签: c cs50 c9.io


【解决方案1】:

这将不再是一个问题。但是,请在 ide.cs50.io 的新 CS50 IDE 中尝试相同的操作。

【讨论】:

    【解决方案2】:

    这看起来不再是问题了。

    我在 CS50 IDE 中测试了完全相同的代码,并且输出很好。

    我什至在主函数中使用和不使用return(0) 对其进行了测试。它工作正常。

    Screenshot

    【讨论】:

      猜你喜欢
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 2021-12-27
      • 1970-01-01
      • 2021-10-13
      • 2020-02-21
      相关资源
      最近更新 更多