![](/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.