C++ 循环终止时无法获得输入

C++ 循环终止时无法获得输入,c++,c,c++11,C++,C,C++11,大家好,哈姆正在数单词和字符的数量等。。但是,对于一个项目,在我输入^D以终止输入的行中,该行不计入单词字符等 输入终止的任何行都不计算。此外,出于某种原因,当我运行程序时,它不会打印底部的语句,但当我调试它时它会打印 如果您能在这方面给予帮助,我将不胜感激 #include <stdio.h> int main() { int input; int words =0; int charecters =0; int spaces =0; in

大家好,哈姆正在数单词和字符的数量等。。但是,对于一个项目,在我输入^D以终止输入的行中,该行不计入单词字符等

输入终止的任何行都不计算。此外,出于某种原因,当我运行程序时,它不会打印底部的语句,但当我调试它时它会打印

如果您能在这方面给予帮助,我将不胜感激

#include <stdio.h>

int main()
{
    int input;
    int words =0;
    int charecters =0;
    int spaces =0;
    int newline =0;
    int tab= 0;
    int total =0;
    int dummy;
    int lastChar;
    printf("Please start your input and press ^D to terminate:\n");
    while ((input=getchar())!= EOF)
    {
        if(input == ' ')
        {
            ++spaces;
        }
        else if(input =='\n')
        {
            ++newline;
            fflush(stdin);
        }
        else if(input == '\t' )
        {
            ++tab;
        }
        else
        {
            charecters++;
            dummy = input;
        }


        lastChar = input;
        if(dummy != lastChar)
        {
            words++;
        }

        total++;
    }
    printf("The total number of keyboard strokes was %d.\n", total);
    printf("The total number of charecters is %d\n", charecters);
    printf("The number of new lines is: %d\n", newline);
    printf("The number of  space is: %d\n", spaces);
    printf("The number of  tabs is: %d\n", tab);
    printf("The number of new words is: %d\n", words);

    return 0;

}
#包括
int main()
{
int输入;
int字=0;
int charecters=0;
int空间=0;
int newline=0;
int tab=0;
int-total=0;
int假人;
int-lastChar;
printf(“请开始输入并按^D终止:\n”);
而((input=getchar())!=EOF)
{
如果(输入=“”)
{
++空间;
}
else if(输入=='\n')
{
++新线;
fflush(stdin);
}
else if(输入=='\t')
{
++标签;
}
其他的
{
charecters++;
虚拟=输入;
}
lastChar=输入;
if(dummy!=lastChar)
{
words++;
}
总计++;
}
printf(“键盘笔划总数为%d.\n”,总计);
printf(“字符总数为%d\n”,字符数);
printf(“新行数为:%d\n”,新行);
printf(“空间数为:%d\n”,空格);
printf(“选项卡的数量为:%d\n”,选项卡);
printf(“新词的数量为:%d\n”,单词);
返回0;
}

以下建议的代码:

#include <stdio.h>

int main( void )
{
    int input;
    int words      = 0;
    int charecters = 0;
    int spaces     = 0;
    int newline    = 0;
    int tab        = 0;
    int total      = 0;

    printf( "Please start your input and press ^D to terminate:\n" );

    enum { inWord, notInWord } state = notInWord;
    while ( (input=getchar())!= EOF )
    {
        ++total;

        if( input == ' ' || input == '\n' || input == '\t' )
        {
            state = notInWord;
        }

        switch( input )
        {
            case ' ':
                ++spaces;
                break;

            case '\n':
                ++newline;
                break;

            case '\t':
                ++tab;
                break;

            default:
                charecters++;
                if  (state == notInWord )
                {
                    state = inWord;
                    ++words;
                }
        }
    }

    printf( "The total number of keyboard strokes was %d.\n", total );
    printf( "The total number of charecters is %d\n", charecters );
    printf( "The number of new lines is: %d\n", newline );
    printf( "The number of  space is: %d\n", spaces );
    printf( "The number of  tabs is: %d\n", tab );
    printf( "The number of new words is: %d\n", words );

    return 0;
}
  • 执行所需的功能
  • 干净地编译
  • 实现具有2个状态的状态机
  • 在LINUX上,将
    识别为输入的结尾,它必须是新行的第一个字符
  • 包含一个
    开关
    语句,以使每个“活动”清晰可见
  • 使用适当的水平和垂直间距,以便于阅读和理解
  • 不更正
    字符的拼写
  • 现在,拟议的守则:

    #include <stdio.h>
    
    int main( void )
    {
        int input;
        int words      = 0;
        int charecters = 0;
        int spaces     = 0;
        int newline    = 0;
        int tab        = 0;
        int total      = 0;
    
        printf( "Please start your input and press ^D to terminate:\n" );
    
        enum { inWord, notInWord } state = notInWord;
        while ( (input=getchar())!= EOF )
        {
            ++total;
    
            if( input == ' ' || input == '\n' || input == '\t' )
            {
                state = notInWord;
            }
    
            switch( input )
            {
                case ' ':
                    ++spaces;
                    break;
    
                case '\n':
                    ++newline;
                    break;
    
                case '\t':
                    ++tab;
                    break;
    
                default:
                    charecters++;
                    if  (state == notInWord )
                    {
                        state = inWord;
                        ++words;
                    }
            }
        }
    
        printf( "The total number of keyboard strokes was %d.\n", total );
        printf( "The total number of charecters is %d\n", charecters );
        printf( "The number of new lines is: %d\n", newline );
        printf( "The number of  space is: %d\n", spaces );
        printf( "The number of  tabs is: %d\n", tab );
        printf( "The number of new words is: %d\n", words );
    
        return 0;
    }
    
    请注意,程序不会将标点符号识别为单词分隔符。建议包括头文件:
    ctype.h
    ,以便通过使用以下功能使代码更加健壮:

    ispunc()
    isdigit()
    isspace()
    

    你好欢迎来到StackOverflow!您是否坚持使用自己的功能,或者愿意使用一些使问题更容易解决的C++函数吗?另外,我在您的codeCtrl-D中没有看到任何
    str
    变量不是真的EOF。它是一个字符序列,终端将其解释为输入结束,EOF是文件句柄上的一个条件,表示没有更多的字节可读取。在LInux shell中,我认为必须在新行中输入Ctrl-D才能达到预期效果。请调整图示代码。@RobertSsupportsMonicaCellio我已调整此代码。为之前的错误道歉。@DanielFarrell我正在CLion IDE上使用macOS Catalina。每当我按enter键并按ctrl-D键时,都没有问题。