当用户输入无效输入时,任何人都知道如何编写错误语句。 [英] Anybody know how I would code an error statement when a user inputs an invalid input.

查看:57
本文介绍了当用户输入无效输入时,任何人都知道如何编写错误语句。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户输入浮点值。 scanf()函数获取值。

但是,我需要使用if else语句创建一个错误处理程序

如果输入不是数字则表示无效输入。有谁知道

我怎么能这样做?

A user inputs a float value. The scanf() function gets the value.
However, I need to create an error handler with an if else statement
saying invalid input if the input is not a number. Does anybody know
how I could do this?

推荐答案

jeff regoord写道:
jeff regoord wrote:

用户输入浮点值。 scanf()函数获取值。
但是,我需要使用if else语句创建一个错误处理程序
如果输入不是数字则表示无效输入。有谁知道
我怎么能这样做?

A user inputs a float value. The scanf() function gets the value.
However, I need to create an error handler with an if else statement
saying invalid input if the input is not a number. Does anybody know
how I could do this?




/ * BEGIN new.c * /


#包括< stdio.h>

#include< stdlib.h>

#include< ctype.h>


#define STRINGLENGTH 1000

#define str(x)#x

#define xstr(x)str(x)


int main(void)

{

char string [STRINGLENGTH + 1] = {''\ 0''};

int rc;


fputs("输入浮点值:",stdout);

fflush(stdout);

rc = scanf("%" xstr(STRINGLENGTH)" [^ \ n]%* [^ \ n]",string);

getchar();

while(rc){

双倍数;

char * endptr;


number = strtod( string,& endptr);

if(errno){

puts(" Value out of range。);

errno = 0;

}否则if(* endptr&&!isspace((int)* endptr)){

放(对不起戴夫。我担心我不能这样做。\ n");

}其他{

printf("数字是%f \ n) ;,数字);

}

fputs("输入浮点值:",stdout);

fflush(stdout) ;

rc = scanf("%" xstr(STRINGLENGTH)" [^ \ n]%* [^ \ n]",string);

getchar();

}

返回0;

}


/ * END new.c * /



/* BEGIN new.c */

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

#define STRINGLENGTH 1000
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
char string[STRINGLENGTH + 1] = {''\0''};
int rc;

fputs("Enter a floating point value: ", stdout);
fflush(stdout);
rc = scanf("%" xstr(STRINGLENGTH) "[^\n]%*[^\n]", string);
getchar();
while (rc) {
double number;
char *endptr;

number = strtod(string, &endptr);
if (errno) {
puts("Value out of range.");
errno = 0;
} else if (*endptr && !isspace((int)*endptr)) {
puts("\nI''m sorry Dave. I''m afraid I can''t do that.\n");
} else {
printf("The number is %f\n", number);
}
fputs("Enter a floating point value: ", stdout);
fflush(stdout);
rc = scanf("%" xstr(STRINGLENGTH) "[^\n]%*[^\n]", string);
getchar();
}
return 0;
}

/* END new.c */




2003年9月10日星期三,jeff regoord写道:

On Wed, 10 Sep 2003, jeff regoord wrote:

用户输入浮点值。 scanf()函数获取值。
但是,我需要使用if else语句创建一个错误处理程序
如果输入不是数字则表示无效输入。有谁知道我怎么能这样做?

A user inputs a float value. The scanf() function gets the value.
However, I need to create an error handler with an if else statement
saying invalid input if the input is not a number. Does anybody know
how I could do this?




试着更具体一点。在最基本的层面上,你可以写出


int ret;

...


input_the_number:

ret = scanf("%d",& number);


if(ret!= 1){

printf(这不是一个数字;再试一次!\ n);

转到input_the_number;

}

if(number < 0){

printf(那是否定的;再试一次!\ n);

转到input_the_number;

}

...

(或者等同于更先进的控制流结构的东西,但是我很难/ b $ b太累了,无法找到一切都会在''while''循环中进行。)


在更高级别,错误处理的概念是一个非常好的

有趣的一个。我自己是以下小功能的粉丝:

#include< stdio.h>

#include< stdlib.h>

#include< stdarg.h>


静态字符* Argv0;


void do_error(const char * fmat,...) ;


int main(int argc,char * argv [])

{

Argv0 = argv [0];


if(0 == 1){

do_error(哦,亲爱的我:%d%d%d \ n,0,1, 0 == 1);

}


返回0;

}


/ **这是有趣的一点。 * /


void do_error(const char * fmat,...)

{

va_list ap;

printf("%s:",Argv0);

va_start(ap,fmat);

vprintf(fmat,ap);

va_end(ap);

退出(EXIT_FAILURE);

}

万一你无法弄清楚从代码中,do_error函数

采用可变数量的参数,就像''printf'',但在打印它们之前,它会尝试打印出来的名字执行程序,

然后,一旦打印完所有内容,它就会直接终止程序

。这适用于那些不可能恢复的人。各种情况,

可能适用于你的问题也可能不适用。


你可能知道,与C ++不同,C确实*不支持异常处理

语言水平。 (任何人都有一个很好的投掷/捕获的成语

标准C?:)


HTH,

-Arthur



Try to be more specific. At the most basic level, you could write

int ret;
...

input_the_number:
ret = scanf("%d", &number);

if (ret != 1) {
printf("That was not a number; try again!\n");
goto input_the_number;
}
if (number < 0) {
printf("That was negative; try again!\n");
goto input_the_number;
}
...
(Or equivalent stuff with more advanced control-flow structures, but I''m
too tired to work out where everything would go in a ''while'' loop.)

At a more advanced level, the concept of "error handling" is a pretty
interesting one. I myself am a fan of the following little function:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

static char *Argv0;

void do_error(const char *fmat, ...);

int main(int argc, char *argv[])
{
Argv0 = argv[0];

if (0 == 1) {
do_error("Oh dearie me: %d %d %d\n", 0, 1, 0==1);
}

return 0;
}

/** This is the interesting bit. */

void do_error(const char *fmat, ...)
{
va_list ap;
printf("%s: ", Argv0);
va_start(ap, fmat);
vprintf(fmat, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
In case you can''t figure it out from the code, the do_error function
takes a variable number of arguments, just like ''printf'', but before
printing them, it tries to print the name of the executing program,
and then, once everything is printed, it terminates the program
directly. This is for those "no recovery possible" sorts of situations,
which may or may not apply to your problem.

As you may know, and unlike C++, C does *not* support exception handling
at the language level. (Anyone got a nice throw/catch sort of idiom for
standard C? :)

HTH,
-Arthur


Arthur J. O'Dwyer< aj*@andrew.cmu.edu>写道:

[...]
Arthur J. O''Dwyer <aj*@andrew.cmu.edu> wrote:
[...]
正如您所知,并且与C ++不同,C在语言级别上不支持异常处理。 (对于
标准C,任何人都有一个很好的投掷/捕捉成语?:)
As you may know, and unlike C++, C does *not* support exception handling
at the language level. (Anyone got a nice throw/catch sort of idiom for
standard C? :)




如果你想调查一下这可能是一个好的开始区域......


#include< stdio.h>

#include< stdlib.h>

#include < setjmp.h>


struct ehandler {jmp_buf env; } $

struct ehandler next_up_handler;


#define INIT_EXCEPTIONS \

if(setjmp(next_up_handler.env) )){\

fprintf(stderr,Uncaught exception,terminating.\ n); \

退出(EXIT_FAILURE); \

}


#define I_CATCH_EXCEPTIONS volatile int caught;


#define try {\

volatile struct ehandler saved_handler = next_up_handler; \

if(!(caught = setjmp(next_up_handler.env)))


#define catch \

next_up_handler = saved_handler; \

} if(抓住)\


#define throw longjmp(next_up_handler.env,1)


/ *抛出异常版本的malloc * /

void * emalloc(size_t s)

{

void * r = malloc( s);

if(!r)

throw;

return r;

}


int bar()

{

throw;


返回0;

}


int foo()

{

I_CATCH_EXCEPTIONS

void * volatile j;


j = emalloc(100);

尝试{

bar();

} catch {

printf(foo抓住异常,清理...... \ n);

免费(j);

throw;

}


printf(" foo正常函数执行\ nn);

免费(j) ;


返回0;

}


int main()

{

I_CATCH_EXCEPTIONS


INIT_EXCEPTIONS


试试{

foo();

} catch {

fprintf(stderr,main catch a exception,exiting\\\
);

返回EXIT_FAILURE;

}


printf(程序正常完成。\ n);


返回0;

}


显然,如果你想做更多这类事情

生产质量你必须使用一致的命名空间来确定

标识符和那种东西 - 也许你会想要允许

多种异常也好。


- 凯文。



This might be a good start if you wanted to look into this area...

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

struct ehandler { jmp_buf env; };

struct ehandler next_up_handler;

#define INIT_EXCEPTIONS \
if (setjmp(next_up_handler.env)) { \
fprintf(stderr, "Uncaught exception, terminating.\n"); \
exit(EXIT_FAILURE); \
}

#define I_CATCH_EXCEPTIONS volatile int caught;

#define try { \
volatile struct ehandler saved_handler = next_up_handler; \
if (!(caught = setjmp(next_up_handler.env)))

#define catch \
next_up_handler = saved_handler; \
} if (caught) \

#define throw longjmp(next_up_handler.env, 1)

/* Exception-throwing version of malloc */
void *emalloc(size_t s)
{
void *r = malloc(s);
if (!r)
throw;
return r;
}

int bar()
{
throw;

return 0;
}

int foo()
{
I_CATCH_EXCEPTIONS
void * volatile j;

j = emalloc(100);
try {
bar();
} catch {
printf("foo caught exception, cleaning up...\n");
free(j);
throw;
}

printf("foo Normal function execution\n");
free(j);

return 0;
}

int main()
{
I_CATCH_EXCEPTIONS

INIT_EXCEPTIONS

try {
foo();
} catch {
fprintf(stderr, "main caught an exception, exiting\n");
return EXIT_FAILURE;
}

printf("program completed normally.\n");

return 0;
}

Obviously if you wanted to make this sort of thing more
production-quality you''d have to use a consistent namespace for
identifiers and that sort of thing - perhaps you''d want to allow
multiple kinds of exceptions too.

- Kevin.


这篇关于当用户输入无效输入时,任何人都知道如何编写错误语句。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆