-2

I don't what is wrong with this code it should answer -4 but the answer i'm getting is 2492

#include <bits/stdc++.h>
using namespace std;

int main() {
    stack <int> st;

   char s[]="231*+9-" ;

    for(int i=0;i<7;i++){

        if (isdigit(s[i])){
            st.push((int)s[i]);

        }
        else{
            float val1,val2;
            val1=st.top();
            st.pop();
            val2=st.top();
            st.pop();
           switch (s[i])
            {
             case '+': st.push( val2 + val1); break;
             case '-': st.push(val2 - val1); break;
             case '*': st.push( val2 * val1); break;
             case '/': st.push( val2/val1);   break;

            }
        }

    }int m=st.top();
    cout<<m;
    return 0;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Skpuri
  • 3
  • 2
  • 2
    Unrelated to your problem, but please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Jun 27 '17 at 08:23
  • 2
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should [edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Jun 27 '17 at 08:23
  • As for your problem, please learn how to use a debugger. With a debugger you can step through the code line by line while monitoring variables and their values. Being able to use a debugger is a crucial skill for all programmers. And probably study an [ASCII table](http://en.cppreference.com/w/cpp/language/ascii) too, because the character `'1'` is not equal to the integer `1`. – Some programmer dude Jun 27 '17 at 08:24
  • You've a debugger, use it... You'll be surprised about how clear are your errors if you discover them by yourself. It's not because I don't want to help you.. It's really a very useful thing to do... – Jepessen Jun 27 '17 at 08:24
  • 2
    `char` values won't magically convert to `float`. – πάντα ῥεῖ Jun 27 '17 at 08:25
  • 3
    `#include ` followed by `using namespace std;`. Your teacher should be fired and you all have your money refunded. – StoryTeller - Unslander Monica Jun 27 '17 at 08:29
  • 1
    There's a big difference between digits (characters) and numbers. Try printing `'1' + '1'`. – molbdnilo Jun 27 '17 at 08:29

2 Answers2

1

You cannot cast a numeric char directly to int if you want its digit value, because that would just give its ASCII code. Instead, do st.push((int)(s[i]-'0')). This removes the offset of the 0-9 character group.

meowgoesthedog
  • 14,670
  • 4
  • 27
  • 40
  • Note that despite the assertion that digits are encoded in ASCII, the code in the answer correctly solves the problem **without** assuming any particular character encoding. – Pete Becker Jun 27 '17 at 12:06
  • @PeteBecker yes you are correct; I only wrote ASCII because the OP used `char` – meowgoesthedog Jun 27 '17 at 12:16
  • `char` holds values. Using it does not imply ASCII, nor does using a character constant like `'0'`. ASCII is one of many character encodings. It happens to be the most common one, but it is not the only one. `'0'` Is represented a numeric value that depends on the character encoding that the compiler chooses. In ASCII, `'0'` is represented by the value 0x30; in EBCDIC it is represented by the value 0xF0. In all cases, though, subtracting `'0'` from a digit character 0-9 is required to give the corresponding numeric value. Sorry about this digression... – Pete Becker Jun 27 '17 at 12:32
  • @PeteBecker not at all, very interesting background, thank you – meowgoesthedog Jun 27 '17 at 12:35
0

One easy way is to replace char s[]="231*+9- by a string string s ="231*+9-" and then create an alias of size_t, so you can push the char as a substr like this:

std::string::size_type sz;
...
st.push(stoi(s.substr(i,1), &sz));


mugo
  • 1