如何创建输入的每个单词以开始新的行

问题描述 投票:-1回答:1

嘿,我正在尝试在新行上打印每个单词。我的EOF也没有用,并且想知道为什么会这样。我已经扫描了一个空格,然后打印了新的一行。

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    char ch;

    while ((ch = getchar()) != '#')
        putchar(ch);

int nextChar;
    nextChar = getchar();
    while (nextChar != '\n' && nextChar != EOF);
    {
        if (ch== ' ')
        {
            printf("\n");
        }
        else
        {
            putchar(ch);
        }
        {
            ch = getchar();
        }
        printf("\n");

        {
            scanf("%lc",&nextChar);
            printf("%c",nextChar);
        }

        return 0;
    }
}

just for example input: Stackoverflow is great
output:
Stackoverflow
is
great
c eof getchar putchar
1个回答
1
投票

您应该真正开始启用编译器警告。他们可以帮助您找到许多错误。当我用-Wall-Wextra编译时,请看这里。

$ gcc ba.c -Wall -Wextra
ba.c: In function ‘main’:
ba.c:13:5: warning: this ‘while’ clause does not guard... [-Wmisleading-indentation]
     while (nextChar != '\n' && nextChar != EOF);
     ^~~~~
ba.c:14:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘while’
     {
     ^

while循环后删除;

但是还有其他问题。正如您在我为您更正缩进时所看到的那样,return 0语句位于while循环中。我认为这不是你想要的。

© www.soinside.com 2019 - 2024. All rights reserved.