【问题标题】:Nothing showing up C beginner没有出现 C 初学者
【发布时间】:2013-07-24 08:35:13
【问题描述】:

我正在跟随一本关于 C 的书来学习它,并且我正在编写书中的所有代码以继续学习,最新的与数组有关的代码应该说明有多少空格、制表符等。那里是,但是当我执行它时,什么都没有显示,它只是空白,因为我可以输入一些东西然后按 Enter 并且没有任何反应,它应该告诉我每件事有多少吗?

我太新了,不明白这个程序是否真的应该输出任何东西,所以我想我会把它贴在这里并得到一个意见,它编译和运行都很好,没有错误,但是这本书有点指它输出东西,但是当我运行它并输入东西时没有任何反应,可以永远输入东西。

这里是代码

 #include <stdio.h>

 int main()
 {
    int c, i, nwhite, nother;
    int ndigit[10];
    nwhite = nother = 0;
    for (i = 0; i < 10; ++i)
    ndigit[i] = 0;
    while ((c = getchar()) != EOF)
    if (c >= '0' && c <= '9')
      ++ndigit[c-'0'];
    else if (c == ' ' || c == '\n' || c == '\t')
      ++nwhite;
    else
    ++nother;
    printf("digits =");
    for (i = 0; i < 10; ++i)
    printf(" %d", ndigit[1]);
    printf(", white space = %d, other = %d\n", nwhite, nother);
 }

【问题讨论】:

  • 你输入过EOF吗?
  • 这看起来很可疑:printf(" %d", ndigit[1]);...你不是要打印digit[i]吗?
  • 如果您使用的是 linux,请使用 CTRL + D 提供 EOF。
  • 是的,应该是“i”的 Joachim Pileborg 并欢呼 Jeyaram 成功了,为什么在 linux 上我们需要使用 CTRL + D?谢谢大家!
  • 你的书有没有教你对main没有返回类型?一本好的现代书籍会向您展示 int main(void) 以了解 main 的规范。

标签: c arrays output


【解决方案1】:

应用程序接受输入一些值,然后计算位数 (0-9) 和空格。中断循环的组合键不是 ENTER 而是 EOF,在 Linux 中是 CRTL-D 而在 WINDOWS 中是 CTRL-Z

然后,在您的应用程序中有一个错误:

  for (i = 0; i < 10; ++i)
        printf(" %d", ndigit[1]);

为了显示位数,这应该是:

for (i = 0; i < 10; ++i)
    printf(" %d", ndigit[i]);

不幸的是,在使用 scanf()、getchar()、fgets() 等时,获取交互式输入是相当有问题的。这就是为什么大多数人通常编写自己的自定义函数,通常从 stdin 获取整行,然后根据到他们的需要。但是,如果您想使用 ENTER 停止循环,您可以修改代码如下,但您将失去计算输入中新行数的可能性。

#include <stdio.h>

int main(void)
{
  int c, i, nwhite, nother;
  int ndigit[10];

  nwhite = nother = 0;
  for (i = 0; i < 10; ++i)
    ndigit[i] = 0;

  while ((c = getchar()) != '\n')
    if (c >= '0' && c <= '9')
      ++ndigit[c-'0'];
    else if (c == ' ' || c == '\n' || c == '\t')
      ++nwhite;
    else
      ++nother;
  printf("digits =");
  for (i = 0; i < 10; ++i)
    printf(" %d", ndigit[i]);
  printf(", white space = %d, other = %d\n", nwhite, nother); 

return 0; 

}

这应该可以按您的预期工作。但是,你应该考虑写一个更好的输入函数,网上有几个有趣的解决方案。

编辑

main() 必须返回 int。不是 void,不是 bool,不是 float。诠释。只是int,只有int,只有int。一些编译器接受 void main(),但这是非标准的,不应使用。

在这里查看一些示例:http://www.parashift.com/c++-faq-lite/main-returns-int.html

【讨论】:

  • 谢谢大家,不知道 CTRL + D 的事情,也感谢指出错误,之前没有注意到!不,我正在阅读的书是一本旧书,并没有说要在 main 函数中添加 void,所以也许我应该改变并阅读一本更现代的书?谢谢大家!!
  • 我有一个 Brian Kernighan 写的,但它还没有提到将“void”放在 main 函数中。
  • 对不起,你可以把 void 放在 main 里。但是放置 void 并不是一个好习惯,因为 main 应该总是返回一个值。如果程序已正确执行,则正确的返回值为零或错误。我修改了代码以添加此更改。
【解决方案2】:

您可以使用以下方法提供 EOF

Windows:

Press F6 then ENTER
or 

Ctrl+Z

Linux:

Ctrl+D

【讨论】:

  • 同时修复你的代码for (i = 0; i &lt; 10; ++i) printf(" %d", ndigit[i]); //instead of ndigit[1]
【解决方案3】:

改变

printf(" %d", ndigit[1]);

printf(" %d", ndigit[i]);

输入值后按ctrl+d给出EOF

输入:

2 4 6 8     //3 white space + 1 new line = 4 white space
[ctrl + d]

输出:

digits = 0 0 1 0 1 0 1 0 1 0, white space = 4, other =0 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    相关资源
    最近更新 更多