计数字符计数器卡住后?

问题描述:

嗨,大家好我有以下代码,我不明白为什么在函数“strcount”最后一行不再显示整个字符串? 在此先感谢!计数字符计数器卡住后?

#include <iostream> 
const int ArSize = 10; 
void strcount(const char * str); 

int main() 
{ 
    using namespace std; 
    char input[ArSize]; 
    char next; 

    cout << "Enter a line:\n"; 
    cin.get(input, ArSize); 
    while(cin) 
    { 
     cin.get(next); 
     while(next != '\n') 
      cin.get(next); 
     strcount(input); 
     cout << "Enter next line (empty line to quit):\n"; 
     cin.get(input, ArSize); 
    } 
    void strcount(const char * str) 
    { 
     using namespace std; 
     static int total = 0; 
     int count = 0; 

     cout << "\"" << str <<"\" contains "; 
     while(*str++) 
      count++; 
     total += count; 
     cout << count << " characters\n"; 
     cout << total << " characters total\n" << endl; 
     cout << str << endl; 
    } 
+1

解决此类问题的正确工具是您的调试器。在*堆栈溢出问题之前,您应该逐行执行您的代码。如需更多帮助,请阅读[如何调试小程序(由Eric Lippert撰写)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您应该\编辑您的问题,以包含一个[最小,完整和可验证](http://*.com/help/mcve)示例,该示例再现了您的问题,以及您在调试器。 –

+0

你为什么期望'cout

+0

我推荐使用'std :: string'和'std :: getline'来读取用户的文本。比担心数组溢出或丢失nul终止字符容易得多。 –

在功能strcount

while(*str++) 

增量str,直到你到达字符串的结尾。

然后试图输出

cout << str << endl; 

将显示任何东西,因为现在str指向字符串的结尾。

+0

谢谢大家的帮助,现在我明白了。 – AndyE