I'm trying to add a digit to a variable called id, but it's not working. Here's what I have: int id=id..1;

What am I doing wrong?

I'm trying to add a digit to a variable called id, but it's not working. Here's what I have: int id=id..1;

What am I doing wrong?

By digit... you mean exactly what? If id was 2 ... you'd want it to be 21?

We'll like if I push a button, and the value of id is 21, it will add, lets say 6, to the end of id, so there for, id would then be 216.

Will you know how many digits there would be? Like, will id always be 2, adding a third digit (doubtful)

I'm thinking of a really complicated approach to solving this, but I'm not sure if it's right.

As many digits. You can't use +, or it will add it.

You can't use +, or it will add it.

You can multiply the existing value by 10, then add the new value. 0 * 10 = 0 + 2 = 2 2 * 10 = 20 + 1 = 21 21 * 10 = 210 + 6 = 216

I did not think of that. That's really creative programming/math! Thanks, I'll try it.

That's really creative programming/math!

That's basic arithmetic.

*Note: The following was written but was beat by PaulS's post (shakes tiny fist :grin:) - still, it has some notes that might be useful...*

Base-10 man, base-10! Do they even teach this in school anymore...?

First time thru is your number (the 1's position) - for the rest, multiple by 10 and add the number:

So - first number is 7 yields: int x= 7 - ie, "7"

Next number is 8 yields: x=x*10 + 8 - ie "78"

Next number is 5 yields: x=x*10 + 5 - ie "785"

Next number is 3 yields: x=x*10 + 3 - ie "7853"

Note that this algorithm only works with single digit "incoming" numbers (0-9) - a double or larger number of digit algorithm is possible, and I will leave it as an exercise for the reader ;)

Also note that if you tried to go beyond 4 digits (depending on where you are in the addition) with the above variable as an "int", you would overflow the variable (so you are limited in the number of numbers, of course).

If the number isn't important as a "number" though, you could use a char string array (though you'd still be limited in length - unless you got fancy with dynamic allocation of memory - not an easy task, though). The only other possiblity would be to use a variable of single datatype, and go from there (still, limits would be easy to hit).

Maybe I should do it as some sort of string. Because after 5 digits, the 6th digit isn't the last digit on the end. It's like a negative number.

Well, a string could be easier.

I got up to 9 digits using long.

Maybe I should do it as some sort of string. Because after 5 digits, the 6th digit isn't the last digit on the end

That's because an "int" can only store up to +32767. after that, it wraps around to -32768. If you used a "long" instead, you could keep going to two billion plus.

Well, a string could be easier

But it wouldn't engender an understanding of the problem.

I'd really think it depends on preference. Both can be done. Just which makes sense to you.

Is there one that can hold at least 20 digits?

20 decimal digits?

No.

A “long long” will do about 18 decimal digits.

Are you into cosmology?

If you want a lot, a lot of digits, String would probably be the best.

What's cosmology?

No.

You could also look into fixed point arithmetic, and span the number across multiple variables (likely in an array); of course this would require writing a fairly complicated library to handle the fixed point math, data shifting among the variables, etc...