Q:

How to handle user input errors

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

struct vector
{
    double x;
    double y;
    double z;
};

struct vector *array;
double length(struct vector*);

int main()
{
    int num,i;
    double xin;
    double yin;
    double zin;
    char buffer[30];
    char buffer2[30];

    printf("Enter number of vectors:");
    fgets(buffer, 30, stdin);
    sscanf(buffer, "%d", &num);

    array = malloc( sizeof(struct vector) * num);

    for(i=0;i<=num;i++)
    {
        printf("Please enter x y z for the vector:");
        fgets(buffer2,100,stdin);
        sscanf(buffer2, " %lf %lf %lf", &xin, &yin, &zin);

            array[i].x = xin;
            array[i].y = yin;
            array[i].z = zin;
    }

    for(i=0;i<=num;i++)
    {
        printf( "Vector:%lf %lf %lf has a length of %lf\n", array[i].x, array[i].y, array[i].z, length(&array[i]));
    }
}

double length(struct vector* vec)
{
    return sqrt( (vec->x * vec->x) + (vec->y * vec->y) + (vec->z * vec->z) );
}

Ok the above code is almost finished it asks user for number of vectors then it asks user for the values of those vectors it then will calculate the length and print it out correspondingly.
I am trying to get some error checking in here but I cannot seem to get it...I looked up every possible return value for fgets and sscanf I just cant seem to get it
Defensive features
FIRST printf-------input should only be a single number greater than 0 and EOF should return a message like printf("enter a number--bye!") so I tried
while( sscanf(buffer, "%d", &num) ==1 && num > 0 )

but it still works if something like 3dadswerudsad is entered
also when the user is entering the 3 values for the vector if anything at all other than 3 doubles are entered for a vector the program should terminate with a message so I tried
while( sscanf(buffer2, "%lf %lf %lf", &xin, &yin, &zin) ==3 )

but it doesn't check for these incorrect inputs!!
I am going crazy

A:

The following code:

compiles cleanly
shows several examples of error checking

The posted code raised a compiler warning about reaching end of non-void function without a return statement (warnings need to be corrected).
To see all the warnings, enable all the warnings when compiling for gcc, use parameters: -Wall -Wextra -Wpedantic
BTW: kudos for properly declaring the struct
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct vector
{
    double x;
    double y;
    double z;
};

struct vector *array;

// prototypes
double length(struct vector*);

int main()
{
    int num,i;
    double xin;
    double yin;
    double zin;
    char buffer[30];
    char buffer2[30];

    printf("Enter number of vectors:");
    if( NULL == fgets(buffer, 30, stdin) )
    { // then fgets failed
        perror( "fgets for num vectors failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fgets successful

    if( 1 != sscanf(buffer, " %d", &num) )
    { // then sscanf failed
        perror( "sscanf for num Vectors failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, sscanf failed

    // add validation of num here

    if( NULL == (array = malloc( sizeof(struct vector) * num) ))
    { // then, malloc failed
        perror( "malloc for num Vectors failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    for(i=0;i<=num;i++)
    {
        printf("Please enter x y z for the vector:");
        if( NULL == fgets(buffer2,100,stdin) )
        { // then fgets failed
            perror( "fgets for vector values failed" );
            free(array); // cleanup
            exit( EXIT_FAILURE );
        }

        // implied else, fgets successful

        if( 3 != sscanf(buffer2, " %lf %lf %lf", &xin, &yin, &zin) )
        { // then sscanf failed
            perror( "sscanf for vector values failed" );
           free(array); // cleanup
            exit( EXIT_FAILURE );
        }

        // implied else, sscanf successful

        array[i].x = xin;
        array[i].y = yin;
        array[i].z = zin;
    } // end for

    for(i=0;i<=num;i++)
    {
        printf( "Vector:%lf %lf %lf has a length of %lf\n",
                array[i].x,
                array[i].y,
                array[i].z,
                length(&array[i]));
    } // end for

    free(array); // cleanup

    return(0);
} // end function: main

