Computer Science Canada Need help in a puzzling C++ problem. |
Author: | Caeliferum [ Wed Oct 25, 2006 5:23 pm ] |
Post subject: | Need help in a puzzling C++ problem. |
This is from my highschool course, btw. I'm required to read in a user's input in the form of an unsigned long. From this input, I must output each digit within the number from left to right on a separate line. The problem here is that loops and condition statements must play an integral part to the execution. I'm not allowed to use vectors or arrays for this problem (the class has only covered input/output and variable basics and are currently discussing loops and condit. statements), which would seem to be the most viable solution to integrating a loop. Any suggestions, guys? It should be able to be done with relatively basic methods. |
Author: | r.3volved [ Wed Oct 25, 2006 6:04 pm ] |
Post subject: | |
I'd say you would read in a ULONG and either do the mod trick to extract the digits, or (simpler idea but not as efficient) convert them to a string and substring through the input from element 0 to input.length |
Author: | md [ Wed Oct 25, 2006 7:22 pm ] |
Post subject: | |
reading an unsigned long is easy std::cin >> unsigned long; Getting the digits is also pretty usea, X % 10 will give you the least significant digit, and X / 10 will then remove the last digit. With loops that's all you need. |
Author: | bugzpodder [ Wed Oct 25, 2006 7:37 pm ] |
Post subject: | |
isn't long samething as int nowadays? do you want the leftmost non-zero digit or leftmost digit? |
Author: | md [ Wed Oct 25, 2006 8:03 pm ] |
Post subject: | |
bugzpodder wrote: isn't long samething as int nowadays? do you want the leftmost non-zero digit or leftmost digit?
long and int are the same on most 32bit CPUs, that's not guaranteed though. It's best to use long when you want long, and int when you don't care. Just in case something changes. |