#include <iostream>
#include <string>

using namespace std;

int main()
{


    //function for taking in a line
    string line ;
    auto line_size = line.size();

    while (getline(cin, line))
        if (!line.empty())
        {
            cout << line_size << endl;
            cout << line << endl;
        }


    system("pause");
    return 0;
}


当我输入某些内容时,为什么line_size不能打印正确的尺寸?

最佳答案

在获取任何输入之前,您正在捕获行的大小。这意味着line_size始终为0,因为它是空字符串的大小。无需捕获大小,只需将循环更改为

cout << line.size() << endl; // always gets the size of the current line
cout << line << endl;

09-10 04:03
查看更多