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 20th, 2006, 10:43 PM
Harbinger Harbinger is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 3 Harbinger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 16 sec
Reputation Power: 0
Please. I need some help with a function.

I have asked my C++ instructor about it and he confused me throughly.

I have a int value. For example 35. I need a function that can add the digits of the number together. So another value will be 3+5 or 8. I have thought of converting it to String and reading each digit as a character but it became very messy and did not work properly. I would appreciate some direction.

Reply With Quote
  #2  
Old April 21st, 2006, 06:51 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Something like this?

You'll only need the add_digits() function, the rest is just a demonstration.

C Code:
Original - C Code
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int add_digits(int input)
  5. {
  6.         if(input < 10)
  7.                 return input;
  8.         else
  9.                 return (input % 10 + add_digits(input / 10));
  10. }
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.         /*      Declarations    */
  15.         int number, i;
  16.  
  17.         /*      See if command line parameter set       */
  18.         if(argc > 1)
  19.         {
  20.                 for(i = 1; i < argc; ++i)
  21.                 {
  22.                         number = atoi(argv[i]);
  23.                         printf("digits for %d add up to %d\n", number, add_digits(number));
  24.                 }
  25.         }
  26.         else
  27.         {
  28.                 number = 35;            /*      Your example    */
  29.                 printf("digits for %d add up to %d\n", number, add_digits(number));
  30.         }
  31.  
  32.  
  33.         /*      Exit Succes     */
  34.         return 0;
  35. }


If you compile this and run it from the command line, you can add any number of (integer) parameters, and it'll perform the function on them.
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 280

Reply With Quote
  #3  
Old April 23rd, 2006, 09:50 PM
Harbinger Harbinger is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 3 Harbinger User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 16 sec
Reputation Power: 0
Thankyou so much!! I can't believe I never thought of that!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Please. I need some help with a function.


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 3 hosted by Hostway
Stay green...Green IT