0

I've googled and searched my problems and maybe I'm not using the correct search terms because I'm not finding any solutions to my problem.

What I'm trying to do is do a postfix calculator expression using the push and pop methods and templates.

Per my professor's words

Input

The input is a string that represents a postfix expression. To simplify the problem, your calculator will be restricted to operating on single-digit non-negative integers. Only the following characters are allowed in the string:

the operators '+', '-'. '*', and '/' the digits '0' through '9' the space (blank) character ' '

Example input: 2 4 3 * +

Result: 14

To handle spaces in the input, you will need to make one minor addition to the algorithm on page 204 of your textbook:

 if (ch is a blank) ignore it end if 

The program is separated into 3 files, the main file, the template, and the virtual template. He wants as little as possible in the main file, most of the checking and calculating done in the template.

#include <iostream> #include <string> #include "ArrayStack.h" using namespace std; int main() { ArrayStack<string> stack; string items; bool done = false; char decision; while(!done) // check to see if the user wants to do more calculations { cout << "Enter postfix expression: "; getline(cin, items); for(int x = 0;x < items.length();x++) { stack.push(&items[x]); cout << "Pushing " << &items[x] << endl; } cout << "Done? Y/N: "; cin >> decision; if((decision == 'y') || (decision == 'Y')) done = true; } } 

and here's the relative function of the template

template<class ItemType> bool ArrayStack<ItemType>::push(const ItemType& newEntry) { bool result = false; if (top < MAX_STACK - 1) // Does stack have room for newEntry? { ////////////////////////////////////// if(isdigit(&newEntry)) { top++; items[top] = newEntry; result = true; } ////////////////////////////////////// } // end if return result; } // end push 

My problem is that isdigit can't do anything when it's a constant char, but I'm not allowed to check to see if it's a digit, space or /-+* in the main file. I'm not sure how to proceed. Am I using the wrong function, or am I just not using it properly?

5
  • 1
    isdigit() ain't a template, You can't use it except on integers, char is also an integer type of 8-bit size, I suggest reading the parameters next time. Commented Apr 26, 2018 at 0:31
  • 1
    If you're getting a compile error, copy/paste the entire error into your question, don't paraphrase. Commented Apr 26, 2018 at 0:32
  • Kira, I thought you were allowed to use functions in templates. Is that not the case? I get the same error with isalnum or isspace either way. When trying to cast it into an int, I get an also either way. It refused to work. Commented Apr 26, 2018 at 0:39
  • 1
    isdigit() accepts an int. It can't simply be passed an argument of arbitrary type i.e. ItemType needs to be an integral type (or one that can be converted to int). Commented Apr 26, 2018 at 0:52
  • "int isdigit( int ch );" is Defined in header <cctype> ... which you did not. < cctype > also defines both 'isalnum' and 'isspace'. Commented Apr 26, 2018 at 1:46

1 Answer 1

2

you might want to change the type to unsigned char instead.

ArrayStack<unsigned char> stack; 

For the isdigit() works with unsigned char.

Also, push unsigned char items in your loop and not a pointer:

stack.push(items[x]); // don't pass a pointer 

UPDATE

As per isdigit() in cppreference:

The behavior is undefined if the value of ch is not representable as unsigned char and is not equal to EOF.

which is different in cplusplus.com. So take note.

Thus, your ItemType should be unsigned char

Sign up to request clarification or add additional context in comments.

4 Comments

Note that technically, isdigit() works with unsigned chars, and giving it a char is undefined behavior. See en.cppreference.com/w/cpp/string/byte/isdigit Also, cplusplus.com has been known to contain incorrect information, including the failure to mention this in the page that you linked to.
std::isdigit (from <cctype> ) works with ints. The OP's code fails to include < cctype >.
@DOUGLASO.MOEN, true it works with ints. but just for the context that OP thought of iterating a string would probably mean OP is thinking about characters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.