简体   繁体   English

我正在使用turbo C ++,我的程序没有产生输出

[英]I am using turbo C++ and my program is producing no output

The title pretty much says it all, so here's the code: 标题几乎说明了一切,所以这里是代码:

#include <stdio.h>

/* Program counts blanks, tabs, and newlines */
int main(void)
{
  int c;
  int b, t, nl;

  b = 0;
  t = 0;
  nl = 0;

  while ((c = getchar()) != EOF)
    if (c == ' ')
      ++b;
    if (c == '\t')
      ++t;
    if (c == '\n')
      ++nl;
  printf("Input has %d blanks, %d tabs, and %d newlines\n", b, t, nl);
  return 0;
}

I don't understand why this doesn't work. 我不明白为什么这不起作用。 It counts the blanks no problem, but then when it comes to the rest their values always get printed as 0. 它计算空白没有问题,但是当涉及到其余部分时,它们的值总是被打印为0。

More than a "here's how it should be" answer, I'd really like a "it doesn't work because... you need to do this because..." answer please. 不仅仅是“这里应该如何”的答案,我真的很喜欢“它不起作用,因为......你需要这样做,因为......”请回答。 I'm trying to grasp the concepts and actually understand the language more than just know what works and what doesn't. 我正在努力掌握这些概念并实际理解语言,而不仅仅是知道哪些有效,哪些无效。

Thanks! 谢谢! You guys have been already helped tons :). 你们已经帮助了吨:)。

It's because you're missing braces for your while loop. 这是因为你缺少while循环的大括号。 Without those braces, the only thing inside the while is the first if statement. 如果没有这些括号,里面的唯一的事情while是第一个if语句。 That's why it's only counting blanks, because you go through the whole file doing that one if statement then, after exiting the loop, c is EOF so neither of the other two if statements will be true. 这就是为什么它是只计算空白,因为你经历了整个文件做一个 if声明的话,退出循环后, cEOF所以其他两个既不if语句为真。

That was a tricky one, it's hard to see because the indentation looks like it should work but, unlike Python, the compiler doesn't use indentation to figure out where things are :-) 这是一个棘手的问题,很难看到因为缩进看起来应该可以工作但是,与Python不同,编译器不会使用缩进来弄清楚事情的位置:-)

If you put a opening brace after the while and a closing brace before the printf , it should count everything. 如果你在while之后放一个左大括号,在printf之前放一个右大括号,它应该算一切。

In other words, this is what your compiler sees: 换句话说,这是您的编译器所看到的:

while ((c = getchar()) != EOF) {
    if (c == ' ')
        ++b;
}
if (c == '\t')
    ++t;
if (c == '\n')
    ++nl;

whereas you want: 而你想要:

while ((c = getchar()) != EOF) {
    if (c == ' ')
        ++b;
    if (c == '\t')
        ++t;
    if (c == '\n')
        ++nl;
}

I see a lot of "always require braces" here. 我在这里看到很多“总是需要大括号”。 That's fine, and it will prevent this from happening, but I tend to prefer to always use a good editor. 这很好,它会阻止这种情况发生,但我倾向于总是使用一个好的编辑器。 Emacs, for example, will automatically indent C code to make this sort of thing very obvious. 例如,Emacs会自动缩进C代码,使这种事情变得非常明显。 Your 2nd and third if statements wouldn't indent as far as they are. 你的第二和第三个if语句不会缩进。

Another alternative, if you dislike braces, is to use else if where you use if . 如果您不喜欢大括号,另一种选择是使用else if您使用if Don't do that, but do understand it. 不要这样做,但要明白它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Turbo C IDE中查看程序的输出? - How can I see the output of my program in the Turbo C IDE? 使用Turbo C ++ 3.0编译器的C语言程序的输出令人困惑 - Perplexing Output of C language program using Turbo C++ 3.0 compiler 在 turbo c++ 上执行程序后出错(在 output 屏幕上) - Error after executing program at turbo c++(at output screen) 我在 Turbo C++ 中使用 struct — 它给出的错误声明是不允许的 - I am using struct in Turbo C++ — it is giving error declaration is not Allowed Here 我正在使用 C 中的双向链接列表,并且我正在使用 Turbo C++ 但编译器正在使用两个额外的节点而不添加 - I am Working with Doubly Linked Lists in C and I am Using Turbo C++ but The Compiler is Taking Two Additional Nodes without Adding 为什么我的程序没有显示任何 output? 0 个错误,0 个警告但没有 output? 我正在使用 Dev C++ 编译器 - Why is my program not showing any output? 0 errors, 0 warnings but no output? I'm using Dev C++ compiler 使用Turbo C ++,我如何在C中绘制图形? - Using Turbo C++, how I can draw graphics in C? 我是c语言的新手,我的程序正在运行,但没有任何输出 - I am new with c my program is running but nothing as an output 我没有为我的 C 程序获得确切的 output - i am not getting the exact output for my C program 我没有得到C程序的期望输出 - I am not getting desired output for a C program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM