char**array, printing only the last evaluated RPN expression

I made a program to print reverse polish notation of expressions using stacks
the problem is that my code prints only the last expression converted, i inserted output statements in between and yeah, it's converting every expression correctly but for some reason i don't understand when i display the outputs of test cases, it only shows the output of last
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int t; // no. of test cases
    cin >> t;
    string a; // input expression
    string b;// output string where RPN expression is stored
    
    char **array; 
    int m=0;
    int j=0;
    array = new char*[t];
    while (t--){
          m = 0;
          cin >> a;
                        
          stack<char, vector<int> > stack1;
          for ( int i = 0; i<a.length(); i++)
          {
              if ( a[i] != '(' &&  a[i] != ')' &&  a[i] != '*' && a[i] != '+' && a[i] != '-' && a[i] != '/' && a[i] !='^')
              { b[m++] = a[i]; }
              else{
                   if(a[i] == '*' || a[i] == '+' || a[i] == '-' || a[i] == '/' || a[i] =='^')
                           stack1.push(a[i]);
                   else if( a[i] == ')')
                   {b[m++]= char(stack1.top());
                   stack1.pop();}
               }
          }                     
          
          
          *(array+j) = &b[0];
          j++;} 
          int    i = 0;
    while(j--)
              cout << array[i++]<<endl; 
you should post the entire program but the last line smells "bad".

array is a pointer to pointer

you are writing array[i++] which basically means dereferencing the content at the address address of array + i++, but since array is not just a pointer, but a pointer to pointer, the result is that from that dereferencing you probably just got a pointer, and the std::cout stream is interpreting that pointer as a pointer to a string and printing only 1 string, the one pointed by array + i++.

but, again you should post your entire code.
Here's the complete code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

 
 
int main()
{
    int t;
    cin >> t;
    string a;
    string b;
    
    char **array;
    int m=0;
    int j=0;
    array = new char*[t];
    while (t--){
          m = 0;
          cin >> a;
                        
          stack<char, vector<int> > stack1;
          for ( int i = 0; i<a.length(); i++)
          {
              if ( a[i] != '(' &&  a[i] != ')' &&  a[i] != '*' && a[i] != '+' && a[i] != '-' && a[i] != '/' && a[i] !='^')
              { b[m++] = a[i]; }
              else{
                   if(a[i] == '*' || a[i] == '+' || a[i] == '-' || a[i] == '/' || a[i] =='^')
                           stack1.push(a[i]);
                   else if( a[i] == ')')
                   {b[m++]= char(stack1.top());
                   stack1.pop();}
               }
          }                     
          
          
          *(array+j) = &b[0];
          j++;} 
          int    i = 0;
    while(j--)
              cout << array[i++]<<endl;         

}
Last edited on
Topic archived. No new replies allowed.