gpt4 book ai didi

c++ - 没有调用 getline(std::istream&, int&) 的匹配函数

转载 作者:行者123 更新时间:2023-12-02 00:47:02 25 4
gpt4 key购买 nike

我正在编写一个代码,该代码将任务语句作为用户的输入,并搜索其中是否存在“不”一词如果不存在,那么它会打印Real Fancy,否则它会打印regularly fancy,但我在这样一个简单的程序中遇到错误。

我的代码如下所示:

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

int main () {
string str2 (" not ");
string str3 ("not ");
string str4 ("not");

int T;
std::getline(std::cin, T);
for (int k=0;k<T;k++){
string str ;


std::getline(std::cin, str);
int len = str.size();
//condition for only not as a sentence
if ((str.find(str4) != string::npos) && len ==3) {
cout<<"Real Fancy";
}
// condition to check for not word in middle of a sentence eg. this is not good
else if ((str.find(str2) != string::npos) ) {
cout<<"Real Fancy";
}
// condition if the statement ends with the word not
else if (str[len-1]=='t' && str[len-2]== 'o' && str[len-3]== 'n' && str[len-4]== ' '){
cout<<"Real Fancy";
}
// code to check for if statement starts with word not
else if ((str.find(str3) != string::npos) ) {
cout<<"Real Fancy";
}
else {
cout<<"regularly fancy";
}
cout<<endl;
}
return 0;
}

运行此代码后出现的错误是:

main.cpp:11:30: error: no matching function for call to ‘getline(std::istream&, int&)’

最佳答案

std::getline() 函数不适用于整数类型。如果您想对整数使用 std::getline() ,您应该声明临时字符串类型值。例如你的情况

int T;
std::string tmp_s;
std::getline(std::cin, tmp_s);
T = std::stoi(tmp_s);

关于c++ - 没有调用 getline(std::istream&, int&) 的匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60108222/

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