views:

149

answers:

3

I get the following error when I compile:

convert.cpp: In function 'std::string convert()':
convert.cpp:62: error: expected primary-expression before 'else'
convert.cpp:62: error: expected `;' before 'else'
convert.cpp:108: error: expected `}' at end of input
convert.cpp:108: error: expected `}' at end of input 
convert.cpp:108: error: expected `}' at end of input 
convert.cpp:108: error: expected `}' at end of input 
convert.cpp:108: error: expected `}' at end of input

Code:

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

string infix;  // infix expression string
string operand;
string operate;
stack <string> mystack; // this stack is used to push the operator
string postfix;  // postfix string where the operand is appended

//....................................................................
// this function read the infix expression from user 
string input()
{
 cout<<" Enter the damn infix expression: "<<endl; 
 getline(cin, infix);
 return infix;
}
//......................................................................
// this function checks for operator precedence in the stack
int precedence(string e)
{
int f;
if(e == "*" || e== "/" || e =="%")
 f = 2;
else
{
if(e == "+" || e == "-")
 f = 1;
}

if(e=="."){
f=0;
}

return f;

}




//....................................................................
// This function converts infix to postfix
string convert()
{ 
 for(int i=0; i<infix.length(); i++)
 { 

  switch(infix[i]){

  // operate case start  
  case '+': case'-': case'*': case'/': case'^': case '(': case ')': case'.':

  operate=infix[i];
  {
  if(mystack.empty() || precedence(operate)>= precedence(mystack.top()))
  {
    mystack.push(operate);           

   else
   {
    while(precedence(operate)<= precedence(mystack.top()))
    {
     postfix.append(mystack.top());
     mystack.pop();
    }

    mystack.push(operate); 
   }

  }
  }//operate case closed

  default:        //when operand string char is parsed
  {
                        operand=infix[i];
                        postfix.append(operand);
                        break;

  } // default case closed

  }//switch closed

 }

while(!mystack.empty())
{
 postfix.append(mystack.top())
 mystack.pop();
}

return postfix;
cout<<postfix;
}



//...................................................................
int main()
{

input();

convert();
cout<<"postfix is "<<postfix<<endl;
} 
+4  A: 

It appears that your code is just missing some closing braces. That's all.

For example, look at this:

  operate=infix[i];
  {
  if(mystack.empty() || precedence(operate)>= precedence(mystack.top()))
  {
    mystack.push(operate);           

   else
   {
    while(precedence(operate)<= precedence(mystack.top()))

Where is the closing } before the else statement. Just go through your code and fix these silly mistakes.

It would be a lot easier to rid yourself of these things if you made your spacing and indentation a little neater.

Alexander Rafferty
The other problem in this code is a missing semi-colon towards the end, right after postfix.append(mystack.top())
jkerian
+1  A: 

The "expected primary expression before else" diagnostic is the compiler-author's way of reporting you rudely foisted on him an "else" without either a preceding "if" or (what amounts to the same thing) "else if". Alexander Rafferty correctly points out that this is because the code has ...

if (condition) {
  // ...
  else { }

... when perhaps what you meant was ...

if (condition) {
   // ...
}
else {
}

... though maybe you deleted a whole bunch of stuff by accident, and were lucky enough that the deletion resulted in unparseable code, so you will realize that something is wrong.

Thomas L Holaday
+1  A: 

Look at these lines:

if(mystack.empty() || precedence(operate)>= precedence(mystack.top()))
{
   mystack.push(operate);           

else
Martin York
Thanks people. I did silly mistakes.
udaysubedi