Basic Input/Output Problem

This code refers to a project that I am trying to complete regarding inputs/outputs. The problem reads as follows.

Suppose you have a list of N integers in a file*. You would like to read all the numbers, calculate
the mean (average) of the numbers, and output all these numbers along with their average to another file.

I keep getting an error stating: "invalid operands to binary expression ('std::__1::istream' (aka 'basic_istream<char>') and 'int *')" for line number 44 (I have identified below) of my code. I don't understand why this error keeps occurring as most of the code as written by my professor and deals with topics we have not covered yet. Any help is much appreciated!


#include <iostream>
#include <fstream>
using namespace std;
void readData (istream& is, int* &a, int size);
void printData (ostream& os, int *a, int size, string& name, double ave);
double findAverage(int *a, int size);
int main()
{
ifstream fin;
ofstream fout;
string fileNameI,fileNameO;
cout<<"enter input and output fileNames: ";
cin>>fileNameI>>fileNameO;
fin.open(fileNameI.c_str());
fout.open(fileNameO.c_str());
int size;

fin >> size;
int *a= new int[size];
readData(fin,a,size);
double ave = findAverage(a,size);
printData(cout, a, size, fileNameI, ave);
printData(fout, a, size, fileNameI, ave);

fin.close();
fout.close();
}
void readData (istream& is, int* &a, int size)
{
//ToDo
cin>>size;
cout<<"The number of integers you would like to take the average of = "
<<size;
cin>>a; <- LINE 44
cout<<"The numbers you are averaging are "<<a;
}
double findAverage(int *a, int size)
{
double ave=0;
double sum=0;
//ToDo
for (int i = 0; i < size; i++){
sum = sum + a[i];
}
ave = sum / size;
return ave;

}
void printData (ostream& os, int *a, int size, string& name, double ave)
{
//ToDo
cout<<"The array of integers you used were: "<<*a;
cout<<"The average of this array = "<<ave;
}
Last edited on
a is a pointer, i.e. it's an address to memory. Why are you trying to write user input to a pointer? Why do you use both fin and cin to try to write to size? It'a as if you can't make up your mind as to if you want user input, or file input.

What are you trying to do, in English, on line 44?
Are you trying to write to every element in the array?
If so, you have to write to each element in the array, individually.
1
2
3
4
for (int i = 0; i < size; i++)
{
    cin >> a[i];
}


Same with printing a -- write a for loop with a[i].

ALSO: PLEASE PUT CODE FORMATTING IN YOUR POST.
Edit your post and add [code] and [/code] around your code.
Last edited on
Topic archived. No new replies allowed.