又一个用户输入问题 [英] Yet another User Input Question

查看:80
本文介绍了又一个用户输入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图限制不。面向行的用户中的列

输入。任何人都可以指出

这种方法的潜在缺陷?

btw ..

1.我没有使用动态内存分配,因为那不是
i想要实现的东西。

2.我想我的输入行是面向的。

3.我需要的最大列是80

4.程序中有一个无限循环:)

5.我有*没试过到目前为止验证数据。

看起来好的用户输入例程非常令人头疼!

#include< stdio.h>

#include< stdlib.h>

#include< string.h>

#define BUFFSIZE 80 +(2)

#define MAXTRY 5 / *错误允许的最大尝试次数..使用

稍后* /


int flushln(FILE * f){

int ch;

while((''\ n''!=(ch = getchar()))&&(EOF!= ch))

继续;

返回ch;

}


/ *

输入是面向行的..每行以换行符结尾

输入函数将消息作为参数打印

返回指向缓冲区的指针或NULL,如果出现错误,可以在main()中处理


* /


char * input(char * message)

{

static error_no = 0; / *需要改进* /

静态字符缓冲区[BUFFSIZE];

char * p =& buffer [BUFFSIZE-1];

int flag = -1;


printf("%s \ n",message);

memset(buffer,-1,BUFFSIZE );

if(fgets(缓冲区,BUFFSIZE,stdin)== NULL){

puts(输入错误);

error_no ++; / *供以后使用* /

返回NULL;

}

else {

if((* p == -1)||((* p ==''\''')&&(* - p ==''\ n'')))

flag = 1; / *在缓冲区范围内输入* /


if(flag == -1){/ *流中有一些垃圾* /

flushln(标准输入); / *从流中取出垃圾* /

puts(别试试亲爱的!);

error_no ++; / *以后使用* /

返回NULL;

}

}

返回缓冲区;

}


int main(无效)

{

char * iVar;

while((iVar = input(" Message:"))== NULL)/ *需要更改* /

继续;

返回0;

}

I have tried to restrict the no. of columns in a line oriented user
input.Can anyone please point out
potential flaws in this method?
btw..
1.I have not used dynamic memory allocation because thats not something
i want to implement.
2.I suppose my input line oriented .
3.The maximum columns i need is 80
4.There is an infinite loop in the program :)
5.I have *not* tried to validate the data as yet.
It looks like a good user input routine is a big headache!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFFSIZE 80 +(2)
#define MAXTRY 5 /*Maximum no of tries allowed on error ..use it
later on*/

int flushln(FILE *f) {
int ch;
while ((''\n'' != (ch = getchar( ))) && (EOF != ch))
continue;
return ch;
}

/*
Input is line oriented..every line ends with a newline character
Input function takes the message to be printed as a parameter
Returns a pointer to the buffer or NULL in case of error which can be
handled in main ( )
*/

char *input(char *message)
{
static error_no = 0; /*to be improved upon*/
static char buffer[ BUFFSIZE ];
char *p = &buffer[ BUFFSIZE-1 ];
int flag = -1;

printf("%s \n", message);
memset(buffer,-1,BUFFSIZE);
if( fgets(buffer,BUFFSIZE,stdin) == NULL){
puts("Input Error");
error_no ++; /*for later use*/
return NULL;
}
else{
if((*p == -1) || ( (*p == ''\0'') && (*--p == ''\n'') ))
flag = 1; /*Input within buffer range*/

if(flag == -1){ /*There is some garbage in the stream*/
flushln(stdin); /*Pull off the garbage from the stream*/
puts("Dont try it honey !");
error_no ++; /*Later use*/
return NULL;
}
}
return buffer;
}

int main(void)
{
char *iVar;
while( (iVar = input("Message :")) == NULL) /*Needs changes*/
continue;
return 0;
}

推荐答案

Tarique说:
Tarique said:

我试图限制不。面向行的用户中的列

输入。任何人都可以指出

这种方法的潜在缺陷?
I have tried to restrict the no. of columns in a line oriented user
input.Can anyone please point out
potential flaws in this method?



它比平均水平要好得多 - 事实上,这是非常好的,而且你已经很明显地放了b $ b有人想到了。但我确实有几个

的观察结果。


< snip>

It''s much better than average - in fact, it''s pretty good, and you''ve
obviously put some thought into it. But I do have a couple of
observations.

<snip>


char *输入(字符*消息)

{

静态error_no = 0; / *需要改进* /

静态字符缓冲区[BUFFSIZE];

char * p =& buffer [BUFFSIZE-1];

int flag = -1;


printf("%s \ n",message);

memset(buffer,-1,BUFFSIZE );

if(fgets(缓冲区,BUFFSIZE,stdin)== NULL){

puts(输入错误);

error_no ++; / *供以后使用* /

返回NULL;
char *input(char *message)
{
static error_no = 0; /*to be improved upon*/
static char buffer[ BUFFSIZE ];
char *p = &buffer[ BUFFSIZE-1 ];
int flag = -1;

printf("%s \n", message);
memset(buffer,-1,BUFFSIZE);
if( fgets(buffer,BUFFSIZE,stdin) == NULL){
puts("Input Error");
error_no ++; /*for later use*/
return NULL;



如果fgets产生NULL,可能是因为已达到输入结束。

(如果用户知道,这可以交互发生如果输入是从文件重定向的,那么如何,并且肯定会在某些时候发生。)

If fgets yields NULL, it may be because the end of input as been reached.
(This can happen interactively if the user knows how, and will certainly
happen at some point if the input is redirected from a file.)


}

else {

if((* p == -1)||((* p ==''\ 0'')&&(* - p ==' '\ n'')))

flag = 1; / *在缓冲区范围内输入* /


if(flag == -1){/ *流中有一些垃圾* /

flushln(标准输入); / *从流中取出垃圾* /
}
else{
if((*p == -1) || ( (*p == ''\0'') && (*--p == ''\n'') ))
flag = 1; /*Input within buffer range*/

if(flag == -1){ /*There is some garbage in the stream*/
flushln(stdin); /*Pull off the garbage from the stream*/



检查flushln是否返回EOF可能是一个好主意(见上文

评论re fgets)。

It''s probably a good idea to check whether flushln returned EOF (see above
comment re fgets).


puts(不要试试亲爱的!);

error_no ++; / *以后使用* /

返回NULL;

}

}

返回缓冲区;

}


int main(无效)

{

char * iVar;

while((iVar = input(" Message:"))== NULL)/ *需要更改* /

继续;
puts("Dont try it honey !");
error_no ++; /*Later use*/
return NULL;
}
}
return buffer;
}

int main(void)
{
char *iVar;
while( (iVar = input("Message :")) == NULL) /*Needs changes*/
continue;



我认为您需要区分您的例外情况。你

显然认为过长的输入是错误的。那是一个。流上的错误也可能是
(如果

发生,则ferror(stdin)将产生非零)。当然你可以点击文件的末尾(在这种情况下

feof(stdin)将产生非零)。这是三个非常不同的

条件。第一个很容易恢复 - 你可以要求用户提供更短的输入,而不是这样的berk。 :-)第二,然而,

更有问题,并且没有标准的方法从这样的

中恢复错误。第三个可能只是意味着你的程序的工作是完整的b
。显然,这些都是非常不同的,应该以不同的方式处理
,但目前它们都被NULL返回所覆盖。


另外,请记住你依赖于input()中的静态缓冲区 -

这很好,它不是那个内存会消失......但它是

表示您一次只能指向一个输入。如果你需要

商店信息,你需要预留一些存储空间来复制

吧。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

I think you need to distinguish between your exception conditions. You
clearly consider a too-long input to be erroneous. That''s one. An error on
the stream is also possible (ferror(stdin) will yield non-zero if that
happens). And of course you could hit the end of the file (in which case
feof(stdin) will yield non-zero). These are three very different
conditions. The first is easily recoverable - you can ask the user to
provide shorter input and not to be such a berk. :-) The second, however,
is more problematical, and there is no standard way to recover from such
an error. The third probably just means that your program''s work is
complete. Clearly, these are all very different, and should be treated
differently, but at present they are all covered by a NULL return.

Also, remember that you''re relying on a static buffer within input() -
which is fine, it''s not as if that memory is going to vanish... but it
does mean that you can only point to one input at once. If you need to
store information, you''ll need to reserve some storage into which to copy
it.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Tarique< pe ***** @ yahoo.comwrites:
Tarique <pe*****@yahoo.comwrites:

我试图限制不。面向行的用户中的列

输入。任何人都可以指出

这种方法的潜在缺陷?

btw ..

1.我没有使用动态内存分配,因为那不是我想要实现的东西。

2.我想我的输入行是面向的。 />
3.我需要的最大列是80

4.程序中有一个无限循环:)

5.我有*不*尝试验证数据。

看起来好的用户输入例程非常头疼!


#include< stdio.h>

#include< stdlib.h>

#include< string.h>

#define BUFFSIZE 80 +(2)
I have tried to restrict the no. of columns in a line oriented user
input.Can anyone please point out
potential flaws in this method?
btw..
1.I have not used dynamic memory allocation because thats not
something i want to implement.
2.I suppose my input line oriented .
3.The maximum columns i need is 80
4.There is an infinite loop in the program :)
5.I have *not* tried to validate the data as yet.
It looks like a good user input routine is a big headache!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BUFFSIZE 80 +(2)



更安全写(80 + 2)

safer to write (80 + 2)


#define MAXTRY 5 / *错误允许的最大尝试次数..use

稍后* /


int flushln(FILE * f){

int ch;

while((''\ n''!= =(ch = getchar()))&& (EOF!= ch))
#define MAXTRY 5 /*Maximum no of tries allowed on error ..use
it later on*/

int flushln(FILE *f) {
int ch;
while ((''\n'' != (ch = getchar( ))) && (EOF != ch))



你不能使用f参数!

You don''t use the f parameter!


继续;

返回ch;

}


/ *

输入是面向行的..每个行以换行符结尾

输入函数将消息作为参数打印

返回指向缓冲区的指针或NULL,如果出现错误,可以是

在main处理()

* /


char * input(char * message)

{

static error_no = 0; / *需要改进* /

静态字符缓冲区[BUFFSIZE];

char * p =& buffer [BUFFSIZE-1];

int flag = -1;


printf("%s \ n",message);

memset(buffer,-1,BUFFSIZE );
continue;
return ch;
}

/*
Input is line oriented..every line ends with a newline character
Input function takes the message to be printed as a parameter
Returns a pointer to the buffer or NULL in case of error which can be
handled in main ( )
*/

char *input(char *message)
{
static error_no = 0; /*to be improved upon*/
static char buffer[ BUFFSIZE ];
char *p = &buffer[ BUFFSIZE-1 ];
int flag = -1;

printf("%s \n", message);
memset(buffer,-1,BUFFSIZE);



你不需要设置整个缓冲区,-1不是一个好选择

无论如何。将最后一个位置设置为''\ n''。确保fgets

永远不要把''\ n''放在那个位置。如果它仍然存在,那么读数就是

短,你没事。如果它是0,你需要查看之前的

字符是否是''\ n''(你有一整行)或不是(你需要删除一些
$ b来自该行的$ b更多输入。

You don''t need to set the whole buffer and -1 is not a good choice
anyway. Set just the last position to ''\n''. fgets is guaranteed
never to put ''\n'' in that position. If it is still there the read was
short and you are OK. If it is 0, you need to see if the previous
char is ''\n'' (you got a whole line) or not (you need to remove some
more input from the line).


if(fgets(buffer,BUFFSIZE,stdin)== NULL){

puts( 输入错误);

error_no ++; / *供以后使用* /

返回NULL;

}

else {

if((* p == -1)||((* p ==''\''')&&(* - p ==''\ n'')))

flag = 1; / *在缓冲区范围内输入* /


if(flag == -1){/ *流中有一些垃圾* /
if( fgets(buffer,BUFFSIZE,stdin) == NULL){
puts("Input Error");
error_no ++; /*for later use*/
return NULL;
}
else{
if((*p == -1) || ( (*p == ''\0'') && (*--p == ''\n'') ))
flag = 1; /*Input within buffer range*/

if(flag == -1){ /*There is some garbage in the stream*/



这个标志毫无意义。下面执行的代码是先前的

测试失败 - 只是否定它!

This flag is pointless. The code below is executed is the previous
test fails -- just negate it!


flushln(stdin); / *从流中取出垃圾* /

puts(别试试亲爱的!);

error_no ++; / *以后使用* /

返回NULL;

}

}

返回缓冲区;

}
flushln(stdin); /*Pull off the garbage from the stream*/
puts("Dont try it honey !");
error_no ++; /*Later use*/
return NULL;
}
}
return buffer;
}



-

Ben。

--
Ben.


Tarique写道:
Tarique wrote:

我试图限制no。面向行的用户中的列

输入。任何人都可以指出

这种方法的潜在缺陷?
I have tried to restrict the no. of columns in a line oriented user
input.Can anyone please point out
potential flaws in this method?



我只是指出一些小的风格点,因为Richard Heathfield和Ben Bacarisse已经解决了严重的问题。

[...]

I''m just pointing out some minor style points, as the serious issues were
already addressed by Richard Heathfield and Ben Bacarisse.
[...]


char * input(char * message)
char *input(char *message)



你可以将参数声明为const char * message,因为你永远不会通过它写出

You could declare the parameter as const char *message, as you never write
through it.


{

static error_no = 0; / *要改进* /
{
static error_no = 0; /*to be improved upon*/



更好`static int error_no = 0;`,它仍然适用于C99。

[。 ..]

Better `static int error_no = 0;`, which still works in C99.
[...]


printf("%s \ n",message);
printf("%s \n", message);



没有理由在那里留空,对吗? (最后的尾随空白
文本流中一行的
甚至不能保证保留。)

只需使用`puts(message);`。 ..

[...]

There is no reason for a blank there, right? (Trailing blanks at the end
of a line in a text stream aren''t even guaranteed to be retained.)
Just use `puts(message);`...
[...]


if((* p == -1)||((* p ==' '\0'')&&(* - p ==''\ n'')))
if((*p == -1) || ( (*p == ''\0'') && (*--p == ''\n'') ))



递减p没有意义,只需使用p [-1]。

No point in decrementing p, just use p[-1].


flag = 1; / *在缓冲区范围内输入* /
flag = 1; /*Input within buffer range*/



1和-1是该用途的奇怪值,如果它只能有两个值

使用0和1更典型,这样你就可以写`if(flag)`。

1 and -1 are strange values for that use, if it can only have two values
it is more typical to use 0 and 1, so that you can just write `if (flag) `.


if(flag == -1){/ *流中有一些垃圾* /
if(flag == -1){ /*There is some garbage in the stream*/



[...]

-

Army1987(将NOSPAM替换为电子邮件)

[...]
--
Army1987 (Replace "NOSPAM" with "email")


这篇关于又一个用户输入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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