Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pulling my hair out..

Reply
Thread Tools

Pulling my hair out..

 
 
Mr. Politics
Guest
Posts: n/a
 
      02-12-2007
This function keeps giving me negative values (esp. if I feed it
64,153,160)

What gives?

 
Reply With Quote
 
 
 
 
Mr. Politics
Guest
Posts: n/a
 
      02-12-2007
On Feb 12, 8:05 am, "Mr. Politics" <mrpolit...@gmail.com> 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;
}

 
Reply With Quote
 
 
 
 
Sylvester Hesp
Guest
Posts: n/a
 
      02-12-2007
"Mr. Politics" <> wrote in message
news: ups.com...
> On Feb 12, 8:05 am, "Mr. Politics" <mrpolit...@gmail.com> 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


 
Reply With Quote
 
Mr. Politics
Guest
Posts: n/a
 
      02-12-2007
On Feb 12, 8:18 am, "Sylvester Hesp" <s.h...@oisyn.nl> wrote:
> "Mr. Politics" <mrpolit...@gmail.com> wrote in message
>
> news: ups.com...
>
>
>
> > On Feb 12, 8:05 am, "Mr. Politics" <mrpolit...@gmail.com> 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;
}

 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      02-12-2007
On Feb 12, 2:35 pm, "Mr. Politics" <mrpolit...@gmail.com> wrote:
> On Feb 12, 8:18 am, "Sylvester Hesp" <s.h...@oisyn.nl> wrote:
>
>
>
>
>
> > "Mr. Politics" <mrpolit...@gmail.com> wrote in message

>
> >news: oups.com...

>
> > > On Feb 12, 8:05 am, "Mr. Politics" <mrpolit...@gmail.com> 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

 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      02-12-2007

"peter koch" <> wrote in message
news: oups.com...
> On Feb 12, 2:35 pm, "Mr. Politics" <mrpolit...@gmail.com> wrote:
>> On Feb 12, 8:18 am, "Sylvester Hesp" <s.h...@oisyn.nl> wrote:
>>
>>
>>
>>
>>
>> > "Mr. Politics" <mrpolit...@gmail.com> wrote in message

>>
>> >news: oups.com...

>>
>> > > On Feb 12, 8:05 am, "Mr. Politics" <mrpolit...@gmail.com> 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


 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      02-12-2007
On Feb 12, 3:13 pm, "Sylvester Hesp" <s.h...@oisyn.nl> wrote:
> "peter koch" <peter.koch.lar...@gmail.com> wrote in message
>
> news: oups.com...
>
>
>
>
>
> > On Feb 12, 2:35 pm, "Mr. Politics" <mrpolit...@gmail.com> wrote:
> >> On Feb 12, 8:18 am, "Sylvester Hesp" <s.h...@oisyn.nl> wrote:

>
> >> > "Mr. Politics" <mrpolit...@gmail.com> wrote in message

>
> >> >news: oups.com...

>
> >> > > On Feb 12, 8:05 am, "Mr. Politics" <mrpolit...@gmail.com> 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

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-12-2007
* 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?
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-12-2007
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
 
Reply With Quote
 
RJH
Guest
Posts: n/a
 
      02-12-2007

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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hair Loss ? This Will Make Your Hair Grow Again ! zackP Python 0 06-15-2009 02:35 AM
Hair Loss ? This Will Make Your Hair Grow Again ! Nicolas Python 0 04-24-2009 11:09 AM
(Hair Loss) Natural 100% Proven Techniques To Grow Hair rogerp C Programming 0 03-22-2009 07:45 AM
(Hair Loss) Natural 100% Proven Techniques To Grow Hair rogerp Python 0 03-22-2009 07:45 AM
hair loss help, grow hair again for better photos thehairlossman@operamail.com Digital Photography 13 03-19-2006 01:31 AM



Advertisments