my code will read in a file and put the integers in the file into the array. It reads in and displays the integers perfectly, but i am stuck on how to find the factors of each number in the array. when i use my code i get the error: invalid operands to binary.

example:

4, factors = 3
6, factors = 4

``````#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX = 30;
void getfilename(string,string&);
void get_data(ifstream&, double[], int&);
double factor(double[], int);
int main()
{
int n;
double fac;
double list[MAX];
ifstream input;
string filename, file;
getfilename("input",filename);
input.open(filename.c_str());
get_data(input,list,n);
input.close();
fac = factor(list, n);
for (int j=0; j<n; j++)
{
cout << list[j] << right << setw(16) << fac << endl;
}

return 0;
}

double factor(double data[], int n)
{
int facts = 2;
for (int j=0; j<n; j++)
{
if (list[j]%2 == 0)
facts++;
}
return facts;
}
void getfilename(string filetype,string& filename)
{
cout << "Enter name of " << filetype << " file\n";
cin >> filename;
}
void get_data(ifstream& input,double list[],int& n)
{
n=0;
int count = 0;
input >> list[n];
while (!input.eof())
{
n++;
count++;
input >> list[n];
}

}``````
2
Contributors
1
2
Views
8 Years
Discussion Span
Last Post by StuXYZ

There are a few little errors:
(a) You have tried to write a factor that somehow works on the whole array.
(b) You have forgotten that if you get clever and add two factors because any number N, has factors 1 and N. Not to test them
(c) why does factor return a double?

``````double factor(double data[], int n)   // this should take a number N
{
int facts = 2;
for (int j=0; j<n; j++)        // THIS loop should be j=2;j<N;j++
{
if (list[j]%2 == 0)   // THIS is wrong
facts++;
}
return facts;
}``````