evaluating postfix expression

so far my program works fine but I keep getting one error which is:
error C2440: 'return' : cannot convert from 'int [5]' to 'int'
1> There is no context in which this conversion is possible

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
using namespace std;
#include <crtdbg.h>
#include "StackHeader.h"



  class ListStack
 {
 private:
 struct node
 {

 int num;
 node *next;
 }*top;
 int operand_count;

 public:

 ListStack()
 {
 top=NULL;
 }
 int push(int);
 int pop();
 void display();
 void get_expression();
 int get_value(int);
};

int ListStack::push(int c)
 {
 node *temp;
 temp = new node;
 temp->num=c;
 temp->next=top;
 top=temp;
 return c;

 }

 int ListStack::pop()
 { int c;
 if(top==NULL)
 cout<<"Stack UnderFlow"<<endl;
 else
 {
 node *temp;
 temp=top;
 cout<<"deleted Number from the stack = ";
 c=top->num;
 top=top->next;
 return (temp->num);
 delete temp;
 return (c);
 }
 }


 void ListStack::display()
 { node*temp;
 temp=top;
 while(temp!=NULL)
 {
 cout<<"\n"<<temp->num<<endl;
 temp=temp->next;
 }
 }

 int ListStack::get_value(int c)
 { int operand_count;
 cout<<"how many number of operands you have?";
 cin>>operand_count;
 int numeric_array[5];
 int i;

 for (i=0;i<=operand_count;i++)
 {
 cout<<"Enter Value: ";
 cin>>numeric_array[i];
 return numeric_array;
 }
 }

 void ListStack::get_expression()
 {
 char save;
 int i=0;
 int first_operand, second_operand, result;
 char postfix_array[50], No_operator[50];
 cout<<"Enter the numeric Postfix expression: ";
 cin.getline(postfix_array,50);

 while (postfix_array[i]!='\0')
 save=postfix_array[i];
 if (save!= '+' && save!= '-' && save!= '*' &&save!= '/' &&save!= '^')
 {

 for(i=0; i<=50; i++)
 {
 cout<<"\n ENter operator :";
 cin>>No_operator[i];
 get_value( No_operator[i]);
 }

 while (No_operator[i]!='\0')

 push( No_operator[i]);

 }
 else
 first_operand = pop();
 second_operand = pop();
 switch(save)
 {
 case '+':
 result=first_operand + second_operand;
 cout<<result;
 push(result);
 break;

 case '-':
 result=first_operand - second_operand;
 cout<<result;
 push(result);
 break;

 case '*':
 result=first_operand * second_operand;
 cout<<result;
 push(result);
 break;

 case '/':
 result=first_operand / second_operand;
 cout<<result;
 push(result);
 break;

 case '%':
 result=first_operand % second_operand;
 cout<<result;
 push(result);
 break;
 }
 }



int main () 
 {
ListStack LS;
 LS.get_expression();
 LS.display();


return 0;
}



I bet your compiler does mention a line-number. 75, 79, 86.

You state that the return value is an int. You attempt to return an array. If you want to return an array, then you have to pass it to the function as parameter.
1
2
3
4
5
6
7
8
9
10
11
12
int ListStack::get_value(int c)
{ 

/* bla bla */

     for (i=0;i<=operand_count;i++)
     {
     cout<<"Enter Value: ";
     cin>>numeric_array[i];
     return numeric_array; // this is your error
     }
}

you are returning an array ( which is i think illegal ) but you only have declared the return type of
get_value() as an int

I'm not sure if you want to return a value one by one, and if so, you have to use static keyword otherwise the data will just be loss.

Or i am not also sure if you want to return an array which is i think illegal but,
you can return a pointer to the array or pass the array as parameter instead
Last edited on
Thank You for your response, ive tried fixing that error and now I get these error messages:

warning C4715: 'ListStack::pop' : not all control paths return a value
warning C4715: 'ListStack::get_value' : not all control paths return a value


here is pop()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int ListStack::pop()
 { int c;
 if(top==NULL)
 cout<<"Stack UnderFlow"<<endl;
 else
 {
 node *temp;
 temp=top;
 cout<<"deleted Number from the stack = ";
 c=top->num;
 top=top->next;
 return (temp->num);
 delete temp;
 return (c);
 }
 }



and here is getValue()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int ListStack::get_value(int c)
 { int operand_count;
 cout<<"how many number of operands you have?";
 cin>>operand_count;
 int numeric_array[5];
 int i;

 for (i=0;i<=operand_count;i++)
 {
 cout<<"Enter Value: ";
 cin>>numeric_array[i];
 return numeric_array;
 }
 }
The error message is rather clear:

The pop() does not return any value if top is null. You do have a design question: how to return when there is nothing to return? One solution: http://www.cplusplus.com/reference/deque/deque/back/

get_value() has a loop. If operand_count is less than zero, nothing is returned. If operand_count is zero or more, numeric_array[0] is shown and then you attempt to return the address of a local array (which is an error too). What is your actual intention?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int ListStack::pop()
{
    int c;
    if (top==NULL)
        cout << "Stack UnderFlow"<<endl;
        return 0;           // return some integer value - doesn't have to be 0
    else
    {
        node *temp;
        temp = top;
        cout << "deleted Number from the stack = ";
        c = top->num;
        top = top->next;
        return (temp->num); // function exits here
        delete temp;        // this line is never executed
        return (c);         // this line is never executed
    }
}
so I don't need line 15 or 16?
cshu wrote:
so I don't need line 15 or 16?

That would be the wrong conclusion.

I pointed out that there were two return statements, and that code after the first one will never be executed. But that doesn't mean you should simply erase those lines. In particular, line 15, delete temp;. This delete should be matching a previous use of new in the function push()

As a general principle, each use of new should be exactly balanced by a corresponding delete, in order to avoid memory leaks.
Topic archived. No new replies allowed.