1

I know this type of question has been asked before but I can't find a solution to this, I know its a invalid memory reference error or array out of bounds, but I can't seem to find the cause of the error in my code. I just tried this problem on SPOJ, it was the 'Transform the Expression' https://www.spoj.com/problems/ONP/ my all test cases are right!

here is my code:

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


int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
string str;
std::stack<char> f ;
cin>>t;
while(t--){
    cin>>str;
    string ans="";
    for(int i=0;i<str.size();i++){
        switch(str[i]){
            case '(':break;
            case '+':
            case '*':
            case '-':
            case '^':
               f.push(str[i]);
               break;
            case ')':
               ans+=f.top();
               f.pop();
               break;
            default:
               ans+=str[i];
               break;
        }

        }
        cout<<ans<<endl;
        
    }

return 0;
}
cigien
  • 57,834
  • 11
  • 73
  • 112
  • difficult to find the answer until you know the input to the code. Do you know the input by any chance ? Also, this is not a C++ language question per se but it might be due to logical error on part of the programmer so this forum is not appropriate for such questions. – Onkar N Mahajan Nov 13 '20 at 06:33
  • 1
    Most likely `top` or `pop` on an empty stack. Other avenues seem to be covered. – user4581301 Nov 13 '20 at 06:37
  • 1
    I'd argue that the root cause is that you try to learn programming and C++ through "competition" sites. Get [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and take classes to learn programming and C++ properly first, *then* use such sites for fun (but *never* as a learning or teaching resource). All you really learn on such sites are bad habits. – Some programmer dude Nov 13 '20 at 06:38
  • `string str;` and `std::stack f;` would be a bit happier if defined inside the `while (t--)` loop. Won't matter much with `str`, but `f` should be allowed to start fresh on every iteration. – user4581301 Nov 13 '20 at 06:40
  • 1
    Some times you need to be really creative with your test cases. Obviously SPOJ has tripped over something you haven't. Or maybe you have, but you weren't lucky enough to crash. – user4581301 Nov 13 '20 at 06:42
  • `#include ` along with `#include` demonstrates you don't really know what `#include ` does. [Be really careful with it.](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – user4581301 Nov 13 '20 at 06:44
  • There is no guarantee that there are parentheses, and you're not handling precedence at all. – molbdnilo Nov 13 '20 at 08:12

1 Answers1

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


int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin>>t;
while(t--){
    string str;
    std::stack<char> f ;
    cin>>str;
    string ans="";
    for(int i=0;i<str.size();i++){
        switch(str[i]){
            case '(':
                break;
            case '+':
            case '*':
            case '/': // Change 1
            case '-':
            case '^':
               f.push(str[i]);
               break;
            case ')':
            if(f.size()>0){  // Change 2
               ans+=f.top();
               f.pop();
            }
               break;
            default:
               ans+=str[i];
               break;
        }
        }
        if(f.size()>0){
            while(f.size()>0){
                ans+=f.top();
                f.pop();
            }
        }
        cout<<ans<<endl;
        
    }

return 0;
}

There were two errors in your code

  1. You forgot to add '/' (this would give WA)
  2. If the stack was empty still your code was adding it to the string and popping empty stack

(I did some changes in your code)

adrsh23
  • 210
  • 2
  • 10
  • Please don't use `#include ` in answers. It's non-portable and pretty bad style [that we shouldn't be spreading](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – user4581301 Nov 13 '20 at 15:20