C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 3rd, 2005, 07:24 AM
mysticorphan mysticorphan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 2 mysticorphan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 27 m 4 sec
Reputation Power: 0
adding digits for integer

I have a small problem.

I want to add digits together but not the integer value.
For example, if the digits are 1, 2, 3. i want d value 123. Or if the value is 12, 30, 15 i want 123015. any suggestions?

Reply With Quote
  #2  
Old April 3rd, 2005, 09:18 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 275 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 45 m 43 sec
Reputation Power: 4
Take the digits as strings of single character. Concatenate them using strcat().
Then use atoi() [in C/C++] or CInt() [in VC++]. For converting concatenated string as number.

Reply With Quote
  #3  
Old April 4th, 2005, 03:25 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
another way: for example, adding 1 + 2 = 12.
what you need to really do is (1*10) + 2. If you notice, you need to multiply the first number by the 10^(the number of digits in the second number). so you do you get that value? I did it for a project I'm working on by making a function called intpow():

Code:
 int intpow(int the_int)
 {
 	int i = 1;
 	while (the_int % i != the_int) i *= 10; //this is the important line, see below
 	return i;
 }
 


so let's have a look at that line.
while (the_int % i != the_int) i *= 10;
that would be read "if the_int modulus i does not equal the_int, multiply i by 10 and try again until it does."
if you are not familiar with the modulus(%) operator, what it does is perform division but returns only the remainder. so 5 % 2 would be 1.
If you play around with inputting different numbers and mentally walking through my function, you will notice that for a function with x number of digits, the number % 10^x (10 to the power of x) = the number.

if you are still confused, ask more questions and i'll help.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > adding digits for integer


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway