Search code examples
c++functioninsertreturnspaces

inserting spaces function after char worked outside the code but inside return error in C++


I made a function inserting spaces after a specific char(s) it works perfect for any long ,any char when separated. like now

using namespace std;
insert_spaces_before_delims(string a)
{
 vector<int> found;
 int temp;
 int i=0;
//string a="Ahmeed+Khaled+awwad=Ahmedd-AWWAd";                //string be a parameter
 temp = a.find_first_of("+-=");                              // chars be parameters if need to change
 found.push_back(temp);
 a.insert(found[i]," ");
 while(a.find_first_of("+-=",found[i]+2)!= string::npos)
 {
  temp = a.find_first_of("+-=",found[i]+2);
  found.push_back(temp);
  a.insert(found[i+1]," ");
  i++;
 }
}
int main(void)
{
 string equation;
 getline(cin,equation);
 insert_spaces_before_delims(equation);
 //the output is the string with spaces before every +,- and = 
}

i debug the function when returns that error "terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::replace: __pos (which is 4294967295) > this->size() (which is 0)"

by condition (==string::npos) why it comes again in the code and stop the code from continue , just return the error and end the program.

struct variable_content
{
//string coefficient;                 // coefficient of x ( value before x)
    int value;                           // value of X = coefficient but in another type
    string order;                       // order (number ) of x (number after x)
};
spilit__each_string(string,char,variable_content,vector<string>,vector<variable_content>);
spliting_each_variable(char ,variable_content ,vector<string> ,vector<variable_content>);
insert_spaces_before_delims(string );

int main()
{
  int number_equations;
   string equation;                             //receive string
   vector<string> equations;                    // vector for equations input from user
   vector<string> variables;                    // initial empty vector of string for each var as strings at all
   vector<variable_content> variable;           //initial empty vector of struct for each var
   char delim[] = " ";
   char delim1[]="xX";
   cin>>number_equations;
   number_equations++;
   variable_content temp;
   for (int i=0; i<number_equations;i++)
   {
     getline(cin,equation);                             //worked
   //***the problem function , if i commented it the code works fine**** 
    insert_spaces_before_delims(equation);              //return error and stop the code
    equations.push_back(equation);                    //worked
    //make a temp struct and put tok1 in coefficient of it
    spilit__each_string(equation, delim,temp,variables,variable);//worked
    spliting_each_variable(delim1,temp ,variables,variable);     //worked
    }
   //printing vector of struct of every variable
    for(int y=0; y<variable.size();y++)
    {
       cout<<variable[y].value<<"\t"<<variable[y].order<<endl;
    }
}

that the important parts and the link of the whole code on github.https://github.com/AhmedKAwwad/Split-String-into-vector/blob/master/main.cpp


Solution

  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ this link make me debug my code and found that having cin>> and getline mixed in my code do the error , then i use getline and then convert the string to integer thanks for comments