Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help in a puzzling C++ problem.
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Caeliferum




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
r.3volved




PostPosted: Wed Oct 25, 2006 6:04 pm   Post subject: (No 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
md




PostPosted: Wed Oct 25, 2006 7:22 pm   Post subject: (No 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.
bugzpodder




PostPosted: Wed Oct 25, 2006 7:37 pm   Post subject: (No subject)

isn't long samething as int nowadays? do you want the leftmost non-zero digit or leftmost digit?
md




PostPosted: Wed Oct 25, 2006 8:03 pm   Post subject: (No 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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: