C++ 统计用户输入的总行数和字符长度

#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

int main(void) {
    string line;
    int count = 0;
    int length = 0;

    cout << "请输入任意多行:" << endl;

    while (1) {
        // 如果遇到文件结束符, (cin的文件结束符是Ctrl+z), getline返回0
        if (getline(cin, line)) {
            count++;
            length += line.length();
        }
        else {
            break;
        }
    }

    cout << "你一共输入了" << count << "" << endl;
    cout << "输入的字符长度:" << length << endl;

    system("pause");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tanghaiyong/p/11331096.html