C语言中的数据验证-确保输入格式正确
我想写一个代码,以确保用户只输入1位数字。如果用户输入类似“0 1 3”的内容,我希望我的程序读取一条错误消息,但我不知道如何执行。有人知道怎么做吗?如果用户输入一组数字,中间有空格,我当前的代码只接收第一个数字 请看下面我的代码。谢谢:DC语言中的数据验证-确保输入格式正确,c,validation,C,Validation,我想写一个代码,以确保用户只输入1位数字。如果用户输入类似“0 1 3”的内容,我希望我的程序读取一条错误消息,但我不知道如何执行。有人知道怎么做吗?如果用户输入一组数字,中间有空格,我当前的代码只接收第一个数字 请看下面我的代码。谢谢:D //Prompt the user to enter the low radius with data validation printf("Enter the low radius [0.0..40.0]: "); do { ret = sc
//Prompt the user to enter the low radius with data validation
printf("Enter the low radius [0.0..40.0]: ");
do
{
ret = scanf("%lf", &lowRadius);
//type validation
if (ret != 1)
{
int ch = 0;
while (((ch = getchar()) != EOF) && (ch != '\n'));
printf("Wrong input. Please enter one numerical value: ");
}
//range validation
else if((lowRadius < 0 || lowRadius > 40))
{
printf("Incorrect value. Please enter in range 0-40: ");
}
else break;
} while ((ret != 1) || (lowRadius < 0 || lowRadius > 40));//end while lowRadius
//提示用户输入带有数据验证的低半径
printf(“输入低半径[0.0..40.0]:”;
做
{
ret=扫描频率(“%lf”和低半径);
//类型验证
如果(ret!=1)
{
int ch=0;
而(((ch=getchar())!=EOF)&&(ch!='\n');
printf(“输入错误。请输入一个数值:”);
}
//范围验证
否则如果((低半径<0 | |低半径>40))
{
printf(“值不正确。请输入范围为0-40:”);
}
否则就断了;
}而((ret!=1)| |(低半径<0 | |低半径>40))//低半径时结束
读取整行并用转换。读取整行并用转换。如果将该行读取为字符串,然后进行分析,则可以避免挂起不适用的输入的问题。您已经完成了大部分工作,但这说明了如何捕获过多的输入。它的工作原理是扫描double
后的字符串以拾取更多输入。sscanf
的返回值告诉您是否有,因为它返回成功扫描的项目数
#include <stdio.h>
#include <stdlib.h>
void err(char *message)
{
puts(message);
exit(1);
}
int main(void)
{
double lowRadius = 0.0;
char inp[100];
char more[2];
int conv;
if(fgets(inp, sizeof inp, stdin) == NULL) {
err("Input unsuccesful");
}
conv = sscanf(inp, "%lf %1s", &lowRadius, more); // conv is number of items scanned
if(conv != 1) {
err("One input value is required");
}
if(lowRadius < 0.0 || lowRadius > 40.0) {
err("Number out of range");
}
printf("%f\n", lowRadius);
return 0;
}
#包括
#包括
无效错误(字符*消息)
{
放置(消息);
出口(1);
}
内部主(空)
{
双低半径=0.0;
char-inp[100];
charmore[2];
int conv;
if(fgets(inp,sizeof inp,stdin)=NULL){
错误(“输入不成功”);
}
conv=sscanf(inp,“%lf%1s”&低半径,更多);//conv是扫描的项目数
如果(conv!=1){
err(“需要一个输入值”);
}
如果(低半径<0.0 | |低半径>40.0){
错误(“数量超出范围”);
}
printf(“%f\n”,低半径);
返回0;
}
我不确定您对一位数的规定,因为这不允许输入最大值。如果您将该行读入字符串,然后对其进行分析,就可以避免挂起不适用的输入的问题。您已经完成了大部分工作,但这说明了如何捕获过多的输入。它的工作原理是扫描
double
后的字符串以拾取更多输入。sscanf
的返回值告诉您是否有,因为它返回成功扫描的项目数
#include <stdio.h>
#include <stdlib.h>
void err(char *message)
{
puts(message);
exit(1);
}
int main(void)
{
double lowRadius = 0.0;
char inp[100];
char more[2];
int conv;
if(fgets(inp, sizeof inp, stdin) == NULL) {
err("Input unsuccesful");
}
conv = sscanf(inp, "%lf %1s", &lowRadius, more); // conv is number of items scanned
if(conv != 1) {
err("One input value is required");
}
if(lowRadius < 0.0 || lowRadius > 40.0) {
err("Number out of range");
}
printf("%f\n", lowRadius);
return 0;
}
#包括
#包括
无效错误(字符*消息)
{
放置(消息);
出口(1);
}
内部主(空)
{
双低半径=0.0;
char-inp[100];
charmore[2];
int conv;
if(fgets(inp,sizeof inp,stdin)=NULL){
错误(“输入不成功”);
}
conv=sscanf(inp,“%lf%1s”&低半径,更多);//conv是扫描的项目数
如果(conv!=1){
err(“需要一个输入值”);
}
如果(低半径<0.0 | |低半径>40.0){
错误(“数量超出范围”);
}
printf(“%f\n”,低半径);
返回0;
}
我不确定您对一位数的规定,因为这不允许输入最大值。Alexander的方法是正确的,但没有给出太多细节。下面是我将如何做的,使用getline()读取输入,然后使用strspn()和strtod()解析读取的输入。如果您不熟悉使用指针,这将很难理解-但如果您正在学习C,您最终会达到目的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
double lowRadius;
char *lineptr = NULL;
size_t n;
char *startptr;
char *endptr;
char *ws = " \t\n"; /* possible whitespace characters */
printf("Enter the low radius [0.0..40.0]: ");
while(1) {
/* free lineptr if set - neeeded if we iterate on error input */
if( lineptr ) {
free(lineptr);
lineptr = NULL;
}
/* now read a line of input */
while( getline(&lineptr, &n, stdin) == -1 ) {
/* error returned, just retry */
continue;
}
/* skip over any leading whitespace */
startptr = lineptr + strspn(lineptr,ws);
/* Now try to convert double */
lowRadius = strtod(startptr, &endptr);
if( endptr==startptr || endptr[strspn(endptr,ws)] != 0 ) {
/* either no characters were processed - e.g., the
line was empty, or there was some non-whitespace
character found after the number. */
printf( "Wrong input. Please enter one numerical value: ");
} else if( (lowRadius < 0.0) || (lowRadius > 40.0) ) {
printf( "Incorrect value. Please enter in range 0-40: " );
} else {
if( lineptr ) free(lineptr);
break;
}
}
printf( "value entered was %lf\n", lowRadius );
}
#包括
#包括
#包括
内部主(空){
双低半径;
char*lineptr=NULL;
尺寸;
char*startptr;
char*endptr;
char*ws=“\t\n”/*可能的空白字符*/
printf(“输入低半径[0.0..40.0]:”;
而(1){
/*free lineptr if set-如果我们在错误输入上迭代,则需要*/
if(lineptr){
免费(lineptr);
lineptr=NULL;
}
/*现在读一行输入*/
while(getline(&lineptr,&n,stdin)=-1){
/*返回错误,请重试*/
继续;
}
/*跳过任何前导空格*/
startptr=lineptr+strspn(lineptr,ws);
/*现在尝试转换为双精度*/
低半径=strtod(开始和结束);
如果(endptr==startptr | | endptr[strspn(endptr,ws)]!=0){
/*未处理任何字符-例如
行为空,或者有一些非空白
在数字后找到字符*/
printf(“输入错误。请输入一个数值:”);
}如果((低半径<0.0)| |(低半径>40.0)){
printf(“值不正确。请输入范围为0-40:”);
}否则{
如果(lineptr)空闲(lineptr);
打破
}
}
printf(“输入的值为%lf\n”,低半径);
}
Alexander有正确的方法,但没有给出太多细节。下面是我将如何做的,使用getline()读取输入,然后使用strspn()和strtod()解析读取的输入。如果您不熟悉使用指针,这将很难理解-但如果您正在学习C,您最终会达到目的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
double lowRadius;
char *lineptr = NULL;
size_t n;
char *startptr;
char *endptr;
char *ws = " \t\n"; /* possible whitespace characters */
printf("Enter the low radius [0.0..40.0]: ");
while(1) {
/* free lineptr if set - neeeded if we iterate on error input */
if( lineptr ) {
free(lineptr);
lineptr = NULL;
}
/* now read a line of input */
while( getline(&lineptr, &n, stdin) == -1 ) {
/* error returned, just retry */
continue;
}
/* skip over any leading whitespace */
startptr = lineptr + strspn(lineptr,ws);
/* Now try to convert double */
lowRadius = strtod(startptr, &endptr);
if( endptr==startptr || endptr[strspn(endptr,ws)] != 0 ) {
/* either no characters were processed - e.g., the
line was empty, or there was some non-whitespace
character found after the number. */
printf( "Wrong input. Please enter one numerical value: ");
} else if( (lowRadius < 0.0) || (lowRadius > 40.0) ) {
printf( "Incorrect value. Please enter in range 0-40: " );
} else {
if( lineptr ) free(lineptr);
break;
}
}
printf( "value entered was %lf\n", lowRadius );
}
#包括
#包括
#包括
内部主(空){
双低半径;
char*lineptr=NULL;
尺寸;
char*startptr;
char*endptr;
char*ws=“\t\n”/*可能的空白字符*/
printf(“输入低半径[0.0..40.0]:”;
而(1){
/*free lineptr if set-如果我们在错误输入上迭代,则需要*/
if(lineptr){
免费(lineptr);
lineptr=NULL;
}
/*现在读一行输入*/
while(getline(&lineptr,&n,stdin)=-1){
/*返回错误,请重试*/
继续;
}
/*跳过任何前导空格*/
startptr=lineptr+strspn(lineptr,ws);
/*现在尝试转换为双精度*/
低半径=strto