dimanche 3 avril 2016

program doesnt enter if condition , gives segmentation fault

the code is somewhat like

cin >> n  // test cases
for( n times )
{ 
 .....
  for(;;)
    {
     getline(cin,inp) //inp is supposed to be +,-,*,\,0-9,if simply enter pressed program passes out of loop
      for(;;)
      {
       if (inp[0] == '*')
       {
        do stuff...
       }

       if (inp[0] == '\')
       {
        do stuff...
       }         

       if (inp[0] == '+') \\ this is where the problem occurs
       {
        do stuff...
       } 
    .....
    .....
        }
    }
return 0 ;
}

here is the actual code with some comments ,i have tried converting infix to postfix inn the program . here's my input and the problem...

>1          //1 test case


>3           //if condition as it should entered line 124

>inp[0] 3   //confirms 3 is inp[0]

>*          //works as it should (if condn entered line 37)

>inp[0] *   

>/          //works as it should

>inp[0] /

>+          //problem!! if condition not entered even though it should 
            //as inp[0] == '+' (line 61)
>inp[0] +   //confirmation inp[0] is + right before if condition checked

>Segmentation fault (core dumped)

I tried my level best to explain the code and the problem briefly, i ask this question as the problem looks totally ambiguos to me , any explanation needed pls tell me .

Also , give any suggestion to debug problems like these , i have got such errors before and i cant everytime ask here. Give any reccomendation to an beginner->amatuer coder for solving such segmentation faults .

thanks and regards

Aucun commentaire:

Enregistrer un commentaire