首页 > 编程语言 > 详细

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

时间:2019-08-10 13:50:52      阅读:102      评论:0      收藏:0      [点我收藏+]

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;
}

 

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

原文:https://www.cnblogs.com/tanghaiyong/p/11331096.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!