WHat kind of error is this?

terminate called after throwing an instance of ‘std::logic_error’
what(): basic_string::_M_construct null not valid

The error shown is:
#include
#include
#include
using namespace std;

string getStr(string s,int i)
{
char *p=strtok((char )s.c_str()," “);
while(i>1)
{
p= strtok(NULL,” ");
i–;
}
string m=p;
return m;
}
int getint(string s)
{
int sum=0;
for(int i=0;i<s.length();i++)
{
sum=sum
10+(s[i]-‘0’);
}
return sum;
}
bool numeric(const pair<string,string>& a,const pair<string,string>& b)
{
string c=a.second;
string d=b.second;
return getint©<getint(d);

}
bool lexico(const pair<string,string>& a,const pair<string,string>& b)
{
string c=a.second;
string d=b.second;

return c<d;
}

int main()
{
int n;
cin>>n;
string s[n];
for(int i=0;i<n;i++)
{
getline(cin,s[i]);
}
int key;
string rev,type;
cin>>key>>rev>>type;
pair<string,string> pr[n];
for(int i=0;i<n;i++)
{
pr[i].first=s[i];
pr[i].second=getStr(s[i],key);
}
if(type==“lexicographical”)
sort(pr,pr+n,lexico);
else
sort(pr,pr+n,numeric);

if(rev=="true")
{
  for(int i=0;i<n/2;i++)
  {
    swap(pr[i],pr[n-1-i]);
  }
}
for(int i=0;i<n;i++)
{
	cout<<pr[i].first<<endl;
}
return 0;

}

@rajarshib03 The problem is the two return 0; statements in your function. The function returns a std::string, which has no constructors that accept an int as input. But, it does have a constructor that accepts a const char * pointer, which 0 is implicitly convertible to. However, constructing a std::string with a null char * pointer is undefined behavior, and your implementation has chosen to throw a std::logic_error exception that you are not catching in your code.

1 Like

#include
#include
#include
using namespace std;

string getStr(string s,int i)
{

char *p=strtok((char )s.c_str()," “);
while(i>1)
{
p=strtok(NULL,” ");
i–;
}
string m§;
return m;
}
int getint(string s)
{
int sum=0;
for(int i=0;i<s.length();i++)
{
sum=sum
10+(s[i]-‘0’);
}
return sum;
}
bool numeric(const pair<string,string>& a,const pair<string,string>& b)
{
string c=a.second;
string d=b.second;
return getint©<getint(d);

}
bool lexico(const pair<string,string>& a,const pair<string,string>& b)
{
string c=a.second;
string d=b.second;

return c<d;
}

int main()
{
int n;
cin>>n;
string s[n];
for(int i=0;i<n;i++)
{
getline(cin,s[i]);
}
int key;
string rev,type;
cin>>key>>rev>>type;
pair<string,string> pr[n];
for(int i=0;i<n;i++)
{
pr[i].first=s[i];
pr[i].second=getStr(s[i],key);
}
if(type==“lexicographical”)
sort(pr,pr+n,lexico);
else
sort(pr,pr+n,numeric);

if(rev=="true")
{
  for(int i=0;i<n/2;i++)
  {
    swap(pr[i],pr[n-1-i]);
  }
}
for(int i=0;i<n;i++)
{
	cout<<pr[i].first<<endl;
}
return 0;

}

Some part of your code is not visible here. Could you please share me a link of your codepair.

1 Like

Please send your code link.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like