Home > Back-end >  Program is not returning to main function
Program is not returning to main function

Time:11-13

#include <stdio.h>
#include<math.h>

int taskchoice, a;

void Menu() {
    printf("What would you like to do: \n Fish = 1\n Hunt = 2 \n Cook = 3\n Boss = 4\n");
    scanf("%d", &a);
    
    if (a == 1) {
        Fishing();
    }
        
    if (a == 2) {
        printf("Hunting\n");
    }
        
    if (a == 3) {
        printf("Cooking\n");
    }

    if (a == 4) {
        printf("Bossing\n");
    }
    else {
       abort();
    }
}

void Fishing() {
    printf("Time to fish me lord?\n");
    return;
}
    
int main() {
    Menu();
    
    printf("Would you like to go back to the menu?");

    //my fishing function is working as intended but the question is, why does it not display
    //anything that is posted after the menu(); in the main function
    //I am new to C and thank you for taking the time to look at my question. 
}

CodePudding user response:

The issue is, there are multiple occurrences of if statement, and the last if-else statement is what makes decides the returning path.

So instead of having multiple if, go with if else-if, because it is sure that the user will input a single int value.

Use the following in place of Menu() function:

void Menu() {
    printf("What would you like to do: \n Fish = 1\n Hunt = 2 \n Cook = 3\n Boss = 4\n");
    scanf("%d", &a);
    
    if (a == 1) {
        Fishing();
    }
        
    else if (a == 2) {
        printf("Hunting\n");
    }
        
    else if (a == 3) {
        printf("Cooking\n");
    }

    else if (a == 4) {
        printf("Bossing\n");
    }
    else {
       abort();
    }
}

CodePudding user response:

Add return value on main since you have a datatype for your main function.

int main(){
  return 0;
}

If not change int main to void main.

Lastly remove void on your finish function. Since the function does not return anything. It returns back to main and ends the main function.

  • Related