如何处理用户输入错误
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) );
}
好的,上面的代码差不多完成了,它询问用户向量的数量,然后它询问用户这些向量的值,然后它会计算长度并相应地打印出来。
我想在这里进行一些错误检查,但我似乎无法得到它...我查找了 fgets 和 sscanf 的每个可能的 return 值我似乎无法得到它
防御特征
FIRST printf------输入应该只是一个大于 0 的数字,EOF 应该 return 像 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 )
但它不会检查这些不正确的输入!!
我要疯了
以下代码:
- 编译干净
- 显示了几个错误检查示例
发布的代码引发了一个编译器警告,即在没有 return 语句的情况下到达非空函数的末尾(警告需要更正)。
要查看所有警告,请在为 gcc
编译时启用所有警告,使用参数:-Wall -Wextra -Wpedantic
顺便说一句:正确声明结构的荣誉
#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
#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 的每个可能的 return 值我似乎无法得到它
防御特征
FIRST printf------输入应该只是一个大于 0 的数字,EOF 应该 return 像 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 )
但它不会检查这些不正确的输入!!
我要疯了
以下代码:
- 编译干净
- 显示了几个错误检查示例
发布的代码引发了一个编译器警告,即在没有 return 语句的情况下到达非空函数的末尾(警告需要更正)。
要查看所有警告,请在为 gcc
编译时启用所有警告,使用参数:-Wall -Wextra -Wpedantic
顺便说一句:正确声明结构的荣誉
#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