multiply the odd numbers from a file

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));
}


int main()
{
ifstream file ("intrare.in.txt");

if(file.is_open())
{
while(file >> id)
{
cout<< id <<" ";
num=num+1;

}
cout<< endl;
cout<< num<<endl;
file.close();
}
cout<< recursive(id);

return 0;
}

These are the numbers from the file: 7 3 4 2 1 8 6 5 0 9

It gives me results like 0 and 9

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".
Last edited on
Two tips:
1. Print in functions (or use debugger) to see where you are going
2. Please use code tags when posting code. See https://www.cplusplus.com/articles/jEywvCM9/

Your program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <sstream>
#include <string>

using namespace std;
int num=0;
int id;

int recursive(int id)
{
  if ( id==0 )
  {
    cout << "Return 0\n";
    return 0;
  }

  if ( id%2!=0 )
  {
    cout << "Calculating " << id << " + recursive(" << id-1 << ")\n";
    return (id + recursive(id-1));
  }

} // warning: control reaches end of non-void function [-Wreturn-type]

int main()
{
  string data( "7 3 4 2 1 8 6 5 0 9" );
  istringstream file (data);

  //if(file.is_open())
  {
    while(file >> id)
    {
      cout<< id <<" ";
      num=num+1;
    }
    cout<< endl;
    cout<< num<<endl;
    //file.close();
  }
  cout << "Calling recursive(" << id << ")\n";
  cout<< recursive(id);

  return 0;
}

7 3 4 2 1 8 6 5 0 9 
10
Calling recursive(9)
Calculating 9 + recursive(8)
6296832 

When you have read the file you only have one int. The last value of the file. 9

The second call to recursive() gets thus value 8. It is not 0. It is not odd.
What does the function return in that case? Whatever.

If the last value of the file were even (not 0), there would be only one call to recursive() and the result would directly be whatever.

One way to do this using recursion could be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <fstream>
#include <iostream>

int recfun(std::istream& ifs)
{
	int num {};

	return (ifs >> num) ? ((num % 2) ? num * recfun(ifs) : recfun(ifs)) : 1;
}

int main()
{
	std::ifstream ifs("intrare.in.txt");

	if (!ifs)
		return (std::cout << "Cannot open file\n"), 1;

	std::cout << recfun(ifs);
}


Using the given data file displays:


945

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

long long oddProduct( istream &in )
{
   bool found = false;
   long long product = 1;
   for ( int num; in >> num; )
   {
      if ( num % 2 ) 
      {
         product *= num;
         found = true;
      }
   }
   if ( !found ) return 0;
   else          return product;
}


int main()
{
   istringstream A( "7 3 4 2 1 8 6 5 0 9" );
   istringstream B( "2 4 6 8 10" );
   
   cout << oddProduct( A ) << '\n';
   cout << oddProduct( B ) << '\n';
}


Recursion just doesn't seem sensible here.
Topic archived. No new replies allowed.