-5

Why is it that the cout statement in the recursive function is printing 15, though the one right outside the recursive function (in main) is printing 0. I set num = recursive_function().

Update: I was a noob and returned 0 in the said function. The issue is resolved. Thank you.

#include <iostream>
#include <string>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int sumNumbers(int sum, vector<int> numbers, int count) 
{
    if ( count == numbers.size() ) {
        cout << "The sum of the numbers in the file is: " << sum << endl; - 1st cout statement
        return sum;
    }

    return sumNumbers(sum + numbers[count], numbers, count + 1);


}

int main(int argc, char* argv[]) 
{
    vector<int> numbers;
    int numElements = 0;; 
    int sum;

    string fileName = argv[2];

    ifstream ifile(fileName);

    if( ifile.fail() ) {    
        cout << "The file could not be opened. The program is terminated." << endl;
        return 0;
    }

    int new_number;

    while (ifile >> new_number) numbers.push_back(new_number);

    sum = sumNumbers(sum, numbers, 0);

    cout << "The sum of the numbers in the file is: " << sum << endl; - 2nd cout statement

    return 0;
}

output:

The sum of the numbers in the file is: 15
The sum of the numbers in the file is: 0
  • 1
    Because `sumNumbers` returns `0`. – juanchopanza Aug 04 '14 at 07:58
  • `return 0;` might have something to do with it – stijn Aug 04 '14 at 07:58
  • 1
    You return 0, honestly what do you expect? – kviiri Aug 04 '14 at 07:58
  • Not to mention you never initialise `sum` before calling `sumNumbers`, this is undefined behaviour. – user657267 Aug 04 '14 at 07:59
  • @user657267 are you sure? I mean, the value of sum is undefined in the sense that it can be anything, but I'm not sure this falls under UB (which is tyically something like accessing objects after being deleted etc)? – stijn Aug 04 '14 at 08:00
  • @stijn trying to use the value of an uninitialised variable is perhaps the most common example of UB. The value of `sum` is accessed when attempting to copy it for the `sumNumbers` argument. – user657267 Aug 04 '14 at 08:02
  • passing a copy shouldn't matter, because I set sum equal to the function. – Rafael Vergnaud Aug 04 '14 at 08:09
  • also, the return 0 statement is never reached -- that is certainly not why it is acting the way it is. I took off the return 0, same thing is happening. – Rafael Vergnaud Aug 04 '14 at 08:10
  • @user657267 correct indeed, this covers it http://stackoverflow.com/questions/4279264/is-reading-an-indeterminate-value-undefined-behavior – stijn Aug 04 '14 at 08:11
  • @RafaelVergnaud `sum` is assigned *when the function returns*, the mere act of calling `sumNumbers` with an uninitialised variable is undefined behaviour, but hey feel free to ignore helpful advice. – user657267 Aug 04 '14 at 08:14
  • BTW, you may write `const int sum = std::accumulate(numbers.begin(), numbers.end(), 0);` – Jarod42 Aug 04 '14 at 08:15
  • @user657267 How do I fix that? I don't quite understand. Sorry if I am a little slow. I'm just learning c++. New to it. – Rafael Vergnaud Aug 04 '14 at 08:28
  • @RafaelVergnaud `int sum = 0;` makes sure that `sum` is initialised. – user657267 Aug 04 '14 at 08:28

1 Answers1

1

It's because your sumNumbers method has return 0 at the end of it.

Simply remove that and put return sumNumbers(sum + numbers[count], numbers, count + 1); at the end of it instead.

Carl Burnett
  • 171
  • 1
  • 8