# Pulling my hair out..

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

1. ### Mr. PoliticsGuest

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

What gives?

Mr. Politics, Feb 12, 2007

2. ### Mr. PoliticsGuest

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

3. ### Sylvester HespGuest

"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
4. ### Mr. PoliticsGuest

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
5. ### peter kochGuest

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
6. ### Sylvester HespGuest

"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
7. ### peter kochGuest

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
8. ### Alf P. SteinbachGuest

* peter koch:
> [umpteen lines quoted]

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
9. ### Default UserGuest

Alf P. Steinbach wrote:

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

>
>
> 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
10. ### RJHGuest

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