鱼C论坛

 找回密码
 立即注册
查看: 2072|回复: 6

[已解决]getline 读不进去

[复制链接]
发表于 2022-9-4 14:25:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码如下, getline 第一次的时候直接给了空格 , 该怎么解决
#include <bits/stdc++.h>
using namespace std;

string temp;
int n;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    
    cin >> n;
    while(n--){
        getline(cin, temp);
        cout << temp << endl;
    }
    
    return 0;
}
最佳答案
2022-9-4 14:35:06
#include <iostream>
#include <string>
using namespace std;

int n;
string temp;

int main(void) {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

        
        cin >> n;
        getchar(); // <------------------------------

        while (n--) {
                getline(cin, temp);
                cout << temp << endl;
        }

        return 0;
}
3
apple
apple
banana
banana
oren
oren
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-9-4 14:35:06 | 显示全部楼层    本楼为最佳答案   
#include <iostream>
#include <string>
using namespace std;

int n;
string temp;

int main(void) {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

        
        cin >> n;
        getchar(); // <------------------------------

        while (n--) {
                getline(cin, temp);
                cout << temp << endl;
        }

        return 0;
}
3
apple
apple
banana
banana
oren
oren
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-4 14:58:41 | 显示全部楼层
// 有可能导致名字冲突
// 想一想,C++为什么要引入名字空间呢?
// using namespace std;
// 你这一行代码直接禁用了C++的名字空间
/*
#include <bits/stdc++.h>
using namespace std;
*/

#include <iostream>
#include <string>
#include <limits>

using std::cin, std::cout, std::endl;
using std::ios;
using std::string;
using std::numeric_limits, std::streamsize;

// 用全局对象?
// 为什么?
// 局部对象不够用吗?
/*
string temp;
int n;
*/

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    //cout.tie(nullptr);    // 上面一行把cout从cin中解除绑定,那这一行把谁从谁中解除绑定?
                            // 把cout从cout中解除绑定?

    string temp;
    int n;
    cin >> n;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    while(n--) {
        getline(cin, temp);
        cout << temp << endl;
    }
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-4 15:02:27 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-4 15:03:25 | 显示全部楼层
https://cplusplus.com/reference/iostream/cin/
 cin is tied to the standard output stream cout (see ios::tie), which indicates that cout's buffer is flushed (see ostream::flush) before each i/o operation performed on cin.

By default, cin is synchronized with stdin (see ios_base::sync_with_stdio).

https://cplusplus.com/reference/iostream/cout/
 cout is not tied to any other output stream (see ios::tie).

By default, cout is synchronized with stdout (see ios_base::sync_with_stdio).
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-24 19:31:51 | 显示全部楼层
    cin.tie(nullptr);
    //cout.tie(nullptr);    // 上面一行把cout从cin中解除绑定,那这一行把谁从谁中解除绑定?
                            // 把cout从cout中解除绑定?

https://fishc.com.cn/forum.php?m ... 357&pid=5957044
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-24 19:33:55 | 显示全部楼层
人造人 发表于 2022-9-24 19:31
https://fishc.com.cn/forum.php?mod=redirect&goto=findpost&ptid=217357&pid=5957044

我发现我回复错地方了,^_^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-27 22:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表