如何使用 getchar 来运行程序?
我是一个彻头彻尾的 C n00b,试图自学 C 关闭 K&R。我的问题有点简单得令人尴尬。好的,这里是:我无法使用 getchar 获得程序来给出我期望的输出类型。如果你手头恰好有 K&R,我就会陷入练习 1.13。问题是,“编写一个程序来打印输入中单词长度的直方图。”由于我遇到的这个问题,我什至无法解决水平版本。
我在 XP 上使用 Dev-C++(mingW 编译器)并从命令行运行程序。我的问题是,当我尝试运行程序时,它会等待我输入要扫描的字符,但是当我完成输入并按 Enter 键时,它不会执行任何操作。我希望它能够按照我的预期继续打印直方图。实际上,它甚至似乎没有计算字长,因为正如您在代码中看到的那样,当我尝试打印 ctr 数组中的内容只是为了查看它是否包含任何内容时,什么也没有打印。
我实在是太笨了,我不知道是我的代码有问题还是命令行有问题。但我怀疑这是系统的问题,因为当我尝试编译并运行 一个模型程序,同样的事情也会发生。键入输入,按 Enter 键,什么也没有发生。如果我按 Ctrl-C,有时它会输出一两个星号,看起来与模型输出完全不同。其他时候,它不执行任何操作(只是直接返回到提示)。
这是我的练习代码。我花了一整天的时间来处理这个问题,并质疑我继续编程的能力。如果有人能让我摆脱这个困境,我真的非常非常感激!
另外,我对上面提到的模型程序还有另一个问题,但我认为我应该将其发布在它自己的问题中。谢谢大家:)
#include <stdio.h>
//#define 1 IN
//#define 0 OUT
int main () {
//start w/ state = OUT
int c = 0;
// int state = OUT;
int len = 0;
int ctr[12];
int i, j;
i = j = 0;
for (i = 0; i <12; i++)
ctr[i] = 0;
while ((c = getchar()) != EOF)
if (c != ' ' && c != '\t' && c != '\n') {
// state = IN;
len++;
printf("%d", len);
}
else {
ctr[len]++;
len = 0;
}
for (i = 0; i <12; i++)
printf("%d\n", ctr[i]);
for (i = 0; i <12; i++) {
printf("%d\n", i);
for (j = 0; j <= ctr[i]; j++)
printf("-");
printf("\n");
}
return 0;
}
I'm a total C n00b trying to teach myself C off K&R. My question is kind of embarrassingly elementary. OK, here goes: I can't get programs using getchar to give the kind of output I expected. If you happen to have K&R on hand, I'm stuck on exercise 1.13. The question goes, "Write a program to print a histogram of the lengths of words in its input. " and I can't even tackle the horizontal version because of this issue I'm having.
I'm on XP using Dev-C++ (mingW compiler) and running programs off the command line. My issue is, when I try to run my program, it waits for me to enter characters to scan from, but when I'm done inputting and hit Enter, it doesn't do anything. I expect it to go ahead and print the histogram as I expected. In reality, it doesn't even seem to count up word lengths, because as you can see in the code, when I try to print what's in the ctr array just to see if it contains anything, nothing prints.
I'm so n00b that I have no idea if it's my code or the command line that's at fault. But I suspect it's something with the system, because when I try to compile and run a model program, the same thing happens. Type in input, hit Enter, nothing happens. If I Ctrl-C, sometimes it spits out an asterisk or two that looks nothing like the model output. Other times, it doesn't do anything (just goes right back to the prompt).
Here's my code for the exercise. I've spent an entire day on this and am questioning my ability to carry on with programming. I'd really, really appreciate it if anyone could get me out of this hole!
Also, I have another question about the model program I mentioned above, but I think I should post it in its own question. Thanks all :)
#include <stdio.h>
//#define 1 IN
//#define 0 OUT
int main () {
//start w/ state = OUT
int c = 0;
// int state = OUT;
int len = 0;
int ctr[12];
int i, j;
i = j = 0;
for (i = 0; i <12; i++)
ctr[i] = 0;
while ((c = getchar()) != EOF)
if (c != ' ' && c != '\t' && c != '\n') {
// state = IN;
len++;
printf("%d", len);
}
else {
ctr[len]++;
len = 0;
}
for (i = 0; i <12; i++)
printf("%d\n", ctr[i]);
for (i = 0; i <12; i++) {
printf("%d\n", i);
for (j = 0; j <= ctr[i]; j++)
printf("-");
printf("\n");
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的 while 循环正在寻找 EOF,它代表文件结束,而不是行结束。
在 Windows 上,您需要键入 ctrl-z 来模拟文件结束。
Your while loop is looking for EOF which stands for end-of-file, not end-of-line.
On Windows, you need to type ctrl-z to simulate end-of-file.
看起来您实际上并没有将 c (您的输入)存储在任何地方......也没有打印它。您正在打印字符串的大小,而不是实际的字符。如果你 ctr[i] = c;某处(即将字符添加到数组中),然后打印数组,您将看到您的输入。哦,是的,关于 ctrl-z 的答案也很重要。
另外,如果您对这门语言完全陌生,我强烈建议您将 while 内容放在括号中。需要一段时间,您才能浏览一下代码并知道哪些内容属于 while 循环的范围,而如果没有大括号括起来,哪些内容则不属于 while 循环的范围。
It doesn't look like you're actually storing c (your input) anywhere... nor printing it. You're printing the size of the string, but not the actual characters. If you ctr[i] = c; somewhere (that's you adding the character to the array), and then print the array, you'll see your input. Oh and yes, the answer about ctrl-z is also important.
Also, if you're totally new to the language, I would strongly urge you to put brackets around your while content. It's going to be a while before you can just glance at the code and know what will fall into the purview of the while loop and what will not if you don't have braces around it.
我没有发现代码有什么真正的错误,所以我将它加载到 gcc 下,它似乎工作正常,只要你记住必须输入 EOF (CTRL-D) 来终止 while 循环。我输入了 4 行,虽然我无法对答案的正确性做出任何陈述,但每次按 Enter 键时,我都会得到一系列等于我输入的字符数的数字,后跟一个空格。这正是您的代码所说的要做的事情。
当我输入CTRL-D时,我得到了摘要信息。再说一次,我不会对输出的正确性做出任何声明,但我确实得到了代码中描述的两个主要部分。
I didn't see anything really wrong with the code, so I loaded it up under gcc and it seems to work fine, as long as you remember that you have to put in an EOF (CTRL-D) to terminate the while loop. I entered 4 lines and while I can't make any statements about the correctness of the answers, each time I hit enter I got a series of numbers equal to the number of characters I entered, followed by a space. This is exactly what your code says to do.
When I entered the CTRL-D, I got the summary information. Again, I'm not going to make any statements about the correctness of the output, but I did get two major sections as described in your code.