Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++. Getting an error when looping again. This program is for a postfix calculator. It takes a postfix expression and returns the result. It also

C++. Getting an error when looping again.

This program is for a postfix calculator. It takes a postfix expression and returns the result. It also asks the user if they want to enter another expression. I'm having difficulty after doing the first input, for the program to keep looping correctly.

Here is what it is supposed to look like:

 Enter postfix expression: 2 3 4 + * The value of the expression is 14 More expressions (Y or N)? Y Enter postfix expression: 7 2 / The value of the expression is 3 More expressions (Y or N)? N 

Here is my output:

image text in transcribed

Here is the code:

stack_driver.cpp

#include

#include

#include

#include "ArrayStack.hpp"

#include "StackInterface.hpp"

using namespace std;

//Function to check postfix expression

int checkPostfix(string expression);

//Function to check if character is a digit

bool digit(char ch);

//Function to check if character is operand

bool operand(char ch);

//Function to evaluate expression and return output

int evaluateExpression(char operation, int operand1, int operand2);

int main()

{

//Declare string variable

string expression;

//Loop again variable

char again;

do

{

//Get string from user

cout

getline(cin, expression);

//Check operators, digits, and spaces

int result = checkPostfix(expression);

//Display result

cout

//Ask user if they want to enter another expression

cout

cin >> again;

}

while (again == 'Y' || again == 'y');

} // end driver

//Function to check postfix expression

int checkPostfix(string expression)

{

//Declare stack

stack Stack;

for(int i = 0; i

{

//Look at each character

if(expression[i] == ' ' || expression[i] == ',') continue;

//If operand, push the value of the operand ch onto the stack

else if(operand(expression[i]))

{

int operand2 = Stack.top(); Stack.pop();

int operand1 = Stack.top(); Stack.pop();

//EvaluateExpression

int result = evaluateExpression(expression[i], operand1, operand2);

//Push result onn top of stack

Stack.push(result);

}

else if((digit)(expression[i]))

{

//Get digits from the string

int operand = 0;

while(i

{

//Going left to right, multiply current total by 10 and add the new digit

operand = (operand * 10) + (expression[i] - '0');

i++;

}

//Deincrement i becaise it will be re-incremented again

i--;

//Push operand onto stack

Stack.push(operand);

}

}

//Output of result

return Stack.top();

}

//Function to check if character is a digit

bool digit(char ch)

{

//Return true for numbers 0 through 9

if(ch >= '0' && ch

{

return true;

}

//Else return false

else

{

return false;

}

}

//Function to check if character is operand

bool operand(char ch)

{

// Return true for +, -, *, and /

if(ch == '+' || ch == '-' || ch == '*' || ch == '/')

{

return true;

}

//Else return false

{

return false;

}

}

//Function to evaluate expression and return output

int evaluateExpression(char operation, int operand1, int operand2)

{

//Add operation

if(operation == '+')

{

return operand1 + operand2;

}

//Subtract operation

else if(operation == '-')

{

return operand1 - operand2;

}

//Multply operation

else if(operation == '*')

{

return operand1 * operand2;

}

//Division operation

else if(operation == '/')

{

return operand1 / operand2;

}

//Exception

cout

return -1;

}

Enter postfix expression: The value if the expression is: 5 More expressions? (Y or N) Enter postfix expression: (1ldb) 2 3 + error: '2' is not a valid command error: Unrecognized command '2' Program ended with exit code: 9

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions