I hit a little barrier while trying to implement an RPN calculator today. I want to have expressions like this in a file:
But that could be any number of numbers and I have no idea how to read them in.
Code:
int main (int argc, char *argv[])
{
FILE *f = 0;
int ch = 0;
Stack *stk = new Stack (256);
f = fopen ("z:\\test.rpn", "r");
if (! f) {
cout << "Error opening file\n";
return 1;
}
while ((ch = fgetc (f)) != EOF) { // The offending line
switch (ch) {
case '+': {
char one, two, ans;
one = stk->pop ();
two = stk->pop ();
ans = one + two;
stk->push (ans);
break;
}
case '-':
break;
case '/':
break;
case '*':
break;
default: // assumed to be a literal to push
stk->push (ch);
}
}
fclose (f);
delete stk;
return 0;
}
I don't have a clue how to read in an unknown number of numbers, as the title suggests.