![](/img/trans.png)
[英]C++ : How to skip first whitespace while using getline() with ifstream object to read a line from a file?
[英]How to handle whitespace while using getline() in C++?
我的程序(处理客户/物品的奖励系统)涉及多次从用户那里获取输入。 通常的情况是用户要么输入一个数字,要么输入一个字符串。 我使用标准 std::cin 处理用户输入的任何数字,并使用 std::getline() 处理他们输入的任何字符串。 我的问题是,当我在输入字符串(例如客户姓名或商品名称)之前/之后包含一个空格时,它无法正确读取它。 例如:在工作之前或之后输入项目名称“水管”或客户名称“约翰”,没有空格,但是像“水 pipe”或“约翰”之类的东西,之后/之前有一个空格,程序结束。 有没有办法让 std::getline() 在用户输入字符串之前/之后忽略空格? 我首先在我的 addPoints() function 中注意到了这个问题,所以我将在此处包含该问题,但这也发生在我的其他函数中。 这两个文件的内容结构相同,项目/客户名称,然后是它们下方的点。 所以像这样:
lighter
10
water pipe
10
等等。 这是代码:
std::cout << "Would you like to add points to an existing member? If not, input 0 to look at other options!" << std::endl;
std::cout<< "Otherwise, input 1 to continue to the point system." << std::endl;
std::cin >> answer;
std::cin.sync();
if (answer == '0')
options();
if (answer == '1') {
inFS.open("name.dat");
if (!inFS.is_open()){
std::cout << "Could not find name.dat." << std::endl;
exit(1);
}
while (std::getline(inFS, item)) {
std::getline(inFS, points);
nameList.push_back(make_pair(item, std::stoi(points)));
}
if (!inFS.eof())
std::cout << "Failure before reaching end of file." << std::endl;
inFS.close();
std::cout << "Please enter the username of the rewards member you would like to add points to." << std::endl;
std::getline(std::cin, userName);
std::cin.sync();
for (int i = 0; i < nameList.size(); i++) {
if (nameList[i].first == userName) {
validName = true;
break; //fix this later so it includes a 'sorry, invalid name' type of comment//
}
if (nameList[i].first != userName) {
validName = false;
continue;
}
}
if (validName == false)
nameList.clear();
if (validName == true) {
std::cout << "Which rewards product did " << userName << " purchase?" << std::endl;
std::getline(std::cin, product);
//find out why adding a space at the end of each input ends the program//
std::cin.sync();
inFS.open("items.dat");
if (!inFS.is_open()){
std::cout << "Could not find items.dat." << std::endl;
exit(1);
}
while (std::getline(inFS, item)) {
std::getline(inFS, points);
itemList.push_back(make_pair(item, std::stoi(points)));
}
if (!inFS.eof())
std::cout << "Failure before reaching end of file." << std::endl;
inFS.close();
我没有仔细阅读您的程序,但 std::getline 总是读取(然后丢弃)换行符'\n'
。 相反,请尝试在调用std::getline
后修剪字符串。 不幸的是,没有用于修剪空白的内置函数,但以下函数有效(从https://stackoverflow.com/a/217605/8421788复制粘贴):
#include <algorithm>
#include <functional>
#include <cctype>
#include <locale>
// trim from start (in place)
static inline void ltrim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
}
// trim from end (in place)
static inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}
// trim from both ends (in place)
static inline void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
然后,您可以通过以下方式使用它们:
std::getline(std::cin, userName);
userName = trim(userName);
/* ... process user name as needed ... */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.