繁体   English   中英

getline 后跟 integer 输入 c++

[英]getline followed by integer input in c++

我想在 c++ 中输入一个包含空格的字符串,后跟一个 integer 输入。 我已经阅读了关于这个问题的类似帖子,但我找不到任何有用的。 这是我尝试过的。

#include<iostream>
#include<string>

using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--){
//this part here is my doubt... i want to accept integer after string input
        string s;
        int c;
        getline(cin,s);
        
        cin>>c;
        cin.ignore();
        cout<<s<<endl;
    }
    return 0;
}

它不会在 output 中打印任何内容。 即使我在输入 integer 之前放置 cin.ignore() ,它仍然不会在 output 中打印任何内容。

我想在 c++ 中输入一个包含空格的字符串,后跟一个 integer 输入。 我已经阅读了关于这个问题的类似帖子,但我找不到任何有用的。 这是我尝试过的。

#include<iostream>
#include<string>

using namespace std;
int main() {
    int t;
    cin>>t;
    while(t--){
//this part here is my doubt... i want to accept integer after string input
        string s;
        int c;
        getline(cin,s);
        
        cin>>c;
        cin.ignore();
        cout<<s<<endl;
    }
    return 0;
}

它不会在 output 中打印任何内容。 即使我在输入 integer 之前放置 cin.ignore() ,它仍然不会在 output 中打印任何内容。

暂无
暂无

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

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