[英]How to fix “no instance of overloaded function ”getline“ matches the argument list” when using getline with an int
Im currently working on an assignment and am trying to use try catch error handling for checking a user's input is a valid int. 我目前正在进行一项任务,我正在尝试使用try catch错误处理来检查用户的输入是否是有效的int。
I currently have this: 我目前有这个:
int inputValidation() {
int e = 0;
std::string es;
bool check = false;
do {
try {
if (!getline(std::cin, e)) {
throw stringInput;
}
else {
check = true;
}
}
catch (std::exception& er) {
std::cout << "Error! " << er.what() << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
} while (!check);
return e;
}
my issue is with the if ((getline(std::cin, e))){}
part. 我的问题是if ((getline(std::cin, e))){}
部分。 I've also tried using std::cin.getline(e, 256)
我也尝试过使用std::cin.getline(e, 256)
When calling the function I'm using this loop: 调用函数时我正在使用这个循环:
do {
std::cout << "Please select a month: ";
selectedMonth = inputValidation();
} while (selectedMonth < 1 || selectedMonth >(12 - actualMonth));
This simply makes sure they can only input a month from the current month until December. 这只是确保他们只能输入从当月到12月的一个月。
I'm aware that I can use es
instead of e
, however that defeats the purpose of the error checking. 我知道我可以使用es
而不是e
,但这会破坏错误检查的目的。 My only idea coming off of this is to check on a conversion. 我唯一的想法是检查转换。
For whatever reason I seem to be getting the error "no instance of overloaded function "getline"" and am unsure of where I'm going wrong. 无论出于何种原因,我似乎得到错误“没有重载函数的实例”getline“”并且我不确定我哪里出错了。 If anyone could provide some insight I would be very grateful. 如果有人能提供一些见解,我将非常感激。
if std::cin >> e
is not appropriate, you could use istringstream
: 如果std::cin >> e
不合适,可以使用istringstream
:
std::string asText;
std::getline(cin,asText);
std::istringstream iss (asText);
if (iss >> e)
First Version 第一版
I managed to fix this by changing it to: 我设法通过将其更改为:
int inputValidation(std::string message) {
int e = NULL;
std::string es;
bool check = false;
do {
try {
std::cout << message;
getline(std::cin, es);
if (!atoi(es.c_str())) {
throw stringInput;
}
else {
e = atoi(es.c_str());
check = true;
}
}
catch (std::exception& er) {
std::cout << "Error! " << er.what() << std::endl;
}
} while (!check);
return e;
}
//In another function -->
do {
selectedMonth = inputValidation("Please select a month: ");
} while (selectedMonth < 1 || selectedMonth >(12 - actualMonth));
Working Version 工作版
As of the comments I changed this. 截至评论我改变了这一点。
(The only difference is this version doesn't include an exception) (唯一的区别是这个版本不包括例外)
bool checkInput(int &input, std::string message) {
try {
std::cin >> input;
if (!std::cin.fail()) {
return true;
}
else {
throw(message);
}
}
catch (std::string e) {
std::cout << "Invalid input!" << std::endl;
std::cout << e << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return false;
}
}
//Elsewhere -->
std::cout << "Please input the lowest number you would like to check" << std::endl;
while (!checkInput(lowestNumber, "Please input the lowest number you would like to check"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.