I cant do change string to int.

  • Thread starter emre esirik(hacettepe computer science and enginee
  • Start date
E

emre esirik(hacettepe computer science and enginee

char poli[50];
int p;
scanf("%s", poli);
if(poli=='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
 
S

santosh

esirik(hacettepe computer science and engineering)
char poli[50];
int p;
scanf("%s", poli);
if(poli=='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


Do:

p = poli[i+2] - '0';

This only works for '0'... '9'.
 
C

Chris Dollin

emre said:
char poli[50];
int p;
scanf("%s", poli);
if(poli=='X' && poli[i+1]=='^')
{
toplam=i+3;
p=poli[i+2];


/Please/ don't offer us random fragments of code; offer us
complete (but small) compilable (unless of course your problem
is a compilation problem) programs. For example, the fragment
above doesn't have a declaration for `i` in it: what /else/
has been missed out?

Are you /sure/ you want to be using `scanf` here, rather than
`fgets`?
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

To convert a digit character into the corresponding value, you
simply subtract `'0'`, eg

int seven = '7' - '0';

C specifies that the digit characters are in order without gaps
(whereas it does /not/ do this for letters).
 
E

emre esirik(hacettepe computer science and enginee

#include <stdio.h>
#include<stdlib.h>
int main()
{
char poli[50],tanpon[6]={'\0'};
int i,d,l,toplam,polinom[6];
int p;
int z=0;
scanf("%s", poli);
for(i=0 ; i<50 ; i++)
{
if(poli=='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);
}
return 0;
}

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????
 
S

santosh

esirik(hacettepe computer science and engineering)

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=='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);
}
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.
 
B

Ben Bacarisse

santosh said:
esirik(hacettepe computer science and engineering)



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.

there is a 'z=1;' in there. Proof, if it were needed, that good names
and good layout are important. The code is uninviting so I have not
had a look at it.
 
S

santosh

Ben Bacarisse said:
there is a 'z=1;' in there. Proof, if it were needed, that good names
and good layout are important. The code is uninviting so I have not
had a look at it.

Ah yes. Good spot. However it comes _after_ the if/else construct so
it's of no consequence to it.
 
J

James Kuyper

santosh said:
esirik(hacettepe computer science and engineering)
....
[Source code, free of indentation]
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.

His code displays fully indented in my newsreader (Firefox). I'll insert
a copy of it here, in the hope that this copy displays better in your
newsreader than the original did:
#include <stdio.h>
#include<stdlib.h>
int main()
{
char poli[50],tanpon[6]={'\0'};
int i,d,l,toplam,polinom[6];
int p;
int z=0;
scanf("%s", poli);
for(i=0 ; i<50 ; i++)
{
if(poli=='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);
}


Here's where 'z' gets set:
z=1;
}
}
for(i=0 ; i<6 ; i++) {
printf(" %d ", polinom);
}
return 0;
}
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.


I see a z=1; statement. Whatever the problem is with his code, that
isn't it.
 
F

Flash Gordon

santosh wrote, On 11/11/07 18:02:
esirik(hacettepe computer science and engineering)

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.

You mean over 49 characters, don't forget that scanf will write a /0 to
terminate the string.
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.

<snip>

Note also that scanf and fgets return values which should be checked,
otherwise you won't know if you actually received *any* input. Also you
have to deal with the newline and how you want to handle over-long inputs.
 
M

Mark McIntyre

for(i=0 ; i<6 ; i++) {
printf(" %d ", polinom);
}
return 0;
}


Without indentation your code is illegible.


FYI, the code is indented just fine when I see it in Agent. Suspect
your news server or client is mangling the tabs.
}
for(i=0 ; i<6 ; i++) {
printf(" %d ", polinom);
}
return 0;
}

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
D

Default User

James said:
santosh said:
[Source code, free of indentation]
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.

His code displays fully indented in my newsreader (Firefox). I'll
insert a copy of it here, in the hope that this copy displays better
in your newsreader than the original did:

He used tabs for indentation. Tabs aren't handled consistently.



Brian
 
F

Flash Gordon

Mark McIntyre wrote, On 11/11/07 23:49:
for(i=0 ; i<6 ; i++) {
printf(" %d ", polinom);
}
return 0;
}

Without indentation your code is illegible.


FYI, the code is indented just fine when I see it in Agent. Suspect
your news server or client is mangling the tabs.


It could easily be a news server in the path, since I know my client
handles tabs correctly when they reach it but I did not see them either.

To the OP, this is one of the reasons why you should not use tabs when
posting to news groups.
}
for(i=0 ; i<6 ; i++) {
printf(" %d ", polinom);
}
return 0;
}


See, tabs still there. I have also seen tabs reach me correctly on
original posts in the past, not just in quoted material.
 
J

James Kuyper

santosh said:
Ah yes. Good spot. However it comes _after_ the if/else construct so
it's of no consequence to it.

It's defined outside of a loop, and both set and tested inside the loop.
If it's set in the first pass through the loop, the else clause should
be triggered on the following pass. I still haven't bothered looking
closely at this code, so I'm not sure what's wrong with it, but the fact
that z is first set to a non-zero after the first time it's value is
tested isn't in itself sufficient explanation.
 
M

Mark Bluemel

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

I don't know what these variables are supposed to represent, 'z' is a
particularly meaningless name for one...
scanf("%s", poli);

bad move - you don't check the return value of scanf(), and don't force
the length to fit into poli.
for(i=0 ; i<50 ; i++)
{
if(poli=='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++)


l can never be less than i.
{
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);
}
return 0;
}

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
No...

but why it doesnt do 'else' part????


It does, but never enters the body of the loop.

Your code is a mess and the wrong approach. Find a text book and look at
parsing, perhaps with a focus on Finite State machines.
 
B

Barry Schwarz

#include <stdio.h>
#include<stdlib.h>
int main()
{
char poli[50],tanpon[6]={'\0'};
int i,d,l,toplam,polinom[6];
int p;
int z=0;
scanf("%s", poli);
for(i=0 ; i<50 ; i++)
{
if(poli=='X' && poli[i+1]=='^')


What happens if the input does not contain 'X' followed by '^'?
{
toplam=i+3;
p = poli[i+2] - '0';
if(z==0)
{
for(d=0 ; d<i ; d++)
{
tanpon[d]=poli[d];

What happens if the X^ is beyond the sixth character?
}
polinom[p]=atoi(tanpon);

What happens if the '^' is followed by a digit greater than 5?
}else {
tanpon[6]='\0';

tanpon[6] does not exist. This invokes undefined behavior.
for(l=toplam+1; l<i ; l++)

toplam is set to i+3. This for loop is executed zero iterations.
{
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);


There is no guarantee that all six element of poinom have been
assigned values.
}
return 0;
}

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????


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top