Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
  1  #include<iostream>
  2  #include<algorithm>
  3  #include<stack>
  4  #include<string>
  5  using namespace std;
  6  
  7  int priority(char c) {
  8  	if (c == '-' || c == '+')
  9  		return 1;
 10  	else if (c == '*' || c == '/')
 11  		return 2;
 12  	else if (c == '^')
 13  		return 3;
 14  	else
 15  		return 0;
 16  
 17  }
 18  
 19  string infixtopostfix(string exp) {
 20  
 21  	stack<char> stk;
 22  	string output = "";
 23  	for (int i = 0; i < exp.length(); i++) {
 24  		if (exp[i] == ' ') { continue; }
 25  		else if (isdigit(exp[i]) || isalpha(exp[i])) {
 26  			output += exp[i];
 27  		}
 28  		else if (exp[i] == '(') {
 29  			stk.push('(');
 30  		}
 31  		else if (exp[i] == ')') {
 32  			while (stk.top() != '(') {
 33  				output += stk.top();
 34  				stk.pop();
 35  			}
 36  
 37  			stk.pop();
 38  		}
 39  		else {
 40  			while (!stk.empty() && priority(exp[i]) <= (priority(stk.top()){
 41  				output += stk.top();
 42  				stk.pop()
 43  			}
 44  			stk.push(exp[i]);
 45  		}
 46  	}
 47  
 48  			while (!stk.empty()) {
 49  				output += stk.top();
 50  				stk.pop();
 51  			}
 52  
 53  			return output;
 54  		
 55  
 56  
 57  
 58  			int main() {			
 59              	string infixexpression = ("(3+2)+7/2*((7+3)*2");
 60  				cout << infixtopostfix(infixexpression) << endl; 
 61  				return 0;
 62  			}


What I have tried:

Note:Errors are bolded in the code block



Errors are as follow
expected a')' line 40
expected a statment line 45
expected a ';'line 58
syntax error ';' line 41
syntax error missing ';' before'}' line 43
syntax error 'while' line 48
syntax error :missing ';' before '{' line 48
'{' missing function header (old style list?) line 48
syntax error :'return' line 53
Posted
Updated 24-Feb-23 20:55pm
v2
Comments
jeron1 24-Feb-23 10:23am    
Generally start with the first error, go to that line and the lines near it and see if there is a syntax error. Hint, most lines require a semicolon at the end.

The first message is reasonably clear. There is a missing parenthesis so line 40 should look like this :
C++
while (!stk.empty() && priority(exp[i]) <= (priority(stk.top()) ) {
Alternatively, you can think of a parenthesis being misplaced. The one after the <= should be in front of the first priority call like this :
C++
while (!stk.empty() && ( priority(exp[i]) <= priority(stk.top() ) ) {
You also appear to be missing a closing brace prior to the int main line to terminate the infixtopostfix function.
 
Share this answer
 
v2
To add to what Rick has said, You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!
 
Share this answer
 
Usually you start with the first error and correct it. The first mistake is obviously missing brackets. Here it helps to simply count how many open and how many close.
C++
while (!stk.empty() && priority(exp[i]) <= (priority(stk.top()))) {

There are even several brackets missing. With the last three errors it comes to the misunderstanding, because a bracket is missing. Since the compiler is not sure where something is wrong, in this case uninvolved places are objected.

If you turn on the warnings, you will see other places where the compiler warns that it has tried to solve the conflict itself.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900