C 语言中的 While 循环帮助

发布于 2024-08-05 09:54:05 字数 538 浏览 2 评论 0原文

我是 C 编程新手,我有 Java 背景。我想知道为什么在下面的代码中,在 while 循环中我必须输入输入十次,然后显示所有十个输入。我正在尝试输入一些内容,然后立即显示它。然后继续输入我的其他输入。

#include <stdio.h>
#include <stdlib.h>
#include "Helper.h"


main(){

print(PROGRAM_INFO); //prints program name and author
print(PROMPT);

char input [100]; //array to hold input from user
int isActive = 1; //1 continue shell, 0 terminate shell
int count = 0;

while (isActive == 1 && count < 10){
    print(PROMPT);
    ++count;
    scanf("%s", input);
    print(input);


}


}

I'm new to C programming, I come from a Java background. I was wondering why in the following code, in the while loop I have to type my input ten times and then all ten inputs are displayed. I'm trying to type something once and have it displayed right after. Then continue typing my other inputs.

#include <stdio.h>
#include <stdlib.h>
#include "Helper.h"


main(){

print(PROGRAM_INFO); //prints program name and author
print(PROMPT);

char input [100]; //array to hold input from user
int isActive = 1; //1 continue shell, 0 terminate shell
int count = 0;

while (isActive == 1 && count < 10){
    print(PROMPT);
    ++count;
    scanf("%s", input);
    print(input);


}


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜唯美灬不弃 2024-08-12 09:54:06

您的 print 是复制/粘贴错误,对吗?它应该是printf

而且你真的不应该直接以 printf 的格式打印用户字符串。
想象一下用户输入 "%d%f%s\a%c"

...使用

printf("%s\n", input);

格式中的 '\n' 时,您不需要 fflush(stdout); 因为 stdout 默认情况下是行缓冲的,而 '\n' 会自行执行一个操作。

此外,如果用户输入 "%d%f%s\a%c" 这就是您打印的内容。


最好的办法是

puts(input);

将 '\n' 添加到输出中,并且格式字符串没有问题。

Your print is a copy/paste error, right? It should be printf.

And you really shouldn't print the user string directly in the format of printf.
Imagine the user types "%d%f%s\a%c" ...

The best thing to do is

printf("%s\n", input);

With the '\n' in the format, you don't need to fflush(stdout); because stdout is line buffered by default and the '\n' does one on its own.

Besides, if the user types "%d%f%s\a%c" that's what you get printed.


The best thing to do is

puts(input);

puts adds the '\n' to the output and has no problems with format strings.

无法言说的痛 2024-08-12 09:54:05

尝试在每次 print(input) 后刷新 fflush(stdout)

Try flushing fflush(stdout) after each print(input)

凉薄对峙 2024-08-12 09:54:05

尝试在打印中添加“\n”字符。流只是没有被刷新。

Try putting a '\n' character in the print. The stream just isn't being flushed.

素手挽清风 2024-08-12 09:54:05

在最终打印语句之后,您可能可以调用 fflush(stdout);将标准输出刷新到屏幕。

after your final print statement you could probably call fflush(stdout); to flush stdout to the screen.

笛声青案梦长安 2024-08-12 09:54:05

您需要刷新 STDOUT。 fflush(STDOUT) 应该做到这一点,在打印之后添加。

You need to flush STDOUT. fflush(STDOUT) should do it, added after the print.

泡沫很甜 2024-08-12 09:54:05

默认情况下,输入/输出是缓冲的,即输入和输出字节在显示到流之前存储在字节数组中。
系统中的BUFSIZ一般是1024的倍数。虽然printf是行缓冲的,但是缓冲
当遇到换行符时会自动刷新。
fflush(stdout) 导致缓冲数据刷新到输出流,在本例中是标准输出。
您可以使用 setvbuf() 函数控制缓冲区处理

by default input/output is buffered, i.e the input and output bytes are stored in a byte array before being displayed to the stream.
BUFSIZ in systems is generally a multiple of 1024.Although printf is line buffered, the buffer
is flushed automatically when a newline is encountered.
fflush(stdout) causes the buffered data to be flushed to output stream which in this case is stdout.
you can control buffer handling using setvbuf() function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文