![]() |
|
#1
|
|||
|
|||
Integer splitting?Can anybody help me with this problem? Or is there a better way of doing it then I am thinking of?
I want to split up an integer into each individal unit and then store it in an array. Basically I need to get the user to enter a whole number and then put each individual unit of that number in a separate variable. Can anybody help?? Thankyou |
|
#2
|
|||
|
|||
|
Cast it into a string, then store the string in an array, and then access each array position?
|
|
#3
|
|||
|
|||
|
Quote:
Wouldn't the array then contain chars though? I forgot to mention that I wanted the array to contain integers. |
|
#4
|
|||
|
|||
|
AFAIK, you CAN store numbers
|
|
#5
|
||||
|
||||
|
Quote:
Hey, I think DWK may have a pretty good idea here. If you do an atoi call on it and get an array of chars, then go through the array and subtract each number by 48, you will have the digits. I have a function that you can modify to do it directly, but I think it will be much easier doing it DWK's way. My function is an atoi replacement and you could modify that not to convert the digit into a char. Let me know if you still want to take a look at it. |
|
#6
|
|||
|
|||
|
But you are doing it ASCII way, and I assume the way of converting would be much faster (and easier)
(By the way....I got stuck again, dsmith so if you are kind drop by my post |
|
#7
|
||||
|
||||
|
Tealion, I am afraid that I gave you some bad advice
CPP / C++ / C Code:
Its kind of ugly and for what you want to do, can be cut down considerably. If you have any trouble with this let me know... |
|
#8
|
|||
|
|||
|
Oh yes it can be cut down :S
I think all you need is 2 cycles: one that fits the number into the array and casts it, and the other that just returns them one by one __________________
Just trying to be a programmer :P |
|
#9
|
||||
|
||||
|
Quote:
Actually, it can't be cut down that much. You need to think about how variables are really stored in C. If I do a typecast of: CPP / C++ / C Code:
If I do: CPP / C++ / C Code:
So, in order to place an integer into a charectar (or digit array), you must do something like I posted. Now, some compilers have a function called itoa that will convert an integer to an ascii string, which is the inverse of the itoa. And there may be other more elegant approaches than mine (I'm sure of it.) But no matter what language you use, somewhere in the background there are functions like these that make it appear that you are doing a typecasting when you are really not. Now, the stuff that can be taken out of the routine for what tealion is doing is the negative tracking (I assume), the conversion from an integer to an ascii (adding by 48 ) and the final placement into a properly sized string. Then change it to return a integer array instead of a char *. Once again, Tealion, if you want to use this, but are struggling with modifiying it, let me know. |
|
#10
|
|||
|
|||
|
Quote:
I need help! It also needs to be in C, not C++ |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The