Addition of digits in five digit number as an character array.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhinuke
    New Member
    • May 2010
    • 23

    #1

    Addition of digits in five digit number as an character array.

    Been brushing up my C,C++ for my new venture in Graduate Studies for this fall.I am doing basic programs in which I am trying this one right now.

    A 5-digit positive integer is entered through the keyboard, write a function to calculate sum of digits of the 5-digit number:
    (1) Without using recursion
    (2) Using recursion



    I am entering five digit number as a character array and I am trying to add them.
    (I found it clumsy to construct conventional method using %,/ operators to implement in functions.)

    I am trying hands on various functions of string handling to add the digits but no success.Another thing my recursive function 'sumr' is not giving any output on terminal and 'sumd' is giving output as 20 however may be the digit entered.

    I am using Netbeans.Here is the code snippet that I have created.



    Code:
    #include <stdio.h>
    #include<ctype.h>
    #include<stdlib.h>
    main()
    {
        char n[5];
        int sum,csum;
        printf("Enter 5 digit number\n");
        gets(n);
        int sumd(char *);
        int sumr(char *);
        sum=sumd(&n[0]);
        printf("\nThe sum of the digits without recursion is %d\n",sum);
        csum=sumr(&n[0]);
        printf("\nThe sum of digits with recursion is %d\n",csum);
        return 0;
    }
    int sumd(char *p)
    {
        int j,sum=0;
        for(j=0;j<5;j++)
        {
            sum=sum+(isdigit(*p));
            p++;
        }
        return sum;
    }
    int sumr(char *q)
    {
        int gsum;
        gsum=(isdigit(*q))+sumr(q++);
        return gsum;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Two things.

    One this is C code. There's no C++ here. Use cout rather than printf.

    Two, for a recursive function to work it hass to stop calling itself at some point and return. sumr() has no such descision. You call call yourself forever and blow the stack. Something liker:

    Code:
    int sumr(int sum, etc...)
    {
         Is this the 6th call?
         If so return sum.
         Otherwise, keep going
    
    }

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      1. I think printf() is perfectly acceptable in C++ so you can continue to use it.
      2. I wonder why you don`t use % and / and sum+= to get the sum of the digits?

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Your character array is too small to hold the entered 5-digit number. At the very least, you need to allow space for the terminating null. Furthermore, you should add some additional extra space to allow for somebody entering a few too many characters. Fundamentally, gets is a poor choice for data entry because there is no way to protect your program from overflow.

        Comment

        • abhinuke
          New Member
          • May 2010
          • 23

          #5
          Originally posted by whodgson
          1. I think printf() is perfectly acceptable in C++ so you can continue to use it.
          2. I wonder why you don`t use % and / and sum+= to get the sum of the digits?


          Thanks...I guess I ll go away with workable method of / and %.But still I wasnt able to work with pointers and char array......

          Comment

          • abhinuke
            New Member
            • May 2010
            • 23

            #6
            Originally posted by donbock
            Your character array is too small to hold the entered 5-digit number. At the very least, you need to allow space for the terminating null. Furthermore, you should add some additional extra space to allow for somebody entering a few too many characters. Fundamentally, gets is a poor choice for data entry because there is no way to protect your program from overflow.
            I was putting 5 digit number as a string.Isn't gets() a standard method to get the string in the program......

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              True, gets is in the Standard C Library, but it is deprecated; for example, look here.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by whodgson
                I think printf() is perfectly acceptable in C++ so you can continue to use it.
                It only works for built-in types and only works for pre-defined display formats and only works for stdout. When these are in C++ code they have to be ripped out at the first requirement change. Like you suddenly want you use a file instead so you switch to fprintf(). Instead, there should have been a function written with a stream reference as an argument to avoid this. This function could acess a paraneters table to obtain variable display formats from one execution of the program to the next. Or may the whole thing is handled by a class hierarchy.

                I say again, the printf() family of functions is a very poor choice for a C++ program.

                Comment

                Working...