Search code examples
cfunctionreturnvoid

My void function returns a value and does not return to the main function.


I was writing a program that asks the pilot to enter coordinates. Then, use those coordinates later on in other functions such as calculating the distance and the angle of the plane

this is my main function:

    int main()
{
    plane_checker();
    double angle_finder(int x, int y);
    double distance_plane(int x, int y, int z);
    void ils_conditions();
}

where my plane_checker() function is:

plane_checker()
{
    printf("Please enter your identification code:");
    scanf("%s", &plane_name[0]);

    if( (plane_name[0]== 'j') || (plane_name[0]== 'f') || (plane_name[0]== 'm') || (plane_name[0]== 'J') || (plane_name[0]== 'F') || (plane_name[0]== 'M'))
    {
        printf("Sorry, we are not authorized to support military air vehicles.");;
    }
    else
    {
        printf("Please enter your current coordinates in x y z form:");
        scanf("%d %d %d", &x, &y, &z);

        if(z < 0)
        {
            printf("Sorry. Invalid coordinates.");
        }

    }
    return;
}

after the user inputs the coordinates, I expect the program to return to the main function and continue with the other functions. However, when I run the program, my function returns the inputted z value and ends the program. As seen here:

Please enter your identification code:lmkng
Please enter your current coordinates in x y z form:1 2 2

Process returned 2 (0x2)   execution time : 12.063 s
Press any key to continue.

What could be the cause of this? I checked my program word by word but could not find the reason behind this? What am I missing?

Thank you a lot in advance!


Solution

  • if you dont want your function to return anything define it like this

    void plane_checker()
    {
        printf("Please enter your identification code:");
        scanf("%s", &plane_name[0]);
    
        if( (plane_name[0]== 'j') || (plane_name[0]== 'f') || (plane_name[0]== 'm') || (plane_name[0]== 'J') || (plane_name[0]== 'F') || (plane_name[0]== 'M'))
        {
            printf("Sorry, we are not authorized to support military air vehicles.");;
        }
        else
        {
            printf("Please enter your current coordinates in x y z form:");
            scanf("%d %d %d", &x, &y, &z);
    
            if(z < 0)
            {
                printf("Sorry. Invalid coordinates.");
            }
    
        }
    
    }
    

    however you wont be able to manipulate the inserted data outside plane_checker function. You should return inserted data from your plane_checker() or use pointers. https://www.tutorialspoint.com/cprogramming/c_pointers.htm