I cant do change string to int.

Discussion in 'C Programming' started by emre esirik(hacettepe computer science and enginee, Nov 11, 2007.

  1. 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
     
    emre esirik(hacettepe computer science and enginee, Nov 11, 2007
    #1
    1. Advertising

  2. emre esirik(hacettepe computer science and enginee

    santosh Guest

    In article <>, emre
    esirik(hacettepe computer science and engineering)
    <> wrote on Sunday 11 Nov 2007 9:16 pm:

    > 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'.
     
    santosh, Nov 11, 2007
    #2
    1. Advertising

  3. emre esirik(hacettepe computer science and enginee

    Chris Dollin Guest

    emre esirik(hacettepe computer science and engineering) wrote:

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

    --
    Non-Digital Hedgehog
    "The path to the web becomes deeper and wider" - October Project
     
    Chris Dollin, Nov 11, 2007
    #3
  4. #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????
     
    emre esirik(hacettepe computer science and enginee, Nov 11, 2007
    #4
  5. emre esirik(hacettepe computer science and enginee

    santosh Guest

    In article <>, emre
    esirik(hacettepe computer science and engineering)
    <> 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=='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.
     
    santosh, Nov 11, 2007
    #5
  6. santosh <> writes:

    > In article <>, emre
    > esirik(hacettepe computer science and engineering)
    > <> wrote on Sunday 11 Nov 2007 11:04 pm:
    >> int z=0;

    <snip>
    >> z=1;

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


    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.

    --
    Ben.
     
    Ben Bacarisse, Nov 11, 2007
    #6
  7. emre esirik(hacettepe computer science and enginee

    santosh Guest

    In article <>, Ben Bacarisse
    <> wrote on Sunday 11 Nov 2007 11:40 pm:

    > santosh <> writes:
    >
    >> In article <>,
    >> emre esirik(hacettepe computer science and engineering)
    >> <> wrote on Sunday 11 Nov 2007 11:04 pm:
    >>> int z=0;

    > <snip>
    >>> z=1;

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

    >
    > 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.
     
    santosh, Nov 11, 2007
    #7
  8. emre esirik(hacettepe computer science and enginee

    James Kuyper Guest

    santosh wrote:
    > In article <>, emre
    > 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.
     
    James Kuyper, Nov 11, 2007
    #8
  9. emre esirik(hacettepe computer science and enginee

    Flash Gordon Guest

    santosh wrote, On 11/11/07 18:02:
    > In article <>, emre
    > esirik(hacettepe computer science and engineering)
    > <> 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.


    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.
    --
    Flash Gordon
     
    Flash Gordon, Nov 11, 2007
    #9
  10. On Sun, 11 Nov 2007 23:32:24 +0530, in comp.lang.c , santosh
    <> wrote:

    >> 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
     
    Mark McIntyre, Nov 11, 2007
    #10
  11. emre esirik(hacettepe computer science and enginee

    Default User Guest

    James Kuyper wrote:

    > santosh wrote:
    > > In article <>,
    > > emre 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:


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



    Brian
     
    Default User, Nov 11, 2007
    #11
  12. emre esirik(hacettepe computer science and enginee

    Flash Gordon Guest

    Mark McIntyre wrote, On 11/11/07 23:49:
    > On Sun, 11 Nov 2007 23:32:24 +0530, in comp.lang.c , santosh
    > <> wrote:
    >
    >>> 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.
    --
    Flash Gordon
     
    Flash Gordon, Nov 12, 2007
    #12
  13. emre esirik(hacettepe computer science and enginee

    James Kuyper Guest

    santosh wrote:
    > In article <>, Ben Bacarisse
    > <> wrote on Sunday 11 Nov 2007 11:40 pm:

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


    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.
     
    James Kuyper, Nov 12, 2007
    #13
  14. emre esirik(hacettepe computer science and enginee

    Mark Bluemel Guest

    emre esirik(hacettepe computer science and engineering) wrote:
    > #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.
     
    Mark Bluemel, Nov 12, 2007
    #14
  15. On Sun, 11 Nov 2007 09:34:05 -0800, "emre esirik(hacettepe computer
    science and engineering)" <> wrote:

    >#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
     
    Barry Schwarz, Nov 13, 2007
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,247
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,697
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    475
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    1,002
    Jim Langston
    Oct 20, 2005
  5. Nagaraj
    Replies:
    1
    Views:
    901
    Lionel B
    Mar 1, 2007
Loading...

Share This Page