Re: I cant do change string to int.



In article <1194802445.381563.324660@xxxxxxxxxxxxxxxxxxxxxxxxxxx>, emre
esirik(hacettepe computer science and engineering)
<emreesirik@xxxxxxxxx> wrote on Sunday 11 Nov 2007 11:04 pm:

Please quote the relevant portions of the article to which you are
replying.

#include <stdio.h>
#include<stdlib.h>
int main()
{
char poli[50],tanpon[6]={'\0'};
int i,d,l,toplam,polinom[6];

Good indentation and judicious use of whitespace can enhance the
readability of your code for others.

int p;
int z=0;
scanf("%s", 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.

for(i=0 ; i<50 ; i++)
{
if(poli[i]=='X' && poli[i+1]=='^')
{
toplam=i+3;
p = poli[i+2] - '0';
if(z==0)
{
for(d=0 ; d<i ; d++)
{
tanpon[d]=poli[d];
}
polinom[p]=atoi(tanpon);
}else {
tanpon[6]='\0';
for(l=toplam+1; l<i ; l++)
{
printf("toplam%d,l---->%d",toplam,l);
tanpon[l]=poli[l];
}
polinom[p]=atoi(tanpon);
}
z=1;
}
}
for(i=0 ; i<6 ; i++) {
printf(" %d ", polinom[i]);
}
return 0;
}

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.

this is my code, I want to enter like this 3X^1+32X^4+12X^0 but its
only do if part which if z==0 but why it doesnt do 'else' part????

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.

.



Relevant Pages

  • Re: I cant do change string to int.
    ... int i,d,l,toplam,polinom; ... scanfexceed the bounds of 'poli' and overwrite memory it ... Note that 'sizeof poli' only works as expected within the function ... Note also that scanf and fgets return values which should be checked, otherwise you won't know if you actually received *any* input. ...
    (comp.lang.c)
  • Re: I cant do change string to int.
    ... esirik(hacettepe computer science and engineering) ... scanf("%s", poli); ... p is a ASCII code of integer but I need integer ...
    (comp.lang.c)
  • I cant do change string to int.
    ... scanf("%s", poli); ... p is a ASCII code of integer but I need integer ... but I need p=2 integer form, how can I do, please help ...
    (comp.lang.c)