Re: I cant do change string to int.
- From: santosh <santosh.k83@xxxxxxxxx>
- Date: Sun, 11 Nov 2007 23:32:24 +0530
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.
.
- Follow-Ups:
- Re: I cant do change string to int.
- From: Mark McIntyre
- Re: I cant do change string to int.
- From: Flash Gordon
- Re: I cant do change string to int.
- From: James Kuyper
- Re: I cant do change string to int.
- From: Ben Bacarisse
- Re: I cant do change string to int.
- References:
- I cant do change string to int.
- From: emre esirik(hacettepe computer science and engineering)
- Re: I cant do change string to int.
- From: Chris Dollin
- Re: I cant do change string to int.
- From: emre esirik(hacettepe computer science and engineering)
- I cant do change string to int.
- Prev by Date: Re: simplebinary tree
- Next by Date: Re: Problem when i pass pointers to functions
- Previous by thread: Re: I cant do change string to int.
- Next by thread: Re: I cant do change string to int.
- Index(es):
Relevant Pages
|