Command line recursion

This is a discussion on Command line recursion within the C++ Programming forums, part of the General Programming Boards category; Here is my code. I was wanting to sum integers from the command line and give the suma s output. ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Angry Command line recursion

    Here is my code. I was wanting to sum integers from the command line and give the suma s output. The code doe compile but gives me a segmentation fault if I put in more than two arguements for argc. The first arguement is the name of the program. I am not sure if I did my sum procedure correctly to make sure I did not include the first arguement(the program name) in the actual sum-value.
    Any suggestions is very helpful. Thanks

    #include <iostream.h>
    #include <stdlib.h>

    int sum(int ac, char *av[]){
    int x = 0;
    if (ac==0){
    return 0;
    }

    else if (ac==1){
    cout << "DONE!" << endl;
    }

    else {
    x = (atoi(av[ac]) + sum(ac-1, av));
    }
    return x;
    }

    int main(int argc, char *argv[]){
    int y=0;
    y = sum(argc, argv);
    cout << "Sum is" << y << endl;
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    3
    I fixed it. thanks anyways guys

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    no problem, I'm glad we could help you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 08:45 PM
  2. convert Recursion to linear can it be done
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 02:58 AM
  3. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  4. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 09:56 AM
  5. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 08:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21