Pulling my hair out..

Discussion in 'C++' started by Mr. Politics, Feb 12, 2007.

  1. Mr. Politics

    Mr. Politics Guest

    This function keeps giving me negative values (esp. if I feed it
    64,153,160)

    What gives?
     
    Mr. Politics, Feb 12, 2007
    #1
    1. Advertising

  2. Mr. Politics

    Mr. Politics Guest

    On Feb 12, 8:05 am, "Mr. Politics" <> wrote:
    > This function keeps giving me negative values (esp. if I feed it
    > 64,153,160)
    >
    > What gives?


    int quantity(char chrs[],int length)
    {
    int q = 0;
    int x;
    int c = 0;

    for (x=0; x<length;x++)
    {
    c = (int)chrs[x];
    q += c;
    }
    return q;
    }
     
    Mr. Politics, Feb 12, 2007
    #2
    1. Advertising

  3. "Mr. Politics" <> wrote in message
    news:...
    > On Feb 12, 8:05 am, "Mr. Politics" <> wrote:
    >> This function keeps giving me negative values (esp. if I feed it
    >> 64,153,160)
    >>
    >> What gives?

    >
    > int quantity(char chrs[],int length)
    > {
    > int q = 0;
    > int x;
    > int c = 0;
    >
    > for (x=0; x<length;x++)
    > {
    > c = (int)chrs[x];
    > q += c;
    > }
    > return q;
    > }
    >


    char is probably signed on your platform (and with 8 bits, it can possibly
    only hold values between -128 and 127 inclusive)

    - Sylvester
     
    Sylvester Hesp, Feb 12, 2007
    #3
  4. Mr. Politics

    Mr. Politics Guest

    On Feb 12, 8:18 am, "Sylvester Hesp" <> wrote:
    > "Mr. Politics" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > On Feb 12, 8:05 am, "Mr. Politics" <> wrote:
    > >> This function keeps giving me negative values (esp. if I feed it
    > >> 64,153,160)

    >
    > >> What gives?

    >
    > > int quantity(char chrs[],int length)
    > > {
    > > int q = 0;
    > > int x;
    > > int c = 0;

    >
    > > for (x=0; x<length;x++)
    > > {
    > > c = (int)chrs[x];
    > > q += c;
    > > }
    > > return q;
    > > }

    >
    > char is probably signed on your platform (and with 8 bits, it can possibly
    > only hold values between -128 and 127 inclusive)
    >
    > - Sylvester


    For posterity, the fix... (Thank you!)

    int quantity(unsigned char chrs[],int length)
    {
    int q = 0;
    int x;
    int c = 0;

    for (x=0; x<length;x++)
    {
    c = (int)chrs[x];
    q += c;

    }
    return q;
    }
     
    Mr. Politics, Feb 12, 2007
    #4
  5. Mr. Politics

    peter koch Guest

    On Feb 12, 2:35 pm, "Mr. Politics" <> wrote:
    > On Feb 12, 8:18 am, "Sylvester Hesp" <> wrote:
    >
    >
    >
    >
    >
    > > "Mr. Politics" <> wrote in message

    >
    > >news:...

    >
    > > > On Feb 12, 8:05 am, "Mr. Politics" <> wrote:
    > > >> This function keeps giving me negative values (esp. if I feed it
    > > >> 64,153,160)

    >
    > > >> What gives?

    >
    > > > int quantity(char chrs[],int length)
    > > > {
    > > > int q = 0;
    > > > int x;
    > > > int c = 0;

    >
    > > > for (x=0; x<length;x++)
    > > > {
    > > > c = (int)chrs[x];
    > > > q += c;
    > > > }
    > > > return q;
    > > > }

    >
    > > char is probably signed on your platform (and with 8 bits, it can possibly
    > > only hold values between -128 and 127 inclusive)

    >
    > > - Sylvester

    >
    > For posterity, the fix... (Thank you!)
    >
    > int quantity(unsigned char chrs[],int length)
    > {
    > int q = 0;
    > int x;
    > int c = 0;
    >
    > for (x=0; x<length;x++)
    > {
    > c = (int)chrs[x];
    > q += c;
    >
    > }
    > return q;
    >
    >
    >
    > }

    Instead of pulling hair out, you should pull your code in to a
    debugger and see whats going on. One hint is that "casts are evil" is
    almost always true.

    /Peter
     
    peter koch, Feb 12, 2007
    #5
  6. "peter koch" <> wrote in message
    news:...
    > On Feb 12, 2:35 pm, "Mr. Politics" <> wrote:
    >> On Feb 12, 8:18 am, "Sylvester Hesp" <> wrote:
    >>
    >>
    >>
    >>
    >>
    >> > "Mr. Politics" <> wrote in message

    >>
    >> >news:...

    >>
    >> > > On Feb 12, 8:05 am, "Mr. Politics" <> wrote:
    >> > >> This function keeps giving me negative values (esp. if I feed it
    >> > >> 64,153,160)

    >>
    >> > >> What gives?

    >>
    >> > > int quantity(char chrs[],int length)
    >> > > {
    >> > > int q = 0;
    >> > > int x;
    >> > > int c = 0;

    >>
    >> > > for (x=0; x<length;x++)
    >> > > {
    >> > > c = (int)chrs[x];
    >> > > q += c;
    >> > > }
    >> > > return q;
    >> > > }

    >>
    >> > char is probably signed on your platform (and with 8 bits, it can
    >> > possibly
    >> > only hold values between -128 and 127 inclusive)

    >>
    >> > - Sylvester

    >>
    >> For posterity, the fix... (Thank you!)
    >>
    >> int quantity(unsigned char chrs[],int length)
    >> {
    >> int q = 0;
    >> int x;
    >> int c = 0;
    >>
    >> for (x=0; x<length;x++)
    >> {
    >> c = (int)chrs[x];
    >> q += c;
    >>
    >> }
    >> return q;
    >>
    >>
    >>
    >> }

    > Instead of pulling hair out, you should pull your code in to a
    > debugger and see whats going on. One hint is that "casts are evil" is
    > almost always true.
    >
    > /Peter
    >


    While I agree with you, I don't really see how this particular situation
    changes when you just remove the cast to int :)

    - Sylvester
     
    Sylvester Hesp, Feb 12, 2007
    #6
  7. Mr. Politics

    peter koch Guest

    On Feb 12, 3:13 pm, "Sylvester Hesp" <> wrote:
    > "peter koch" <> wrote in message
    >
    > news:...
    >
    >
    >
    >
    >
    > > On Feb 12, 2:35 pm, "Mr. Politics" <> wrote:
    > >> On Feb 12, 8:18 am, "Sylvester Hesp" <> wrote:

    >
    > >> > "Mr. Politics" <> wrote in message

    >
    > >> >news:...

    >
    > >> > > On Feb 12, 8:05 am, "Mr. Politics" <> wrote:
    > >> > >> This function keeps giving me negative values (esp. if I feed it
    > >> > >> 64,153,160)

    >
    > >> > >> What gives?

    >
    > >> > > int quantity(char chrs[],int length)
    > >> > > {
    > >> > > int q = 0;
    > >> > > int x;
    > >> > > int c = 0;

    >
    > >> > > for (x=0; x<length;x++)
    > >> > > {
    > >> > > c = (int)chrs[x];
    > >> > > q += c;
    > >> > > }
    > >> > > return q;
    > >> > > }

    >
    > >> > char is probably signed on your platform (and with 8 bits, it can
    > >> > possibly
    > >> > only hold values between -128 and 127 inclusive)

    >
    > >> > - Sylvester

    >
    > >> For posterity, the fix... (Thank you!)

    >
    > >> int quantity(unsigned char chrs[],int length)
    > >> {
    > >> int q = 0;
    > >> int x;
    > >> int c = 0;

    >
    > >> for (x=0; x<length;x++)
    > >> {
    > >> c = (int)chrs[x];
    > >> q += c;

    >
    > >> }
    > >> return q;

    >
    > >> }

    > > Instead of pulling hair out, you should pull your code in to a
    > > debugger and see whats going on. One hint is that "casts are evil" is
    > > almost always true.

    >
    > > /Peter

    >
    > While I agree with you, I don't really see how this particular situation
    > changes when you just remove the cast to int :)


    My hope was that the OP would realise that there would have to be a
    "real" conversion, and not just a cast. I was seemingly to optimistic.

    /Peter
     
    peter koch, Feb 12, 2007
    #7
  8. * peter koch:
    > [umpteen lines quoted]


    Please.

    Quoting that much just to add a single line is /worse/ than top-posting.

    IMHO.


    Cheers,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Feb 12, 2007
    #8
  9. Mr. Politics

    Default User Guest

    Alf P. Steinbach wrote:

    > * peter koch:
    > > [umpteen lines quoted]

    >
    > Please.
    >
    > Quoting that much just to add a single line is worse than top-posting.
    >
    > IMHO.


    Except that top-posters would almost certainly have quoted the whole as
    well.




    Brian
     
    Default User, Feb 12, 2007
    #9
  10. Mr. Politics

    RJH Guest

    Not quite sure what you are trying to accomplish? Are you trying to
    cast the char array into 1 int or into a combination of 3 or so ints?
    It seems like 1 int.. if so, you need to do a lot more.

    If its one int, this would work.

    int quantity(unsigned char chrs[],int length)
    {
    int q = 0;
    int x;
    int c = 0;

    for (x=0; x<length;x++)
    {
    c = (int)chrs[x];
    c -= 48;
    int pos = (length - x);
    c *= pow(10, pos-1);
    q += c;
    }
    return q;
    }
     
    RJH, Feb 12, 2007
    #10
    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. Replies:
    9
    Views:
    456
    Toby A Inkster
    Jul 16, 2003
  2. Richard
    Replies:
    6
    Views:
    338
    Richard
    Nov 15, 2003
  3. Sam Roberts
    Replies:
    16
    Views:
    387
    Daniel Berger
    Feb 18, 2005
  4. James Edward Gray II

    Pulling My Hair Out With WEBrick!

    James Edward Gray II, Jun 5, 2005, in forum: Ruby
    Replies:
    0
    Views:
    94
    James Edward Gray II
    Jun 5, 2005
  5. Replies:
    3
    Views:
    100
Loading...

Share This Page