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)
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.
Last edited by SlyMaelstrom; 12-02-2005 at 07:32 PM.
Sent from my iPadŽ
I would be. STL and C don't mix.Originally Posted by SlyMaelstrom
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
i went with the atoi...thanks for the help