Hallo guys and sry for the huge code , this programm is ment to output a random string generated through certain rules , as it seems when run everything is ok but out of nowhere i get thrown a call for termination
(v+Y)
temp = 24
turning E to (Y)
token1 is (v+Y)
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 25) > this->size() (which is 5)
now if you look at the function that gives me the temp (called search()),temp shouldnt even be close to 24
Here is my code:
<pre>#include <iostream>
using namespace std;
#include <stdlib.h>
#include <cstring>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
int countparan(string s)
{
int count=0;
for (int i = 0; i != s.size(); i++)
if (s[i] == '(')
count++;
return count;
}
int countv(string s)
{
int count=0;
for (int i = 0; i != s.size(); i++)
if (s[i] == 'v')
count++;
return count;
}
int countplus(string s)
{
int count=0;
for (int i = 0; i != s.size(); i++)
if (s[i] == '+')
count++;
return count;
}
int countminus(string s)
{
int count=0;
for (int i = 0; i != s.size(); i++)
if (s[i] == '-')
count++;
return count;
}
int countparanklin(string s)
{
int count=0;
for (int i = 0; i != s.size(); i++)
if (s[i] == ')')
count++;
return count;
}
int search(char el,string s)
{
int n = s.size();
int i = 0;
bool flagf=false;
while (i <n )
{
if (s[i] == el )
{
flagf=true;
break ;
}
i++;
}
if(flagf=false)
{
i=-1;
}
return i;
}
string symbo(string st)
{
st="E";
int temp;
bool flag=true;
int step=0;
while(step<30 && flag )
{
cout<<"in the while \n";
cout<<"string is "<<st<<"\n";
temp=search('E',st);
cout<<"temp = "<<temp<<"\n";
if(temp!=-1)
{
cout<<"turning E to (Y) \n";
string token1=st.substr(0,temp+1);
cout<<"token1 is "<<token1<<"\n";
string token2=st.substr(temp+1,st.size()-1);
cout<<"token2 is "<<token2<<"\n";
token1.pop_back();
cout<<"turning E to (Y) \n";
token1=token1+"(Y)";
cout<<"done \n";
st=token1+token2;
cout<<"done and string is "<< st<<"\n";
step++;
}
temp=search('Y',st);
if(temp!=-1)
{
cout<<"turning Y to A and B \n";
cout<<"temp = "<<temp<<"\n";
string token1=st.substr(0,temp+1);
cout<<"token1 is "<<token1<<"\n";
string token2=st.substr(temp+1,st.size()-1);
cout<<"token2 is "<<token2<<"\n";
token1.pop_back();
cout<<"poped\n";
cout<<"turning Y to A and B \n";
token1=token1+"AB";
cout<<"done \n";
st=token1+token2;
cout<<"done and string is "<< st<<"\n";
step++;
}
temp=search('A',st);
if(temp!=-1)
{
srand((unsigned) time(0));
int p =(rand() % 2);
if(p==0)
{
cout<<"turning A to v \n";
cout<<"temp = "<<temp<<"\n";
string token1=st.substr(0,temp+1);
cout<<"token1 is "<<token1<<"\n";
string token2=st.substr(temp+1,st.size()-1);
cout<<"token2 is "<<token2<<"\n";
token1.pop_back();
cout<<"turning A to v \n";
token1=token1+"v";
cout<<"done \n";
st=token1+token2;
cout<<"done and string is "<< st<<"\n";
step++;
}
if(p==1)
{
cout<<"turning A to E \n";
cout<<"temp = "<<temp<<"\n";
string token1=st.substr(0,temp+1);
cout<<"token1 is "<<token1<<"\n";
string token2=st.substr(temp+1,st.size()-1);
cout<<"token2 is "<<token2<<"\n";
token1.pop_back();
cout<<"turning A to E \n";
token1=token1+"Ε";
cout<<"done \n";
st=token1+token2;
cout<<"done and string is "<< st<<"\n";
step++;
}
}
temp=search('B',st);
{
srand((unsigned) time(0));
int p =(rand() % 3);
if(p==0)
{
cout<<"turning B to -<Y> \n";
cout<<"temp = "<<temp<<"\n";
string token1=st.substr(0,temp+1);
cout<<"token1 is "<<token1<<"\n";
string token2=st.substr(temp+1,st.size()-1);
cout<<"token2 is "<<token2<<"\n";
token1.pop_back();
cout<<"turning B to -<Y> \n";
token1=token1+"-Y";
cout<<"done \n";
st=token1+token2;
cout<<"done and string is "<< st<<"\n";
step++;
}
if(p==1)
{
cout<<"turning B to +<Y> \n";
cout<<"temp = "<<temp<<"\n";
string token1=st.substr(0,temp+1);
cout<<"token1 is "<<token1<<"\n";
string token2=st.substr(temp+1,st.size()-1);
cout<<"token2 is "<<token2<<"\n";
token1.pop_back();
cout<<"turning B to +<Y> \n";
token1=token1+"+Y";
cout<<"done \n";
st=token1+token2;
cout<<"done and string is "<< st<<"\n";
step++;
}
if(p==2)
{
cout<<"turning B to empty \n";
cout<<"temp = "<<temp<<"\n";
string token1=st.substr(0,temp+1);
cout<<"token1 is "<<token1<<"\n";
string token2=st.substr(temp+1,st.size()-1);
cout<<"token2 is "<<token2<<"\n";
token1.pop_back();
cout<<"turning B to empty \n";
cout<<"done \n";
st=token1+token2;
cout<<"done and string is "<< st<<"\n";
step++;
}
}
if(pa(st)+v(st)+pl(st)+pk(st)+min(st)==st.size() ==st.size() || step>30)
{
cout<<"the allowed steps have been met and the string is this one\n ";
flag=false;
}
}
cout<<"this is the string "<<st<<"\n";
}
int main()
{
string s="f";
symbo(s);
}
Thank you very much in advance.
What I have tried:
Dont really know what to try cause i cant explain this at all..