#include <iostream.h>
#include <conio.h>

int main()


create s
push (s, '#');

while (not end of infix input)
{
	ch = get char;
	
	if (ch is an operand)
		add ch to postfix expression;
	if (ch is a '(')
		push (s, ch);
	if (ch is a ')')
	{
		pop (s);
		while (ch !='('))
		{
			add ch to postfix expression;
			pop (s);
		}
	}

	if (ch is an operator)
	{
		while (!isEmpty(s)&&(precedence(stackTop())>=precedence(ch)))
		{
			pop (s);
			add ch to postfix expression;
		}
		push (s, ch);
	}
}

while (stackTop() !='#')
{
	pop (s);
	add ch to postfix expression;

getch();
return 0;
}

and what is below error about?
error C2146: syntax error : missing ';' before identifier 'create'
fatal error C1004: unexpected end of file found
Error executing cl.exe.

Cpp1.obj - 2 error(s), 0 warning(s)

Dani AI

Generated

Your snippet is algorithmic pseudocode, not real C++, so the compiler stops when it sees words like create where it expects a C++ declaration or a semicolon. As said, that explains the syntax errors; and as asked, the goal is to convert an infix expression to postfix (use a stack-based algorithm). Common causes here are missing or mismatched braces/parentheses, nonstandard headers, and using pseudocode keywords instead of C++ statements.

Quick checklist before trying to compile:

  • Use modern headers (<iostream>, <stack>, <string>, <cctype>) and avoid obsolete/nonportable ones.
  • Put a proper function body after int main() and make sure every opening brace/parenthesis has a matching closer.
  • Use std::stack<char> for operators and std::string for input/output.
  • Implement a precedence function and account for right-associative operators (like ^).
  • When treating characters as operands, call std::isalnum(static_cast<unsigned char>(ch)) to be safe.
  • If you need multi-digit numbers or spaces, tokenize the input instead of processing single chars.

A minimal, modern C++ example that implements infix-to-postfix (single-character operands) follows — try compiling this and then adapt it for multi-digit tokens if needed:

#include <iostream>
#include <stack>
#include <string>
#include <cctype>

int precedence(char op) {
    if (op == '^') return 3;
    if (op == '*' || op == '/') return 2;
    if (op == '+' || op == '-') return 1;
    return -1;
}

std::string infixToPostfix(const std::string& in) {
    std::string out;
    std::stack<char> st;
    for (char ch : in) {
        if (std::isalnum(static_cast<unsigned char>(ch))) {
            out.push_back(ch);
        } else if (ch == '(') {
            st.push(ch);
        } else if (ch == ')') {
            while (!st.empty() && st.top() != '(') {
                out.push_back(st.top()); st.pop();
            }
            if (!st.empty()) st.pop(); // pop '('
        } else { // operator
            while (!st.empty() && st.top() != '(' &&
                  (precedence(st.top()) > precedence(ch) ||
                   (precedence(st.top()) == precedence(ch) && ch != '^'))) {
                out.push_back(st.top()); st.pop();
            }
            st.push(ch);
        }
    }
    while (!st.empty()) { out.push_back(st.top()); st.pop(); }
    return out;
}

int main() {
    std::string s;
    std::getline(std::cin, s);
    std::cout << infixToPostfix(s) << '\n';
    return 0;
}

Test with simple inputs like a+b*c (should produce abc*+) and (a+b)*c (should produce ab+c*). If errors persist, post the exact minimal code you compiled and the exact compiler messages so the problem can be pinpointed.

Recommended Answers

All 3 Replies

Erm what you want your program to do ?

Thats not the c++ code you are trying to run.
Its little language constructs plus a lot of algorithmic language. Hence, the syntax errors
Please read a beginner's book on c++

Cheers

robert:i wan to convert infix expression to postfix expression...
abhi:ok...thanks...

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.