-2

I'm writing a code to evaluate a prefix expression. The values of the expression are separated by spaces. So if the input is "+ * 87 89 666", I should get 8409 as the answer. The concept of my code is to store the values to an array and then evaluate them value by value. Right now I'm stuck at the switch part because the compiler says invalid conversion from char to const char*

#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
#include <stack>
#include <string>
#include <sstream>

using namespace std;

char n[99999][6]={};

int evaluatePrefix(int l)
{
    stack<int> Stack;

    for (int j = l; j >= 0; j--) {
        string x=n[j];
        if (n[j][0]!='+' || n[j][0]!='-' || n[j][0]!='*' || n[j][0]!='/'){
            stringstream ss;
            int a;
            ss<<x;
            ss>>a;
            Stack.push(a);
        }
        else {
            int o1 = Stack.top();
            Stack.pop();
            int o2 = Stack.top();
            Stack.pop();
            if (strcmp(n[j], '+')==0){
                Stack.push(o1 + o2);
            }
            else if (strcmp(x, '-')==0){
                Stack.push(o1 - o2);
            }
            else if (strcmp(x, '*')==0){
                Stack.push(o1 * o2);
            }
            else if (strcmp(x, '/')==0){
                Stack.push(o1 / o2);
            }
        }
    }

    return Stack.top();
}

int main()
{
    char e[99999], w[99999];
    int i=0;
    scanf("%[^\n]%*c",e);
    char *token = strtok(e, " ");
    while (token != NULL)
    {
        strcpy(n[i], token);
        token = strtok(NULL, " ");
    }
    return 0;
}
Adrenno
  • 23
  • 1
  • 2
    Which C++ textbook taught you to use `bits/stdc++.h`? There is no such standard header file in C++, it's a compiler-specific non-standard header. Whichever C++ textbook this came from should be thrown away and replaced [with a better C++ textbook](https://stackoverflow.com/questions/388242/). Looks like the same textbook also doesn't correctly explain why declaring large arrays in function is also undesirable, for technical reasons. If this is something your teacher or instructor showed you -- you have an incompetent instructor, unfortunately. – Sam Varshavchik Mar 19 '22 at 02:41
  • 1
    I recommend watching https://www.youtube.com/watch?v=YnWhqhNdYyk or have your instructor watch it to understand why you need a better/newer c++ textbook. – Goswin von Brederlow Mar 19 '22 at 02:53
  • 1
    Next problem: `if (n[j][0] != ‘+’ || n[j][0] != ‘-‘)` is always true. – Pete Becker Mar 19 '22 at 02:58
  • Why mix string streams and char arrays? Why not use strings everywhere? – Etienne de Martel Mar 19 '22 at 03:07
  • see [Why should I not #include ?](https://stackoverflow.com/q/31816095/995714), [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/995714) – phuclv Mar 19 '22 at 03:55

1 Answers1

1

You wrote:

if (strcmp(n[j], '+')==0)

n[j] decays into a char*, but '+' is a single char, not a char*. strcmp needs two char pointers.

https://en.cppreference.com/w/c/string/byte/strcmp

So, you should use:

if (strcmp(n[j], "+")==0)
Jeffrey
  • 11,063
  • 1
  • 21
  • 42
  • Also pay attention to the good comments of @SamVarshavchik , even though they're not the immediate cause of the compile error – Jeffrey Mar 19 '22 at 02:44