In some of my programs I encounter error message in the middle of the successful start without debugging.... is there a code showing the reason of the error message?

depends on the error message. compile your program for debugging and find out what's causing the problem.

depends on the error message. compile your program for debugging and find out what's causing the problem.

I know what you mean but for the clearness of my interrogative statement is i got screen shots but I don't know how to post it here
Do you know some way to post pictures here?

Press the Go Advance button, then scroll down near the bottom of the screen and you will see Manage Attachments. Hit that, then the browse button will let you find the file on your computer.

Press the Go Advance button, then scroll down near the bottom of the screen and you will see Manage Attachments. Hit that, then the browse button will let you find the file on your computer.

here is the code:

#include <iostream>
#include <string>
#include <stack>
#include <cctype>  //alnum
#include <cassert> //assert
using namespace std;
#include <sstream>

string  infixTOposfix(string);
int evaluarRPN(string);

int main ()
{ // main began

string infix,
       posfixWspaces,
       dummy;
double result;
char repeat;

do {
cout << "\nPlease enter the expression in INFIX: ";

getline(cin,infix);
posfixWspaces = infixTOposfix(infix);

cout << "This is the expression in POSTFIX: " << posfixWspaces << endl;

result = evaluarRPN(posfixWspaces);
cout << "The expression after the evaluation: " << result;

cout << "\nDo you want to repeat? Enter 'y' ";
cin >> repeat;
getline(cin,dummy);
} while ( repeat == 'y');

system("PAUSE");
return 0;
} // main end


string infixTOposfix(string exp)
{
/*-------------------------------------------------------------------------
   Function to convert an infix expression exp to postfix.

   Precondition:  None
   Postcondition: Postfix expression corresponding to exp is returned
       or an error message displayed if exp is not well-formed.
-------------------------------------------------------------------------*/

   char token,                   // character in exp
        topToken;                // token on top of opStack
   stack<char> opStack;                // stack of operators
   string postfixExp;            // postfix expression
   const string BLANK = " ";
   for (int i = 0; i < exp.length(); i++)
   {
      token = exp[i];
      switch(token)
      {
         case ' ' : break;       // do nothing -- skip blanks
         case '(' : opStack.push(token);
                    break;
         case ')' : for (;;)
                    {
                       assert (!opStack.empty());
                       topToken = opStack.top();
                       opStack.pop();
                       if (topToken == '(') break;
                       postfixExp.append(BLANK + topToken);
                    }
                    break;
         case '+' : case '-' :
         case '*' : case '/': case'%':
                    for (;;)
                    {
                       if (opStack.empty() ||
                           opStack.top() == '(' ||
                           (token == '*' || token == '/' || token == '%') &&
                           (opStack.top() == '+' || opStack.top() == '-'))
                       {
                          opStack.push(token);
                          break;
                       }
                       else
                       {
                          topToken = opStack.top();
                          opStack.pop();
                          postfixExp.append(BLANK + topToken);
                       }
                    }
                    break;
         default :  // operand
                    postfixExp.append(BLANK + token);
                    for(;;)
                    {
                       if ( !isalnum(exp[i+1]) ) break; // end of identifier
                       i++;
                       token = exp[i];
                       postfixExp.append(1, token);
                    }
      }
   }
   // Pop remaining operators on the stack
   for (;;)
   {
      if (opStack.empty()) break;
      topToken = opStack.top();
      opStack.pop();
      if (topToken != '(')
      {
         postfixExp.append(BLANK + topToken);
      }
      else
      {
         cout << " *** Error in infix expression ***\n";
         break;
      }
   }
   return postfixExp;
}

int evaluarRPN(string posfijo)
{ // evaluarRPN began
stack<int> numStack;

int x, 
    y,
    toInt;
  
string number;

for (int i = 0; i < posfijo.length(); i++)
 { // for began  

  if ( isdigit(posfijo[i]) )
  { // if began
   number = posfijo[i];
   stringstream ss( number );
   ss >> toInt;
   numStack.push( toInt );
  } // if end
  
  else if (posfijo[i] != ' ' )
  { // else began
      y = numStack.top();
      numStack.pop();
      x = numStack.top();
      numStack.pop();
      
     if (posfijo[i] == '+')
        numStack.push( x+y );
        
     else if (posfijo[i] == '-')
        numStack.push( x-y );
        
     else if (posfijo[i] == '*')
        numStack.push( x*y );
             
     else
        numStack.push( x/y );
  } // else end
  
 } // for end
return numStack.top(); 
} // evaluarRPN end

what should i do now?
there are lots of math errors

Member Avatar for iamthwee

>what should i do now?
Debug it.

commented: that's what I was thinking too :) +34

The problem seems to be in this code snippet

else if (posfijo[i] != ' ' )
  { // else began
      y = numStack.top();
      numStack.pop();
      x = numStack.top();
      numStack.pop();

Apparently the stack is empty when top() is called.

How to fix it -- I don't know without doing a lot of debugging. That part I leave up to you to figure out how to do.

Member Avatar for iamthwee

You also seem to be lacking any part to handle the percentage operator: The evaluation part at the end, I mean.

The problem seems to be in this code snippet

else if (posfijo[i] != ' ' )
  { // else began
      y = numStack.top();
      numStack.pop();
      x = numStack.top();
      numStack.pop();

Apparently the stack is empty when top() is called.

How to fix it -- I don't know without doing a lot of debugging. That part I leave up to you to figure out how to do.

Hey thanks for that... :) I bumped my head on the wall for that... It only works single digit I dunno if there's a possible snippet or code that works also in more than 1 digit.... decimal value and exponents... do you have any tip for me?

Hey thanks for that... :) I bumped my head on the wall for that... It only works single digit I dunno if there's a possible snippet or code that works also in more than 1 digit.... decimal value and exponents... do you have any tip for me?

:icon_rolleyes: try to get in touch with the author of this code and ask if he kindly could/would supply you with the necessary code.

Be a part of the DaniWeb community

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