I have an array, lets call it buffer.
buffer[0] = 9
buffer[1] = 2
buffer[2] = 3
how would i get a variable, x, to equal 923 (not only those numbers, but the contents of the buffer, whatever they may be)
Printable View
I have an array, lets call it buffer.
buffer[0] = 9
buffer[1] = 2
buffer[2] = 3
how would i get a variable, x, to equal 923 (not only those numbers, but the contents of the buffer, whatever they may be)
Well you could start from the back of the array and as you go backwards through it summing, just create a multiple that gets 10 times bigger everytime you decrement the index.
Another way would be to use the itoa() function on each one and strcat() the values together and atoi() that value.Code:int x;
int sum = 0;
int multiple = 1;
for (x = 2; x > 0; x--) {
sum += buffer[x] * multiple;
mutiple *= 10;
}
Either way, I'm sure there is a better way. I wouldn't be suprised if one of the wiseasses in here came in with a STL function that does just what you're asking. :p
I would be. STL and C don't mix. ;)Quote:
Originally Posted by SlyMaelstrom
i went with the atoi...thanks for the help