0

I am getting a "Function should return a value" error at the 91st line of the code in Turbo C++, please help me as I have to submit my project, I know that Turbo C++ is a very old compiler but that's what our University Teacher recommends so I cant do nothing in that

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

#include <stdio.h>
#include <conio.h>

struct stack
{
    int element;
    struct stack *next;
} * top;

void push(int);
int pop();
void display();

void main()
{
    int num1, num2, choice;

    while (1)
    {
        clrscr();
        printf("Select a choice from the following:");
        printf("\n[1] Push an element into the stack");
        printf("\n[2] Pop out an element from the stack");
        printf("\n[3] Display the stack elements");
        printf("\n[4] Exit\n");
        printf("\n\tYour choice: ");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
        {
            printf("\n\tEnter the element to be pushed into the stack: ");
            scanf("%d", &num1);
            push(num1);
            break;
        }

        case 2:
        {
            num2 = pop();
            printf("\n\t%d element popped out of the stack\n\t", num2);
            getch();
            break;
        }

        case 3:
        {
            display();
            getch();
            break;
        }

        case 4:
            exit(1);
            break;

        default:
            printf("\nInvalid choice !\n");
            break;
        }
    }
}

void push(int value)
{
    struct stack *ptr;
    ptr = (struct stack *)malloc(sizeof(struct stack));

    ptr->element = value;

    ptr->next = top;
    top = ptr;
    return;
}

int pop()
{
    if (top == NULL)
    {
        printf("\n\STACK is Empty.");
        getch();
        exit(1);
    }
    else
    {
        int temp = top->element;
        top = top->next;
        return (temp);
    }
}

void display()
{
    struct stack *ptr1 = NULL;
    ptr1 = top;
    printf("\nThe various stack elements are:\n");
    while (ptr1 != NULL)
    {
        printf("%d\t", ptr1->element);
        ptr1 = ptr1->next;
    }
}

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Ansh Tyagi
  • 11
  • 4
  • line 91 , I see `printf("\n\STACK is Empty.");` if that's the case remove \ after `\n` , it should be `printf("\n STACK is Empty.");`, also did you include `#include ` ? – IrAM Nov 29 '20 at 12:06
  • 'void push(int value) { struct stack *ptr; ptr = (struct stack *)malloc(sizeof(struct stack)); ptr->element = value; ptr->next = top; top = ptr; return; }' the program is showing error here at the bracket after return, no i didn't include '#include ' – Ansh Tyagi Nov 29 '20 at 12:20
  • Line 91 is the end of the function `pop()` which *does* return a value under all circumstances, so it is a false warning. Try removing `else` because there is no `else` alternative after the previous `exit(1);` The first comment refers to code at line 81. – Weather Vane Nov 29 '20 at 12:23
  • not related to the error , but there is no need to use `return;` in `push` function – IrAM Nov 29 '20 at 12:31
  • Thanks Weather Vane it worked, but i want to know that after removing 'else' will the code in bracket { int temp = top->element; top = top->next; return (temp); } will work or not? or is it useless so should i delete it – Ansh Tyagi Nov 29 '20 at 12:31
  • The `if` code block ends with `exit(1)`. So when the `if` path is followed the code execution does not reach `else` anyway. So just remove the `else { }` wrapper round that code. – Weather Vane Nov 29 '20 at 12:33
  • after deleting else and all the content between { }, it is showing the same error again, Weather Vane – Ansh Tyagi Nov 29 '20 at 12:41
  • Sorry, if you have another question please post a new question. – Weather Vane Nov 29 '20 at 12:41
  • after deleting else and all the content between { }, it is showing the same error again, Weather Vane – Ansh Tyagi Nov 29 '20 at 12:46

2 Answers2

0

you can change your pop function as below ( assuming you are not storing -1 as an element in the stack)

int pop()
{
    if (top == NULL)
    {
        printf("\n\STACK is Empty.");
        getch();
        return -1;// or other invalid value which indicates stack empty
    }
    else
    {
        int temp = top->element;
        top = top->next;
        return (temp);
    }
}

and at the place you are calling modify as following

case 2:
{
    num2 = pop();
    if(num2 != -1) {
        printf("\n\t%d element popped out of the stack\n\t", num2);
        getch();
    }else{
        printf("Stack is Empty\n");
        exit(1);
    }
    break;
}

IrAM
  • 1,720
  • 5
  • 18
  • I don't want to change the code too much, please made some modifications in the same problem only – Ansh Tyagi Nov 29 '20 at 13:42
  • this is not too much code , you should handle the error scenarios correctly and you have not yet handled memory leaks in your code. – IrAM Nov 29 '20 at 13:53
0

The compiler is complaining because you don’t have a return statement outside of the if statement. Even though you call exit in the if branch, syntactically speaking that’s just another function call; structurally, the compiler sees a pathway where you reach the closing } of the function body without a return statement.

You want to make sure the return is reachable outside the body of the if-else statement, and the best way to do it is take the else branch out of the statement entirely:

int pop( void )
{
  int temp;

  if ( !top )
  { 
    fputs( "Stack is empty", stderr );
    exit( 1 );
  }

  temp = top->element;
  top = top->next;
  return temp;
}
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • at int temp = top->element; it is showing "Declaration is not allowed here" – Ansh Tyagi Nov 29 '20 at 13:35
  • declare the `int temp;` at the beginning of the function and use only `temp = top->element;` after `if` – IrAM Nov 29 '20 at 13:49
  • @AnshTyagi: That’s right, you’re using an ancient implementation that doesn’t support mixed declarations and code. You’ll need to declare `int temp;` at the beginning of the function and change the assignment to `temp = top->element;`. Sorry about that. – John Bode Nov 29 '20 at 13:50
  • john bode, please send the code with the necessary changes, I don't know where i should declare int temp; – Ansh Tyagi Nov 29 '20 at 14:03