用户输入问题 [英] User Input issue

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

问题描述

Hello Comp.Lang.C会员,


我有一个用户输入问题,我一直在试图找出最长的
。例如,让我们说你有这样的事情:


void foo()

{

int num;

printf(请输入一个数字:);

scanf("%d",& num); //是的我知道这个函数可能会抛出一些

警告一些编译器(MSVC ++ 8.0)

...

}


现在我们知道int数据类型使用4个字节的内存。让我们说

用户输入一个超过int

数据类型可以容纳的最小/最大大小的数字。这将被视为一个错误,并将导致

程序以不恰当的方式运行。更具体地说,

让我们说程序员想要一个真正的数字,这意味着他们不需要浮动点数
。如果是这种情况,我们知道通过

只是将''num''的数据类型更改为double将无法解决

问题,原因我是将在一分钟内解释。另外,通过

更改数据类型''num'''到long int也不会解决

问题。为什么? long int使用4个字节就像int一样,而double,

这不是程序员在这个实例中想要的,使用8个字节。

因此,如果有人可以指导我至于如何解决这个轻微的问题,我会非常感激。但是,现在我认为

,如果我将''num''设为double,是否可以输入''num''来表示

一个int数据类型,以便''num''将作为整数输出

没有十进制数?另外,作为旁注,我知道我可以使用

C风格的字符串或C ++字符串,然后使用stdlib标头

将字符串转换为整数,这将解决我用户输入时遇到的这个小问题,但是,我不是因为我的计算机科学而已经允许使用这种技术>
编程讲师告诉我不要使用字符串因为我们还没有

覆盖的字符串(我是编程I / Novice的大学新生

编程当然,虽然我应该进入更高级别的CS课程

但是现在这个时候是无关紧要的。)因此,我希望

一些指导我可以使用哪些函数/实现

不使用字符串,这将控制使用超大数据 -

类型;超过数据类型可容纳的最大/最小大小。


n3o

Hello Comp.Lang.C Members,

I have an issue with user input that I have been trying to figure out
for the longest. For instance, let''s say you have something like this:

void foo()
{
int num;
printf("Please enter a number: ");
scanf("%d", &num); // yes I know this function may throw a
warning with some compilers (MSVC++ 8.0)
...
}

now we know that the int data type uses 4 bytes of memory. Let''s say
the user enters a number that exceeds the minimum/maximum size the int
data type can hold. This would be considered a bug and would cause the
program to function in an innappropriate manner. To be more specific,
let''s say that the programmer wants a real number meaning that they do
not want floating point numbers. If that is the case, we know that by
just changing the data type of ''num'' to double would not solve the
issue for a specific reason which I will explain in a minute. Also, by
changing the data type of, ''num,'' to long int would not solve the
issue either. Why? long int uses 4 bytes just like int, and double,
which is not what the programmer wants in this instance, uses 8 bytes.
Therefore, if someone could guide me as to how I can fix this minor
problem, I would really appreciate it greatly. But, now that I think
of it, if I make ''num'' a double, is it possible to type cast ''num'' to
an int data type so that the ''num'' will output as a whole number with
no decimal number? Also, just as a sidenote, I know that I can use
either a C-style string or C++ string and then use the stdlib header
to convert the string to an integer which would solve this little
problem that I have been having with user input, however, I am not
allowed to use such a technique because my Computer Science
programming instructor told me not to use strings since we have not
covered strings yet (I''m a college freshman in a Programming I/Novice
Programming course, although I should be in a higher level CS course
but that''s irrelavent at this moment in time.) Therefore, I would like
some guidance as to what functions/implementations I could use that
DOES NOT use strings, that would control the use of oversizing a data-
type; exceeding the maximum/minimum size that a data type can hold.

n3o

推荐答案

n3o写道:
n3o wrote:

Hello Comp.Lang.C会员,


我有一个用户输入有问题我有一直试图找出最长的
。例如,让我们说你有这样的事情:


void foo()

{

int num;

printf(请输入一个数字:);
Hello Comp.Lang.C Members,

I have an issue with user input that I have been trying to figure out
for the longest. For instance, let''s say you have something like this:

void foo()
{
int num;
printf("Please enter a number: ");



除非你用换行符终止字符串,否则输出可能不会立即显示为
。或者你也可以在流上调用fflush。

Unless you terminate the string with a newline the output might not appear
immediately. Alternatively you can also call fflush on the stream.


scanf("%d",& num); //是的我知道这个函数可能会对某些编译器发出

警告(MSVC ++ 8.0)
scanf("%d", &num); // yes I know this function may throw a
warning with some compilers (MSVC++ 8.0)



标准不要求诊断,所以这是一个QoI问题。另外,作为
,您可以看到C ++样式注释在发布到Usenet时往往会中断。另外

某些开关下的某些编译器无法编译包含它们的代码。

The diagnostic is not required by the Standard, so it a QoI issue. Also as
you can see C++ style comments tend to break when posted to Usenet. Also
some compilers under certain switches cannot compile code containing them.


...

}
...
}



从风格上来说,我更倾向于使用return语句来结束。

Stylistically I prefer a return statement to falling of the end.


现在我们知道int数据类型使用4个字节的内存。
now we know that the int data type uses 4 bytes of memory.



否。标准未规定。一个int必须至少为16

位,但可能更多,24位,32位,64位和其他大小

常见。

No. This is not specified by the Standard. An int has to be at least of 16
bits but could be anything more, 24 bit, 32 bit, 64 bit and other sizes are
common.


让我们说

用户输入的数字超过了最小/最大尺寸int

数据类型可以保持。这将被视为一个错误,并将导致

程序以不恰当的方式运行。
Let''s say
the user enters a number that exceeds the minimum/maximum size the int
data type can hold. This would be considered a bug and would cause the
program to function in an innappropriate manner.



大概。

Presumably.


更具体一点,

let'' s说程序员想要一个真正的数字,这意味着他们不需要浮点数。
不想要浮点数。
To be more specific,
let''s say that the programmer wants a real number meaning that they do
not want floating point numbers.



嗯?

Huh?


如果是这种情况,我们知道

只是将''num''的数据类型更改为double将无法解决

问题,具体原因我将在一分钟内解释。另外,通过

更改数据类型''num'''到long int也不会解决

问题。为什么? long int使用4个字节,就像int一样,而double,

这不是程序员在这个实例中想要的,使用8个字节。
If that is the case, we know that by
just changing the data type of ''num'' to double would not solve the
issue for a specific reason which I will explain in a minute. Also, by
changing the data type of, ''num,'' to long int would not solve the
issue either. Why? long int uses 4 bytes just like int, and double,
which is not what the programmer wants in this instance, uses 8 bytes.





标准规定了关于类型大小的所有假设。 long必须至少为32位,而double可以是32到96位或更多位,具体取决于实现。


None of your assumptions regarding the size of types are mandated by the
Standard. A long has to be at least 32 bits while a double could be
anywhere from 32 to 96 or more bits depending on the implementation.


因此,如果有人可以指导我如何解决这个轻微的问题,我会非常感激。但是,现在我认为

,如果我将''num''设为double,是否可以输入''num''来表示

一个int数据类型,以便''num''将作为整数输出

没有十进制数?
Therefore, if someone could guide me as to how I can fix this minor
problem, I would really appreciate it greatly. But, now that I think
of it, if I make ''num'' a double, is it possible to type cast ''num'' to
an int data type so that the ''num'' will output as a whole number with
no decimal number?



在C中,该术语是强制转换的,你不需要这样做,因为将一个

浮动类型转换为整数类型如果

整数类型不足以保持double'的值,则可能涉及未定义的行为。只需将

加倍到最接近的整数并打印出来。

In C the term is cast and you don''t need to do that, since casting a
floating type to an integer type might involve undefined behaviour if the
integer type is not big enough to hold the double''s value. Just round the
double to the nearest whole number and print it out.


另外,作为旁注,我知道我可以使用

C风格的字符串或C ++字符串
Also, just as a sidenote, I know that I can use
either a C-style string or C++ string



编号如果您使用C ++中的任何内容,那么您的代码就变成了C ++。请参阅

comp.lang.c ++。 C和C ++之间的交互是微妙的并且涉及。

No. If you use anything from C++, then your code becomes C++. Consult on
comp.lang.c++. Interaction between C and C++ is subtle and involved.


然后使用stdlib头

将字符串转换为可以求解的整数我用户输入时遇到的这个小问题,但是,我不是因为我的计算机科学而需要使用这种技术。

编程讲师告诉我不要使用字符串,因为我们还没有

覆盖的字符串(我是编程I / Novice的大学新生

编程课程,虽然我应该是更高级别的CS课程

但是这个时刻是无关紧要的。)因此,我希望

关于什么功能/实现的一些指导我可以使用

不使用字符串,这将控制使用超大数据 -

类型;超过数据类型可容纳的最大/最小大小。
and then use the stdlib header
to convert the string to an integer which would solve this little
problem that I have been having with user input, however, I am not
allowed to use such a technique because my Computer Science
programming instructor told me not to use strings since we have not
covered strings yet (I''m a college freshman in a Programming I/Novice
Programming course, although I should be in a higher level CS course
but that''s irrelavent at this moment in time.) Therefore, I would like
some guidance as to what functions/implementations I could use that
DOES NOT use strings, that would control the use of oversizing a data-
type; exceeding the maximum/minimum size that a data type can hold.



如果你不能使用

字符串,scanf系列函数是你唯一的选择。如果scanf读取的值对于有问题的对象来说太大了,那么行为是未定义的。通常你会得到垃圾值。


一个更强大的解决方案是将值作为字符串读入fgets和

将其转换为strtol或strtod转换。这些函数表明它们何时无法转换价值。不幸的是,显然你被禁止使用它们。


我的建议是使用long,unsigned long,long long,unsigned中的任何一个

long long,double或long double,具体取决于您的确切需求和
可用性,并在

提示输入时通知用户最大可能值。对于非交互式输入,没有办法,鉴于你的

限制,防止溢出或环绕。这些类型的范围

可以在limits.h和float.h中找到

The scanf family of functions are your only choice if you cannot use
strings. If the value read by scanf is too big for the object in question
then the behaviour is undefined. Usually you''ll get garbage values.

A more robust solution is to read in the value as a string with fgets and
convert it with strtol or strtod. These functions indicate when they are
unable to convert the value. Unfortunately you are apparently prohibited
from using them.

My advice is to use either one of long, unsigned long, long long, unsigned
long long, double or long double, depending on your exact needs and
availability and inform the user of the maximum possible value when
prompting for input. For non-interactive input there is no way, given your
restrictions, against overflow or wrap-around. The ranges of these types
are found in limits.h and float.h


10月2日,下午1:06 ,santosh< santosh .... @ gmail.comwrote:
On Oct 2, 1:06 pm, santosh <santosh....@gmail.comwrote:

n3o写道:
n3o wrote:

你好Comp.Lang.C成员,
Hello Comp.Lang.C Members,


我有一个用户输入问题,我一直想弄清楚

最长的。例如,让我们说你有这样的东西:
I have an issue with user input that I have been trying to figure out
for the longest. For instance, let''s say you have something like this:


void foo()

{

int num;

printf(" Please enter a number:");
void foo()
{
int num;
printf("Please enter a number: ");



除非你用换行符终止字符串,否则输出可能不会立即显示为
。或者你也可以在流上调用fflush。


Unless you terminate the string with a newline the output might not appear
immediately. Alternatively you can also call fflush on the stream.


scanf("%d",& num); //是的我知道这个函数可能会对某些编译器发出

警告(MSVC ++ 8.0)
scanf("%d", &num); // yes I know this function may throw a
warning with some compilers (MSVC++ 8.0)



标准不要求诊断,所以这是一个QoI问题。另外,作为
,您可以看到C ++样式注释在发布到Usenet时往往会中断。另外

某些开关下的某些编译器无法编译包含它们的代码。


The diagnostic is not required by the Standard, so it a QoI issue. Also as
you can see C++ style comments tend to break when posted to Usenet. Also
some compilers under certain switches cannot compile code containing them.


...

}
...
}



从风格上来说,我更喜欢一个返回语句到结尾。


Stylistically I prefer a return statement to falling of the end.


现在我们知道int数据类型使用4个字节的内存。
now we know that the int data type uses 4 bytes of memory.



否。标准没有规定。一个int必须至少为16

位,但可能更多,24位,32位,64位和其他大小

常见。


No. This is not specified by the Standard. An int has to be at least of 16
bits but could be anything more, 24 bit, 32 bit, 64 bit and other sizes are
common.


让我们说

用户输入的数字超过了最小/最大尺寸int

数据类型可以保持。这将被视为一个错误,并将导致

程序以不恰当的方式运行。
Let''s say
the user enters a number that exceeds the minimum/maximum size the int
data type can hold. This would be considered a bug and would cause the
program to function in an innappropriate manner.



据推测。


Presumably.


更具体一点,

let'' s说程序员想要一个真正的数字,这意味着他们不需要浮点数。
不想要浮点数。
To be more specific,
let''s say that the programmer wants a real number meaning that they do
not want floating point numbers.



嗯?


Huh?


如果是这种情况,我们知道

只是将''num''的数据类型更改为double将无法解决

问题,具体原因我将在一分钟内解释。另外,通过

更改数据类型''num'''到long int也不会解决

问题。为什么? long int使用4个字节,就像int一样,而double,

这不是程序员在这个实例中想要的,使用8个字节。
If that is the case, we know that by
just changing the data type of ''num'' to double would not solve the
issue for a specific reason which I will explain in a minute. Also, by
changing the data type of, ''num,'' to long int would not solve the
issue either. Why? long int uses 4 bytes just like int, and double,
which is not what the programmer wants in this instance, uses 8 bytes.





标准规定了关于类型大小的所有假设。 long必须至少为32位,而double可以是32到96位或更多位,具体取决于实现。


None of your assumptions regarding the size of types are mandated by the
Standard. A long has to be at least 32 bits while a double could be
anywhere from 32 to 96 or more bits depending on the implementation.


因此,如果有人可以指导我如何解决这个轻微的问题,我会非常感激。但是,现在我认为

,如果我将''num''设为double,是否可以输入''num''来表示

一个int数据类型,以便''num''将作为整数输出

没有十进制数?
Therefore, if someone could guide me as to how I can fix this minor
problem, I would really appreciate it greatly. But, now that I think
of it, if I make ''num'' a double, is it possible to type cast ''num'' to
an int data type so that the ''num'' will output as a whole number with
no decimal number?



在C中,该术语是强制转换的,您不需要这样做,因为将一个

浮动类型转换为整数类型如果

整数类型不足以保持double'的值,则可能涉及未定义的行为。只需将

加倍到最接近的整数并打印出来。


In C the term is cast and you don''t need to do that, since casting a
floating type to an integer type might involve undefined behaviour if the
integer type is not big enough to hold the double''s value. Just round the
double to the nearest whole number and print it out.


另外,作为旁注,我知道我可以使用

C风格的字符串或C ++字符串
Also, just as a sidenote, I know that I can use
either a C-style string or C++ string



编号如果您使用C ++中的任何内容,那么您的代码就变成了C ++。请参阅

comp.lang.c ++。 C和C ++之间的交互是微妙的并且涉及到。


No. If you use anything from C++, then your code becomes C++. Consult on
comp.lang.c++. Interaction between C and C++ is subtle and involved.


然后使用stdlib头

将字符串转换为可解决的整数我用户输入时遇到的这个小问题,但是,我不是因为我的计算机科学而需要使用这种技术。

编程讲师告诉我不要使用字符串,因为我们还没有

覆盖的字符串(我是编程I / Novice的大学新生

编程课程,虽然我应该是更高级别的CS课程

但是这个时刻是无关紧要的。)因此,我希望

关于什么功能/实现的一些指导我可以使用

不使用字符串,这将控制使用超大数据 -

类型;超过数据类型可容纳的最大/最小大小。
and then use the stdlib header
to convert the string to an integer which would solve this little
problem that I have been having with user input, however, I am not
allowed to use such a technique because my Computer Science
programming instructor told me not to use strings since we have not
covered strings yet (I''m a college freshman in a Programming I/Novice
Programming course, although I should be in a higher level CS course
but that''s irrelavent at this moment in time.) Therefore, I would like
some guidance as to what functions/implementations I could use that
DOES NOT use strings, that would control the use of oversizing a data-
type; exceeding the maximum/minimum size that a data type can hold.



如果你不能使用

字符串,scanf系列函数是你唯一的选择。如果scanf读取的值对于有问题的对象来说太大了,那么行为是未定义的。通常你会得到垃圾值。


一个更强大的解决方案是将值作为字符串读入fgets和

将其转换为strtol或strtod转换。这些函数表明它们何时无法转换价值。不幸的是,显然你被禁止使用它们。


我的建议是使用long,unsigned long,long long,unsigned中的任何一个

long long,double或long double,具体取决于您的确切需求和
可用性,并在

提示输入时通知用户最大可能值。对于非交互式输入,没有办法,鉴于你的

限制,防止溢出或环绕。这些类型的范围

可在limits.h和float.h
中找到


The scanf family of functions are your only choice if you cannot use
strings. If the value read by scanf is too big for the object in question
then the behaviour is undefined. Usually you''ll get garbage values.

A more robust solution is to read in the value as a string with fgets and
convert it with strtol or strtod. These functions indicate when they are
unable to convert the value. Unfortunately you are apparently prohibited
from using them.

My advice is to use either one of long, unsigned long, long long, unsigned
long long, double or long double, depending on your exact needs and
availability and inform the user of the maximum possible value when
prompting for input. For non-interactive input there is no way, given your
restrictions, against overflow or wrap-around. The ranges of these types
are found in limits.h and float.h



好​​的我真的很感激。我会继续把你所拥有的东西包括在内。
说。虽然这些程序非常基础,但我相信当我做某事时,我会充分发挥其潜力。这是另一个话题本身就是超出社区参数的



n3o

"你如果不先了解攻击问题就不能考虑防御问题。

Ok I really appreciate this. I will go ahead and include what you have
said. Although these programs are very basic, I believe that when I do
something, I do it to my full potential. And that''s another topic
within itself that is outside the parameters of this community.

n3o
"You can''t consider the problem of defense without first understanding
the problem of attack."


n3o写道On 10 / 02/07 13:35,:
n3o wrote On 10/02/07 13:35,:

Hello Comp.Lang.C会员,


我有一个问题用户输入我一直在试图找出最长的
。例如,让我们说你有这样的事情:


void foo()

{

int num;

printf(请输入一个数字:);

scanf("%d",& num); //是的我知道这个函数可能会抛出一些

警告一些编译器(MSVC ++ 8.0)

...

}


现在我们知道int数据类型使用4个字节的内存。
Hello Comp.Lang.C Members,

I have an issue with user input that I have been trying to figure out
for the longest. For instance, let''s say you have something like this:

void foo()
{
int num;
printf("Please enter a number: ");
scanf("%d", &num); // yes I know this function may throw a
warning with some compilers (MSVC++ 8.0)
...
}

now we know that the int data type uses 4 bytes of memory.



在您的机器上,也许,但不是全部。

On your machine, perhaps, but not on all.


让我们说

用户输入的数字超过了int

数据类型可以容纳的最小/最大大小。这将被视为一个错误,并将导致

程序以不恰当的方式运行。
Let''s say
the user enters a number that exceeds the minimum/maximum size the int
data type can hold. This would be considered a bug and would cause the
program to function in an innappropriate manner.



它产生未定义的行为。也就是说,如果

输入超出范围,并且
$ b $的不同实现,则c

语言不会指定scanf()将执行的操作。 b C的行为会有所不同。

It produces "undefined behavior." That is, the C
language does not specify what scanf() will do if the
input is out of range, and different implementations of
C will behave differently.


更具体一点,

让我们说程序员想要一个真正的数字意味着他们不要
不要浮点数。
To be more specific,
let''s say that the programmer wants a real number meaning that they do
not want floating point numbers.



我不确定你的意思。如果你的意思是你想要用户能够输入任意实数

如pi或e甚至是phi,那你就不走运了:C没有

对这些值的内置支持。

I''m not sure what you mean. If you mean that you
want the user to be able to enter arbitrary real numbers
like pi or e or even p you are out of luck: C has no
built-in support for such values.


如果是这种情况,我们知道

只是将''num''的数据类型更改为double将无法解决

问题,具体原因我将在一分钟内解释。
If that is the case, we know that by
just changing the data type of ''num'' to double would not solve the
issue for a specific reason which I will explain in a minute.



你刚才写过程序员没有想要浮点数的b $ b。在C中,'double''是浮点数

You wrote a moment ago that the programmer does not
want floating-point numbers. In C, `double'' is a floating-
point number, so why are you even considering it?


此外,通过

将数据类型''num''更改为long int也不能解决

问题。为什么? long int使用4个字节,就像int一样,
Also, by
changing the data type of, ''num,'' to long int would not solve the
issue either. Why? long int uses 4 bytes just like int,



同样,这可能适用于你的机器,但不是

都是真的。

Again, this may be true of your machine but is not
true of all.


和double,

这不是程序员在这个例子中想要的,使用8个字节。
and double,
which is not what the programmer wants in this instance, uses 8 bytes.



您机器的Tru,但不一定是。

Tru of your machine, but not necessarily of all.


因此,如果有人可以指导我至于如何解决这个轻微的问题,我会非常感激。
Therefore, if someone could guide me as to how I can fix this minor
problem, I would really appreciate it greatly.



什么是问题?你想修好吗?添加

处理任意实数的能力可能是不可能的b $ b:计算机是有限的机器因此

只能处理有限的数字不同的价值观。


如果出现问题是为了读取%d的数字太多或者很小的数字。可能有办法改善

位。但是移动边界并不能消除它们;

仍然会有数字太大或太小而无论您使用何种转换方案,以及
的行为
scanf()遇到这样的数字时仍然是

undefined。

What is the "problem" you want fixed? Adding the
ability to handle arbitrary real numbers is probably
not possible: computers are finite machines and thus
can only deal with a finite number of distinct values.

If the "problem" is to read numbers that are too
large or small for "%d" there may be ways to improve a
bit. But moving the boundaries does not eliminate them;
there will still be numbers too large or too small for
whatever conversion scheme you use, and the behavior of
scanf() when it encounters such numbers will still be
undefined.


但是,现在我认为

,如果我将''num''设为double,是否可以输入''num''到

一个int数据类型,以便''num' '将输出整数,

无十进制数?
But, now that I think
of it, if I make ''num'' a double, is it possible to type cast ''num'' to
an int data type so that the ''num'' will output as a whole number with
no decimal number?



如果您需要做的就是打印'double''值

舍入为整数,只需将其保留为`double ''和

使用"%。0f"而不仅仅是%f。

If all you need to do is print the `double'' value
rounded to an integer, just leave it as a `double'' and
use "%.0f" instead of just "%f".


另外,作为旁注,我知道我可以使用

或者C-样式字符串或C ++字符串
Also, just as a sidenote, I know that I can use
either a C-style string or C++ string



不在C中,你不能。

Not in C, you can''t.


然后使用stdlib标题

将字符串转换为一个整数,这将解决我用户输入时遇到的这个小问题,但是,我不是

允许使用这种技术因为我的计算机科学

编程讲师告诉我不要使用字符串因为我们还没有用b $ b覆盖的字符串(我是编程我/新手的大学新生

编程课程,虽然我应该进入更高级别的CS课程

但是现在这是无关紧要的。)因此,我想

关于我可以使用哪些函数/实现的一些指导

不使用字符串,这将是控制使用超大数据 -

类型;超过数据类型可容纳的最大/最小大小。
and then use the stdlib header
to convert the string to an integer which would solve this little
problem that I have been having with user input, however, I am not
allowed to use such a technique because my Computer Science
programming instructor told me not to use strings since we have not
covered strings yet (I''m a college freshman in a Programming I/Novice
Programming course, although I should be in a higher level CS course
but that''s irrelavent at this moment in time.) Therefore, I would like
some guidance as to what functions/implementations I could use that
DOES NOT use strings, that would control the use of oversizing a data-
type; exceeding the maximum/minimum size that a data type can hold.



我想,你可以用getc()或其中一个亲戚一次读取一个字符

,并开发值
你去的数字是
。这样就可以避免使用字符串禁止使用
,因为只需处理一个字符,而不是其他字符,就不会形成任何字符串。
但是,b $ b似乎是一个非常愚蠢的运动。


-
Er ********* @ sun.com


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

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