Pulling my hair out..

M

Mr. Politics

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

What gives?
 
M

Mr. Politics

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

Sylvester Hesp

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

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

- Sylvester
 
M

Mr. Politics

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

peter koch

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
 
S

Sylvester Hesp

peter koch 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;
}
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
 
P

peter koch

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.

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
 
R

RJH

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

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top