问题描述
char poli [50];
int p;
scanf("%s",poli);
if(poli) [i] ==''X''&& poli [i + 1] ==''^'')
{
toplam = i + 3 ;
p = poli [i + 2];
p是整数的ASCII码,但我需要整数
,例如if poli [i + 2] =''2''它放p 50(ascii代码为''2'')
但我需要p = 2整数形式,我该怎么办,请帮忙
char poli[50];
int p;
scanf("%s", poli);
if(poli[i]==''X'' && poli[i+1]==''^'')
{
toplam=i+3;
p=poli[i+2];
p is a ASCII code of integer but I need integer
for example if poli[i+2]=''2'' it puts p 50 (ascii code of ''2'')
but I need p=2 integer form, how can I do, please help
推荐答案
做:
p = poli [i +2] - ''0'';
这仅适用于''0''...'''9''。
Do:
p = poli[i+2] - ''0'';
This only works for ''0''... ''9''.
良好的缩进和明智地使用空格可以增强代码对其他人的可读性。
Good indentation and judicious use of whitespace can enhance the
readability of your code for others.
scanf()与's''说明符的行为很像gets(),是一种获得输入的危险方式。如果输入包含超过50个
个字符,则scanf()超出''poli''的范围并覆盖它所拥有的内存,即b $ b不拥有,调用未定义的行为。 br />
要读取一行输入,请使用fgets()。
fgets(poli,sizeof poli,stdin);
注意''sizeof poli''只能在函数中按预期工作
,其中''poli''被定义。在其他任何地方你必须明确地将'/ b'大小的''poli''传递给需要它的代码。
scanf() with the ''s'' specifier behaves much like gets() and is a
dangerous way to get input. If the input consists of over 50
characters, scanf() exceed the bounds of ''poli'' and overwrite memory it
does not own, invoking undefined behaviour.
To read a line of input use fgets().
fgets(poli, sizeof poli, stdin);
Note that ''sizeof poli'' only works as expected within the function
where ''poli'' is defined. Anywhere else you must explicitly pass the
size of ''poli'' to code that needs it.
没有缩进你的代码难以辨认。我确定必须有一个更简单的方法来解决你想要做的事情,如果你能拼出来的那么明显。
Without indentation your code is illegible. I''m sure there must be a
much simpler way for what you are trying to do, if you can spell it out
clearly.
因为''z''始终为零。您将其初始化为零但没有其他代码
更改它,因此始终执行if / else语句的if部分。
Because ''z'' is always zero. You initialise it to zero but no other code
changes it, so the if part of the if/else statement is always executed.
这篇关于我不能将字符串更改为int。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!