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;
}