I had tried to excute the logic but program is trapped in the sort function and "bye you" is never printed

#include
#include
#include
using namespace std;

string extract_key(string s,int k)
{
char *ch=strtok((char *)s.c_str()," “);
while(k>1)
{
ch=strtok(NULL,” ");
k–;
}
return (string)ch;
}

int to_int(string s)
{
int p=1;
int ans=0;
for(int i=s.length()-1;i>=0;i++)
{
ans+=((s[i]-‘0’)p);
p
=10;
}
return ans;
}
bool compare(pair<string,string> s1,pair<string,string>s2)
{
string key1,key2;
key1=s1.second;
key2=s2.second;
cout<<key1<<" “<<key2<” ";
return to_int(key1) < to_int(key2);
}

int main()
{
int n;cin>>n;
cin.ignore();

string str[n];
for(int i=0;i<n;i++)
getline(cin,str[i]);

int key;
string reverse,ordering;
cin>>key>>reverse>>ordering;
cout<<ordering<<endl;

pair <string,string> strpair[n];
for(int i=0;i<n;i++)
{
	strpair[i].first=str[i];
	strpair[i].second=extract_key(str[i],key);
	cout<<strpair[i].second<<" ";
}

if(ordering =="numeric")
 sort(strpair,strpair+n-1,compare);

else
sort(strpair,strpair+n);
cout<<"bye";
for(int i=0;i<n;i++)
cout<<strpair[i].first<<endl;

}

I`ll debug the question and let u know the problem

@Nidhi_Alipuria
bro

int to_int(string s)
{
int p=1;
int ans=0;

for(int i=s.length()-1;i>=0;i–)

in the to_int function u have incremented the value of i it should be – since u are starting from s.length()

and also u havent understood the question completely the sort function has variety of varaition
sorting is also based on key 1
u need to find out the part 1 of the string not just the second part

please try it out furthur in case u are unable to do it comment below i`ll help

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.