简体   繁体   English

如何在C ++中使用空格输入长字符串?

[英]How to take long string input with spaces in c++?

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int T;
    cin >> T;
    char input[1000];
    for(int i=0;i<T;i++)
    {
        cin.getline(input,sizeof(input));
        cout << input << "\n";
    }
    return 0;
}

I am currently having problem getting string input using getline but it seems to work only for short lines. 我目前在使用getline获取字符串输入时遇到问题,但它似乎仅适用于短行。 Here is my input and output: 这是我的输入和输出:

Input: 输入:

3 Can I have a large container of coffee right now Can I have a large container of tea right now Now I wish I could recollect pi Eureka cried the great inventor Christmas Pudding Christmas Pie Is the problems very center 3我现在可以盛一大杯咖啡吗现在我可以盛一大杯茶现在我希望我可以回忆起来尤里卡哭喊着伟大的发明家圣诞布丁圣诞馅饼问题是不是很严重

Output: 输出:

Can I have a large container of coffee right now Can I have a large container of tea right now 我现在可以喝一大杯咖啡吗现在我可以喝一大杯茶吗

It doesn't store last line why? 为什么不存储最后一行?

You can do it with this way, using string instead of char [] : 你可以用这种方式做到这一点,使用string代替char []

int main() {
    int T;
    cin >> T;
    string input;
    for(int i=0;i<T;i++)
    {
        std::getline(std::cin,input);
        cout << input << endl;
    }
    return 0;
}

Check the code with the following test : 请与下面的测试代码:

Input : 输入:

3 Can I have a large container of coffee right now Can I have a large container of tea right now Now I wish I could recollect pi Eureka cried the great inventor Christmas Pudding Christmas Pie Is the problems very center

Output: 输出:

3 Can I have a large container of coffee right now Can I have a large container of tea right now Now I wish I could recollect pi Eureka cried the great inventor Christmas Pudding Christmas Pie Is the problems very center

Your problem is the first input operator: 您的问题是第一个输入运算符:

cin >> T;

This reads a number and stores it as an integer in T, but leaves the newline in the stream. 这将读取一个数字并将其存储为T中的整数,但是将换行符保留在流中。 Now the loop will read three lines, but the first will be an empty line. 现在,循环将读取三行,但第一行将为空行。

You can fix this in several ways, the simplest is to just discard the newline after fetching the number: 您可以通过多种方式解决此问题,最简单的方法是在获取数字后就丢弃换行符:

cin >> T;
cin.getline(input, sizeof input);

A better way will be to check for end of file instead of fetching the number of lines in advance. 更好的方法是检查文件的末尾,而不是预先获取行数。 And use std::string instead of char arrays as others have suggested. 并使用std::string而不是其他人建议的char数组。

Try something like this 试试这个

while(cin.getline(input,sizeof(input))){
}

Try: 尝试:

int main() {
    int T;
    cin >> T;
    std::string temp;
    std::string input;
    for(int i=0;i<T;i++)
    {
        while(std::getline(std::cin,temp)) {
        input += temp;
    }
    std::cout << input << endl;
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM