鱼C论坛

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

[已解决]getline 读不进去

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

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

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

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

  3. string temp;
  4. int n;

  5. int main(){
  6.     ios::sync_with_stdio(0);
  7.     cin.tie(0); cout.tie(0);
  8.    
  9.     cin >> n;
  10.     while(n--){
  11.         getline(cin, temp);
  12.         cout << temp << endl;
  13.     }
  14.    
  15.     return 0;
  16. }
复制代码
最佳答案
2022-9-4 14:35:06
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. int n;
  5. string temp;

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

  9.        
  10.         cin >> n;
  11.         getchar(); // <------------------------------

  12.         while (n--) {
  13.                 getline(cin, temp);
  14.                 cout << temp << endl;
  15.         }

  16.         return 0;
  17. }
复制代码
  1. 3
  2. apple
  3. apple
  4. banana
  5. banana
  6. oren
  7. oren
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

  4. int n;
  5. string temp;

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

  9.        
  10.         cin >> n;
  11.         getchar(); // <------------------------------

  12.         while (n--) {
  13.                 getline(cin, temp);
  14.                 cout << temp << endl;
  15.         }

  16.         return 0;
  17. }
复制代码
  1. 3
  2. apple
  3. apple
  4. banana
  5. banana
  6. oren
  7. oren
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

  9. #include <iostream>
  10. #include <string>
  11. #include <limits>

  12. using std::cin, std::cout, std::endl;
  13. using std::ios;
  14. using std::string;
  15. using std::numeric_limits, std::streamsize;

  16. // 用全局对象?
  17. // 为什么?
  18. // 局部对象不够用吗?
  19. /*
  20. string temp;
  21. int n;
  22. */

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

  28.     string temp;
  29.     int n;
  30.     cin >> n;
  31.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  32.     while(n--) {
  33.         getline(cin, temp);
  34.         cout << temp << endl;
  35.     }
  36.     return 0;
  37. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

使用道具 举报

发表于 2022-9-4 15:03:25 | 显示全部楼层
https://cplusplus.com/reference/iostream/cin/
  1. 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.

  2. By default, cin is synchronized with stdin (see ios_base::sync_with_stdio).
复制代码


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

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

使用道具 举报

发表于 2022-9-24 19:31:51 | 显示全部楼层
  1.     cin.tie(nullptr);
  2.     //cout.tie(nullptr);    // 上面一行把cout从cin中解除绑定,那这一行把谁从谁中解除绑定?
  3.                             // 把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-5-20 13:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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