gpt4 book ai didi

c++ - 字符串和 getline 字符

转载 作者:行者123 更新时间:2023-11-28 04:06:56 25 4
gpt4 key购买 nike

  1. 当我运行代码时,输​​出是我的第一个和姓氏,仅此而已。
  2. 我使用过 cin.clear、cin.sync 和 cin.ignore。这些似乎都不起作用。但是,当我使用cin.fail,为什么会成功?
#include<iostream>
#include<string>
using namespace std;

int main()
{
string str_mystr;

cout << "What is your name? ";
getline (cin, str_mystr, '#');
cout << "Hello " << str_mystr << '\n';
// cin.ignore();
cout << "What is your favorite city? ";
getline (cin, str_mystr);
cout << "I like " << str_mystr << '\n';
return 0;
}

最佳答案

做你为名所做的事

#include<iostream>
#include<string>
using namespace std;

int main()
{
string str_mystr;

cout << "What is your name? ";
getline (cin, str_mystr, '#');
cout << "Hello " << str_mystr << '\n';
// cin.ignore();
cout << "What is your favorite city? ";
getline (cin, str_mystr,'#');
// ^^^^

cout << "I like " << str_mystr << '\n';
return 0;
}

添加“#”就可以了

关于c++ - 字符串和 getline 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58571556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com