Reputation:
I was writing a code which task statements as inputs from user and searches for the word "not" being present in it or not if not is present then it prints Real Fancy else it prints regularly fancy but i am getting an error in such a simple program.
My Code looks like :
#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;
}
and the error i am getting once i run this code is :
main.cpp:11:30: error: no matching function for call to ‘getline(std::istream&, int&)’
Upvotes: 3
Views: 13850
Reputation: 12283
std::getline
has the following overloads:
istream& getline (istream& is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
istream& getline (istream& is, string& str);
istream& getline (istream&& is, string& str);
and, as you can see, none of them receives integer as a second parameter, so here are possible solutions for you:
std::stoi
in a following manner:std::string line;
std::getline(std::cin, line);
int T = std::stoi(line);
std::from_chars
as follows:int value = 0;
std::string line;
std::getline(std::cin, line);
const auto result = std::from_chars(str.data(), str.data() + str.size(), value);
if (result.ec == std::errc()) {
// strings is successfully parsed
}
Why is std::from_chars
better than std::stoi
?
Because std::from_chars
is non-throwing and gives you better error reporting (which will give you more info about the conversion outcome). Also, it is a bit faster than std::stoi
according to some resources. Read more about std::from_chars
here.
Upvotes: 1
Reputation: 39959
std::getline
has two overloads:
(1) istream& getline (istream& is, string& str, char delim);
(2) istream& getline (istream& is, string& str);
You are trying to call it with an int
parameter, which won't work.
There are two ways to solve this:
Use std::stoi
to parse the int
after reading into a std::string
Read from the stream directly into an int
by using std::cin >> T
. This will not consider new lines specially though and use any whitespace as a separator between ints. So if you are trying to parse exactly one int
per line, the former option will suit your better.
Upvotes: 2
Reputation: 1484
According to http://www.cplusplus.com/reference/string/string/getline/
You should be looking to use getline in this form:
istream& getline (istream& is, string& str);
istream& getline (istream&& is, string& str);
You do this:
int T;
std::getline(std::cin, T);
T
is an integer and there is no such call that can be made.
Upvotes: 0
Reputation: 64
std::getline()
function is not for integer type.
If you want to use std::getline()
for your integer, you should declare temporary string type value.
for example in your case
int T;
std::string tmp_s;
std::getline(std::cin, tmp_s);
T = std::stoi(tmp_s);
Upvotes: 2