计算字符计数器卡住后?

时间:2017-02-18 23:00:06

标签: c++ pointers char const increment

大家好我有以下代码,我不明白为什么在功能" 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 个答案:

答案 0 :(得分:2)

在函数strcount

while(*str++)

递增str,直到到达字符串的末尾。

然后尝试输出

cout << str << endl;

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