hi, I am trying to multiply the odd numbers from a file using a recursive function and I don't quite know how, I've tried several times but it just doesn't work
This is what I tried:
#include <iostream>
#include <fstream>
using namespace std;
int num=0;
int id;
int recursive(int id)
{
if(id==0)
return 0;
if(id%2!=0)
return (id +recursive(id-1));
}
If anything you appear to be trying to add things, not multiply them. in your routine recursive(). Also, it doesn't return anything if id is positive and even.
What answer do you expect from your file
7 3 4 2 1 8 6 5 0 9
and what do you think a recursive function would do if all entries in that file were even?
Actually, why do you need a recursive function here at all - it seems particularly ill-suited if you genuinely want to "multiply the odd numbers in a file".