将数组传递给isdigit() [英] passing array to isdigit()

查看:83
本文介绍了将数组传递给isdigit()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我正在尝试确认输入是否为数字,所以我认为这很容易用/或
用isdigit()来做funktion,

但如果输入超过1个符号,如何将数组传递给它?


#include< stdio.h>

#include< ctype.h>

#include< stdlib.h>

int main(){

char a [3]; //输入2个符号+ \ 0


scanf("%c",& a);

if(isdigit(a [0] )&& isdigit(a [1])){printf(" japp");

}

else {

printf (nopp);

}


printf(" \ n\\\
a [0] =>%da [1] = >%d",a [0],a [1]);

返回0;

}


获取产量:

12

nopp

2359144

-

提前b Thanx!


;-)


______________________________________

我最后点亮,但每一个时间我走了一步它变得昏暗。

hi!

I am trying to confirm if input is digit, so I thought it would by easy to
do it with with isdigit() funktion,
but how do I pass arrays to it if the imput is more then 1 sign?

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main(){
char a[3]; // enter 2 sign + \0

scanf("%c", &a);
if(isdigit(a[0]) && isdigit(a[1])) {printf("japp");
}
else{
printf("nopp");
}

printf("\n\na[0]=>%d a[1]=>%d", a[0], a[1]);
return 0;
}

getting output:
12
nopp

2359144
--
Thanx in advance!

;-)

______________________________________
I se the lightat the end, but every time I take a step it''s get dim.

推荐答案




Carramba写道:


Carramba wrote:
嗨!

我正在尝试确认输入是否为数字,所以我认为它会通过easyd
用isdigit()funktion来实现,
但如果输入超过1个符号,如何将数组传递给它呢?

# include< stdio.h>
#include< ctype.h>
#include< stdlib.h>
int main(){
char a [3]; //输入2符号+ \0

scanf("%c",& a);


这就是问题所在:"%c"只读一个字符,所以

只有[0]接收输入。 a [1]和[2]保持不变,

包含随机垃圾 (可能包括也可能不包括

你提到''\ 0'')。您可能想要的是


scanf("%2s",a);


请注意c的变化(读一个字符)到s (读取

一个字符串),包含2字符串。将字符串长度

限制为a []数组可以容纳的数量,并删除

'&''运算符。

if(isdigit(a [0])&& isdigit(a [1])){printf(" japp");
hi!

I am trying to confirm if input is digit, so I thought it would by easyto
do it with with isdigit() funktion,
but how do I pass arrays to it if the imput is more then 1 sign?

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main(){
char a[3]; // enter 2 sign + \0

scanf("%c", &a);
Here''s the problem: "%c" reads only one character, so
only a[0] receives input. a[1] and a[2] remain unchanged,
containing "random garbage" (which may or may not include
the ''\0'' you mention). What you probably want is

scanf("%2s", a);

Note the change from "c" (read one character) to "s" (read
a string), the inclusion of "2" to limit the string length
to the amount the a[] array can hold, and the removal of
the `&'' operator.
if(isdigit(a[0]) && isdigit(a[1])) {printf("japp");




Here'另一个问题,可能没有涉及你的问题,但问题仍然存在。写一下


if(isdigit((unsigned char)a [0])&& ...


来防范字符代码所有

的数字0-9都有正码,但是像

''?''和''''这样的字符可能会对某些字符产生负面影响。如果用户

输入这样的字符,你就不能安全地使用isdigit()on

it,直到你把它转换为非负值。


-
Er ********* @ sun。 com



Here''s another problem, probably not involved in your
trouble but a problem nonetheless. Write

if (isdigit( (unsigned char)a[0] ) && ...

to guard against character codes with negative values. All
of the digits 0-9 have positive codes, but characters like
''?'' and ''?'' may be negative on some machines. If the user
enters such a character, you cannot safely use isdigit() on
it until you convert it to a non-negative value.

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




Carramba写道:


Carramba wrote:
hi!

我试图确认输入是否为数字,所以我认为用isdigit()funktion很容易
但是如何将数组传递给它如果输入超过1个符号?


你的意思是:不止一个字符。

有几种方法可以解决这个问题:

1)你有一个字符串并将其传递给af通过

整个字符串运行并检查每个字符(但不是字符串终止符)

with isdigit()。如果您遇到非数字,则返回false。

2)您有一个char数组和潜在数字

字符。如上所述并传递数组和长度,但不是检查字符串终止符,而是使用你传递的长度运行所有潜在的

数字。

3)strtol()函数将字符串转换为long值。

它的界面提供了检查最后一个数字

读取是否是最后一个数字的方法字符串终结符之前的字符。

可能会丢弃前导空格 - 只需查看它。

4)使用sscanf()加上限制为数字的scanset并获得

读取字符数。如果它等于字符串长度,

你知道一切都是数字。

#include< stdio.h>
#include< ctype.h>
#include< stdlib.h>
int main(){
char a [3]; //输入2符号+ \0

scanf("%c",& a);
你的意思是

int n,onlydigits;

if(1!= scanf("%2s%n",a,& n))

{

/ *处理错误* /

}

if(strlen(a)!= n) {/ *我们阅读的字符多于预期* /}

if(isdigit(a [0])&& isdigit(a [1]))


onlydigits = 1;

for(n = 0; n< strlen(a); n ++)

if(!isdigit(a [n]))

onlydigits = 0;

if(onlydigits)

{printf(" japp");
}
{
printf(" nopp);;

printf(" \ n\\\ [0] =>%da [1] => %d",a [0],a [1]);
返回0;
}
获得输出:
12
nopp

2359144
hi!

I am trying to confirm if input is digit, so I thought it would by easy
to do it with with isdigit() funktion,
but how do I pass arrays to it if the imput is more then 1 sign?
You mean: more than one character.
There are several ways to go about this:
1) You have a string and pass it to a function which runs through the
whole string and checks every character (but not the string terminator)
with isdigit(). If you encounter non-digit, you return false.
2) You have an array of char and the number of potential digit
characters. Do as above and pass array and length but instead of
checking against the string terminator, run through all potential
digits using the length you got passed.
3) The strtol() function will convert a string into a long value.
Its interface provides the means to check whether the last digit
read was the last character before the string terminator.
Leading white spaces might be discarded -- just look it up.
4) Use sscanf() plus scanset restricted to the digits and get also
the number of read characters. If it equals the string length,
you know everything was a digit.

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main(){
char a[3]; // enter 2 sign + \0

scanf("%c", &a); You mean
int n, onlydigits;
if( 1 != scanf("%2s%n", a, &n))
{
/* Deal with error */
}
if (strlen(a)!=n) { /* We read more characters than expected */ }
if(isdigit(a[0]) && isdigit(a[1]))
onlydigits = 1;
for (n=0; n<strlen(a); n++)
if (!isdigit(a[n]))
onlydigits = 0;
if(onlydigits)
{printf("japp");
}
else{
printf("nopp");
}

printf("\n\na[0]=>%d a[1]=>%d", a[0], a[1]);
return 0;
}

getting output:
12
nopp

2359144




-Michael

-

电子邮件:我的是一个gmx dot de地址。



-Michael
--
E-Mail: Mine is a gmx dot de address.




Carramba写道:

Carramba wrote:
hi!

我想确认是否有inp ut是数字,所以我认为用
很容易用isdigit()funktion,


我认为你试图得到1位数字或来自用户的2位数字
数字(由3个元素字符数组判断,其中

1个元素为null)。
但如果输入超过1个符号,如何将数组传递给它?


符号我认为你的意思是数字。你不能将数组传递给isdigit()。

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


为什么要添加stdlib.h?

int main(){
char a [3]; //输入2个符号+ \ 0


假设您的意思是2位且为空。


scanf("%c",& ; a);


您正在向scanf发送整个阵列的地址,而它只需要

一个字符的地址。这会调用UB(类型

不匹配),但由于数组的地址与数组的第一个元素的地址相等(值不是
类型),你的程序

可能只是填充了一个(a [0])的第一个元素,它得到了来自stdin的
。 a [1]和[2]肯定不会被触及(因为你的

数组是自动的,很可能会包含垃圾)。

if(isdigit(a [ 0])&& isdigit(a [1])){printf(" japp");


a [1]包含垃圾,所以不要惊讶于if

子句中的条件评估为false。

}
else {
printf(" nopp");
}

printf(" \ n\\\ [0] =>%da [1] =>%d",a [0],a [1]);
返回0;
}
hi!

I am trying to confirm if input is digit, so I thought it would by easy to do it with with isdigit() funktion,

I think you are trying to get either a 1 digit number or a 2 digit
number from the user (judging by your 3 element character array, where
1 of the elements would be for null).

but how do I pass arrays to it if the imput is more then 1 sign?

By sign I assume you mean digit. You can''t pass arrays to isdigit().


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

why did you add stdlib.h?

int main(){
char a[3]; // enter 2 sign + \0

Assuming you meant 2 digits and null.


scanf("%c", &a);

You are sending scanf the address of the whole array, while it expects
the address of a single character only. This invokes UB (type
mismatch), but since the address of an array is equivalent(in value not
type) to the address of the first element of the array, your program
might just fill the first element of a (a[0]) with the charcter it gets
from stdin. a[1] and a[2] will definitely be untouched (and since your
array is automatic, most likely will contain garbage).

if(isdigit(a[0]) && isdigit(a[1])) {printf("japp");

a[1] contains garbage, so dont'' be suprised if the condition in the if
clause evaluates to false.

}
else{
printf("nopp");
}

printf("\n\na[0]=>%d a[1]=>%d", a[0], a[1]);
return 0;
}



这是我的程序版本,它接受输入并检查用户是否输入了1或2位数字(
)不信任scanf或得到):


#include< stdio.h> / * for getchar(),printf()& puts()* /

#include< string.h> / *表示strchr()* /

#include< stdlib.h> / *表示abs(),strtol()和exit()* /


int main()

{

char c; / * for stdin消耗while循环* /

char输入[10]; / *保存用户字符输入* /

char * chptr; / *用于fgets()&中的错误检查strtol()* /

长号; / *通过strotol转换后的数字* /

printf(输入一个或两位数的数字并按下输入:);

fflush( stdout);


chptr = fgets(输入,sizeof输入,stdin); / *用户输入

缓冲区* /

if(chptr == NULL)

{

puts( 错误:fgets失败,按Enter键退出);

getchar();

退出(EXIT_FAILURE);

}


/ *跟随if-while吃掉stdin中所有剩余的字符

upto并包括换行符(必须仍然在stdin中

如果它不在我们的输入缓冲区内* /

if(!strchr(input,''\ n''))

while((c = getchar())!=''\ n'');

number = strtol(输入,& chptr,0); / *转换为long interger * /

/ *如果number的绝对值在0到99之间,那么用户

必须输入一个或两位数的数字;如果用户输入了

一些奇怪的东西,strtol仍然可以返回''0'但是endptr会将
指向缓冲区的第一个元素,如果是这样的话;所以我们必须

确保chptr!=& ;输入[0]或chptr!=输入if * /

if(abs(number)> = 0&& abs(数字)< = 99&& chptr!=输入)

{

/ *用户输入1或2位数字* /

printf(" yapp,%ld很好的输入\ n \\ n \\ n \\ n,数字);

}

否则if(chptr == input)

{

/ *用户输入了奇怪的输入(不是数字)* /

printf(nopp,那甚至不是数字!! \ n \ n) ;

}

其他

{

/ *用户输入> 2位数* /

printf(nopp,wasn''ta 1或2位数字\ n \\ nnn);

}


puts(程序完成:按Enter键退出);

getchar();

返回EXIT_SUCCESS;

}

如果上面的程序让你感到困惑,那很好,因为混淆是什么

迫使我们进行b
调查,调查带来了启示(去年我没有

线索为什么大多数专家都使用fgets,现在我一直都在使用它。)


祝你好运。


Hereis my version of a program that takes input and checks if the user
entered a 1 or 2 digit number (don''t trust scanf or gets):

#include <stdio.h> /* for getchar(), printf() & puts() */
#include <string.h> /* for strchr() */
#include <stdlib.h> /* for abs(), strtol(), and exit() */

int main()
{
char c; /* for stdin consuming while loop */
char input[10]; /* holds user character input */
char *chptr; /* for error checks in fgets() & strtol() */
long number; /* number after conversion by strotol */

printf("Input a 1 or 2 digit number and hit enter: ");
fflush(stdout);

chptr = fgets(input, sizeof input, stdin); /* user input into
buffer */
if(chptr == NULL)
{
puts("ERROR: fgets failed, press Enter to exit");
getchar();
exit(EXIT_FAILURE);
}

/* following if-while eats all the leftover characters in stdin
upto and including the newline (which must be still in stdin
if it isn''t inside our input buffer */
if(!strchr(input, ''\n''))
while((c = getchar()) != ''\n'');
number = strtol(input, &chptr, 0); /* convert to long interger */
/* if absolute value of number is between 0 and 99, then the user
must have entered a 1 or 2 digit number; if the user entered
something weird, strtol can still return ''0'' but endptr will
point to first element of buffer if that is so; so we must
make sure that chptr != &input[0] or chptr != input in our if */

if( abs(number) >= 0 && abs(number) <= 99 && chptr != input)
{
/* user entered a 1 or 2 digit number */
printf("yapp, %ld is good input\n\n", number);
}
else if( chptr == input)
{
/* user entered weird input (not number) */
printf("nopp, that wasn''t even a number!!\n\n");
}
else
{
/* user entered > 2 digit number */
printf("nopp, wasn''t a 1 or 2 digit number\n\n");
}

puts("Program Finished: Press enter to exit");
getchar();
return EXIT_SUCCESS;
}
If the program above confuses you, good, because confusion is what
forces us to
investigate, and investigation brings enlightenment (last year I had no
clue why most experts used fgets, now I use it all the time).

Good luck.


这篇关于将数组传递给isdigit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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