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

标签 c++ c c++11

大家好,我想计算项目的单词和字符等的数量。但是,在我输入^ 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;

}

最佳答案

以下建议的代码:

  • 执行所需的功能
  • 干净地编译
  • 实现具有2个状态的状态机
  • LINUX上的
  • <ctrl-d>识别为输入的末尾,它必须是新行
  • 上的第一个字符
  • 合并了switch语句,以使每个“ Activity ”清晰可见
  • 使用适当的水平和垂直间距,以便于阅读和理解
  • 无法更正characters的拼写

  • 现在,建议的代码为:
    #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;
    }
    

    该程序的典型运行:
    Please start your input and press ^D to terminate:
    lkfjaiennf;aliea vliesge lsker'
    flkaiwore
    The total number of keyboard strokes was 42.
    The total number of charecters is 38
    The number of new lines is: 2
    The number of  space is: 2
    The number of  tabs is: 0
    The number of new words is: 4
    

    请注意,程序无法将标点符号识别为单词分隔符。建议包括头文件ctype.h,以使用以下功能使代码更健壮:
    ispunc()
    isdigit()
    isspace()
    

    关于c++ - 循环终止时无法获得输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61560855/

    相关文章:

    c - 函数内的数组声明

    c - 未从 strtok_s() 获取标记化数组

    c++ - std::async 和 std::future 行为

    c++ - float 的全局格式化选项

    c++ - 包装 std::thread 调用函数

    由 C++ libcurl json rest

    c++ - 堆在 C++ 中的算法重复?

    c++ - 如何将应用程序的基本优先级动态设置为 31?

    c - execl 和 execv 有什么区别?

    c++ - 在 Visual Studio2010 中通过 C++ 优化(由编译器完成)的无限循环示例