M
Mr. Politics
This function keeps giving me negative values (esp. if I feed it
64,153,160)
What gives?
64,153,160)
What gives?
This function keeps giving me negative values (esp. if I feed it
64,153,160)
What gives?
Mr. Politics said: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;
}
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
Instead of pulling hair out, you should pull your code in to anews:[email protected]...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;
}
peter koch said:Instead of pulling hair out, you should pull your code in to aThis 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;
}
debugger and see whats going on. One hint is that "casts are evil" is
almost always true.
/Peter
Instead of pulling hair out, you should pull your code in to aThis 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;
}
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
[umpteen lines quoted]
Alf said:* peter koch:[umpteen lines quoted]
Please.
Quoting that much just to add a single line is worse than top-posting.
IMHO.
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.