如何处理用户输入错误

时间:2015-01-09 14:27:27

标签: c data-structures error-handling structure

#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) );
}

好的,上面的代码差不多完了,它会询问用户的矢量数量,然后它会询问用户这些矢量的值,然后计算长度并相应地打印出来。

我想在这里得到一些错误检查,但我似乎无法得到它...我查找fgets和sscanf的每个可能的返回值我似乎无法得到它

防守功能

FIRST printf -------输入应该只是一个大于0的数字,EOF应该返回printf("enter a number--bye!")之类的消息,所以我试过

while( sscanf(buffer, "%d", &num) ==1 && num > 0 )

但是如果输入了像3dadswerudsad这样的东西它仍然有用

当用户输入向量的3个值时,如果为向量输入了除3个双精度以外的任何值,程序应该以消息终止,所以我试过

while( sscanf(buffer2, "%lf %lf %lf", &xin, &yin, &zin) ==3 )

但它没有检查这些不正确的输入!!

我要发疯了

1 个答案:

答案 0 :(得分:0)

以下代码:

  • 干净利落地编译
  • 显示了几个错误检查示例

发布的代码在没有返回语句的情况下引发了关于到达非void函数结束的编译器警告(警告需要更正)。
要查看所有警告,请在编译gcc时启用所有警告,请使用参数:-Wall -Wextra -Wpedantic

BTW:赞成正确声明结构

#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