I have a small keyboard with digits (0-9). I'm going to create a function that returns the number of the current pressed button. The thing is that I need to create values with more than one digit. For example: if I want to write 1376, the output will be 1, 3, 7, 6. I need to join these integers together into one variable (just like a calculator), and of course I can't use 1 + 3 + 7 + 6.
Does anyone have a suggestion to a solution of my problem? I appreciate all help I can get.
you could make your function so the moment you press a non-numeric key, it builds an integer value by adding 6 * 10^0 + 7 * 10^1 +3 * 10^2 + 1 * 10^1 by using a loop where the exponent is the counter.
Of course! Thank you! I don't know why I didn't think of that by myself. But I leave the question open for more suggestions. Is there maybe a built-in class or function to solve the problem?
Read a key. Save it's value (1).
Read another key. Multiply previous value by 10. Add the new key. (10 + 3 = 13).
Repeat until the end...
13 * 10 = 130. 130 + 7 = 137
1370 * 10 = 1370. 1370 + 6 = 1376.
Read a key. Save it's value (1).
Read another key. Multiply previous value by 10. Add the new key. (10 + 3 = 13).
Repeat until the end...
13 * 10 = 130. 130 + 7 = 137
1370 * 10 = 1370. 1370 + 6 = 1376.
Okay, I think I'll go with this solution. It's pretty easy to develop an algorithm based on your suggestion. Thank you!