#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;


bool opgreater(char ,char);


  bool opgreater(char a ,char b)
{
  if(a=='^')
    return 1;
  else 
    return((a=='*' || a== '/')>=(b=='*' || b=='/'));
}

int main()
{
  int n;
  scanf("%d",&n);
  while (n--)
    {
  stack<char> exp;
  string initial;
  string final="";
  int i=0;
  bool x;
  char temp,temp2;

  getline(cin,initial,'\n');
  
  initial+='\0';

  while(initial[i]!='\0')
    {    

    
    
      if(initial[i]=='^' ||initial[i]=='/'||initial[i]=='*'||initial[i]=='+'||initial[i]=='-')
	{
	  while(!exp.empty() && exp.top()=='(')
	    {
	      temp2=exp.top();	      
		x=opgreater(temp2,initial[i]);

	      if(x)
		{
		  temp=exp.top();
		  exp.pop();
		  final+=temp;
		}
		else
		  {
		    exp.push(initial[i]);
		    break;
		  }
	    }
	}
	  else if(initial[i]=='(')
	    exp.push(initial[i]);
	  else if(initial[i]==')')
	    {
	      while(exp.top()!='(')
		{
		  temp=exp.top();
		  exp.pop();

		 final+=temp;
		}
	      exp.pop();
	    }
	  else
	    {
	      final+=initial[i];
	    }
      i++;  
    }

  cout<<final;
    }
}

I am encountering a segmentation fault when i run this program .. please help me..

Recommended Answers

Does your program directly crash after executing it?
If no, what input are you entering?

Also please answer the following:
What compiler are you using?
What OS are you on?

BTW, what are you going to do when the user enters a negative value at the start of …

Jump to Post

you should use code=cplusplus rather than code=cpp because that doesn't work! also, if you don't have / know how to use a debugger, then you should do "printf debugging" which means outputting text so you can tell where the program crashes. That will probably help you find the segfault.

Jump to Post

All 7 Replies

Does your program directly crash after executing it?
If no, what input are you entering?

Also please answer the following:
What compiler are you using?
What OS are you on?

BTW, what are you going to do when the user enters a negative value at the start of your program?
(see: while (n--) )
You'll create a never-ending loop then.

i am using GNU g++ compiler and Linux OS

you should use code=cplusplus rather than code=cpp because that doesn't work! also, if you don't have / know how to use a debugger, then you should do "printf debugging" which means outputting text so you can tell where the program crashes. That will probably help you find the segfault.

1>

if(initial=='^' ||initial=='/'||initial=='*'||initial=='+'||initial=='-')
{
while(!exp.empty() && exp.top()=='(')
{
temp2=exp.top();
x=opgreater(temp2,initial);

The actual statement needed here is exp.top()!='(' 2>And your code is just half way across.Its just extracting the symbols used.Its not converting it from input to output.

3>And ya no need to declare the function opgreater because you are defining the function before main and using it inside main.

sorry abt two threads. I didnt see in the forum my first thread. So i had to write another thread. Now its here

sorry abt two threads. I didnt see in the forum my first thread. So i had to write another thread. Now its here

Mark one as solved so that people can direct their attention towards others in need.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.