Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
<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..
Posted
Updated 21-Jan-21 19:55pm
v3
Comments
jeron1 21-Jan-21 19:58pm    
if(flagf=false) <== in the search() function

This doesn't do what you want, you need two equal signs for a comparison like this.

if (flagf == false)
or
if (!flagf)
RastamanVibrations 22-Jan-21 4:39am    
this was what was wrong in the end ,after changing this i no longer get calls for terminations , thanks alot my friend for reading through my code and finding my mistake , i highly apreciate it
jeron1 22-Jan-21 12:15pm    
You are welcome.
CPallini 22-Jan-21 2:34am    
It looks your code is trying to validate a string generated with the grammar you posted here:
https://www.codeproject.com/Questions/5292703/In-need-of-string-generator-based-on-a-grammar
However it is not clear what are the exact requirements of your validation.
RastamanVibrations 22-Jan-21 4:37am    
my friend,thanks again for your previous answer on my previous question , your code although briliantly simple and functional , i cannot use it in a way to provide to the user the steps taken to produce the string . So i had to resort to making another programm that in the end allowed me to change the non-terminal symbols into terminal ones,all this is made randomly using a seeded rand , although i cannot use your code as is , still i highly aprecciate your answer

1 solution

You could use brackets to make your code readable

int n = sizeof(s);
   int i = 0;
   bool flagf=false;
   while (i <n )
   {
       if (s[i] == el )
       {
           flagf=true;
           break ;
       }
       i++;

   }


Are you stepping through? Is sizeof(s) returning the same as s.size()? Why use it?
 
Share this answer
 
Comments
RastamanVibrations 21-Jan-21 19:44pm    
ok, i am sry for the messy code , but as it seems even after changing the sizeof into s.size() i still get the error,how is my search() function detecting an E when the string is (v-Y) or (v+Y) etc.It just doesnt make sense.
Christian Graus 21-Jan-21 19:45pm    
You posted a lot of code. Step through it and tell us which line blows up
RastamanVibrations 21-Jan-21 19:48pm    
its not a certain line thoe , i get thrown a termination
lets say the next string to be checked again is :
string is (v-Y)
temp = 5
turning E to (Y)--> it means that the search() function(the same you posted in code) detected a letter E while there obviously isnt one
token1 is (v-Y)
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 6) > this->size() (which is 5)
Christian Graus 21-Jan-21 19:50pm    
You need to learn to set breakpoints and debug your code. I think you're saying the whole thing blows up. I'm going to guess that you are getting infinite recursion and your stack is blowing out. That is, you call that function over and over until the system blows up
RastamanVibrations 21-Jan-21 19:53pm    
so the problem i am facing here is that ,by calling the same function over i cause a stack overflow ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900