warning: control reaches end of non-void function [-Wreturn-type] }

Hi, i'm working on an infix to RPN program for my CS2 class. I'm sure there are still a few things that need to be worked out in my main while loop, but i can't see the problem in my out put because i get a "warning: control reaches end of non-void function [-Wreturn-type]" about my int priority() function and i dont really understand what it means. any help would be appreciated if. i run the .exe without fixing it i get a infinite loop telling me the stack is full, which i am sure means something but i dont know what. thanks for any help you can give.

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
/*
***Program Goals***
1. convert infix expression to RPN.
2. Read input file into a string /done
3. use stack to convert input string into RPN output string
4. Separte push pop functions /done
5. create function to determine priority of the operators.
6. output both infix and RPN strings into output file.  /infix done 

***Algortithm***
1. Function for create stack. /done
2. functions to determine empty and full stack /done
3. functions for push and pop /done
4. Read data from input file /done
5. convert infix to rpn.
6. function to determine priority of operators // may be involved in rpn conversion, ask for clarification.
7. output infix and RPN strings to Program3RPN.ot
8. main function to call createStack and readEm functions. (personal goal keep main function as clean and short as possible.)
9. E.C. include parentheses in conversion, and add extra date to input file to be read in.
*/

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <ctype.h>

using namespace std;

const int maxstack = 20;
const char initstack = 'z';



void createStack(char stack[], int &top)
{
    int i;
    top = -1;
    for (i = 0; i < maxstack; i++)
        stack[i] = initstack;
}

bool fullStack(int top)
{
    return top >= maxstack - 1;
}

bool emptyStack(int top)
{
    return top < 0;
}

void push(char stack[], int &top, char ch)
{
    if (!fullStack(top))
    {
        top++;
        stack[top] = ch;
    }
    else
        cout << "The stack is full, can't push" << endl;
}

char pop(char stack[], int &top)
{
    char ch = initstack;
    if (!emptyStack(top))
    {
        ch = stack[top];
        stack[top] = initstack;
        top--;
    }
    return ch;
}

int priority(char ch)
{
    switch (ch)
    {
    case '+':
        return 1;
        break;
    case '-':
        return 1;
        break;
    case '*':
        return 5;
        break;
    case '/':
        return 5;
        break;
    }
}

void readEm(char stack[], int &top, char ch)
{
    string inString, outString;
    int i, len;
    ifstream inf("Program3RPN.dat");
    ofstream outf("Program3RPN.ot");

    while (!inf.eof())
    {
        inf >> inString;
        ch = inString[i];
        outString += ch;
        len = inString.length();
        for (i = 0; i < len; i++)
        {
                ch = inString[i];
            if(ch == isalpha(i))
            
                outString += ch;
            else if(emptyStack(top))
                push(stack, top, ch);
            
            else if (priority(ch)== priority(stack[top]))
                push(stack, top, ch);
                    
            else if(emptyStack(top))
                push(stack, top, ch);
                
            else if(priority(ch) >= priority(stack[top]))
                push (stack, top, ch);
        }
    }
    while (!emptyStack(top) && priority(ch) <= priority(stack[top]))
        {
            push(stack, top, ch);
        }    
    cout << inString << " " << outString << endl;
    outf << inString << " " << outString << endl;
    //cout << inString << " " << outString << endl;
}

int main()
{
    char ch;
    char stack[maxstack];
    int top;
    createStack(stack, top);
    readEm(stack, top, ch);
    system("pause");
    return 0;
}
Last edited on
What happenes if I call priority( 'a' )?
i dont think priority('a') could happen because isalpha() is addressed earlier in the loop? it should only effect my operators not my operands right? i dont know if i understand what your asking.
TheJast, the compiler gives warnings like that one simply on a per-function basis.

It may be true that it's impossible due to external logic for priority( 'a' ) to be called, but all the compiler sees is that there is a function where not all code paths return a value. Not returning a value from a non-void function is illegal.

If you want the warning to go away, the easiest thing would be to add a default: option to your switch statement, and return some dummy value (or perhaps throw an exception).

___________________________________

The more important issue:
i run the .exe without fixing it i get a infinite loop telling me the stack is full,
I didn't run your code, but it looks like your while loop on line 127 is looping while NOT empty stack. And you call push repeatedly on the same char.
... did you mean to pop here? I'm not sure, but you never call pop anywhere.
Last edited on
thank you, i still dont understand how to create a case in my switch that encompasses all other options besides the operator cases presented. However i see what you mean about not using pop. that fixed a lot. I really appreciate it.
i still dont understand how to create a case in my switch that encompasses all other options
1
2
3
4
5
6
7
8
9
10
11
char ch = 'w'; // etc.
switch (ch)
{
    case 'a':
        // ...
        break;
    case 'b':
        break;
    default:
       break;
}

The 'default' case is what encompasses all other options not listed.
Topic archived. No new replies allowed.