错误使用C处理?如何处理scanf函数和非法字符? [英] Error Handling in C? How to deal with scanf and illegal characters?
问题描述
我用C写一个程序,错误处理它会告诉你去寻找非法字符,当你scanf函数-ING。换句话说,查找的字符不是一个整数。
它应该显示相应的错误消息并终止程序。
I'm writing a program in C, and for error handling it tells you to look for an illegal character when you're scanf-ing. In other words, look for a character that isn't an integer. It is supposed to display an appropriate error message and terminate the program.
我,我怎么去寻找非法字符,或者注意到,它不是一个整数,有点困惑。任何帮助吗?
I'm a little confused as to how I go about looking for that illegal character, or noticing that it isn't an integer. Any help?
推荐答案
scanf()的
返回成功参数的个数。如果你这样做:
scanf()
returns the number of successful arguments. If you do:
int ivar, return_val;
return_val = scanf("%i", &ivar);
RETURN_VAL
应该是1,1参数的原因(伊娃
)。检查用户输入:
return_val
should be 1, cause of 1 parameter (ivar
). Check the user input:
if (return_val == 1) {
// right input
} else {
// wrong input
}
这篇关于错误使用C处理?如何处理scanf函数和非法字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!