I basically need to read numbers from a file, where the first # incdicates how many numbers should be averaged together. And the program runs until all the numbers have been accounted for. I have a program written but for some reason, this program does not create an output file. It just prints out the numbers in my input file in secureshell. Please help!!
Line 33, 44: You're reading the input one character at a time. If you intend to average the numbers (per the instructions), you should be reading ints.
The instructions say the first number (4) is the number of values to be averaged, and to continue until all numbers are accounted for.
int main()
{
// Use the constructors instead of calling open.
ifstream in("lab6input.dat");
if(!in)
{
cerr >> "Couldn't open the input file.\n";
return 1;
}
ofstream out("lab6output.dat");
if(!out)
{
cerr >> "Couldn't open the output file.\n";
return 2;
}
add_plus_plus(in, out);
// No need to close the file streams, let the destructors do their jobs.
}
void add_plus_plus (ifstream& in_stream, ofstream& out_stream)
{ int count;
int num;
int sum;
double avg;
while (cin >> count) // Loop until no more counts are found
{ cout << "count: " << count << endl;
out_stream << "count: " << count << endl;
sum = 0;
for (int i=0; i<count; i++) // Loop count times to pick up numbers
{ in_stream >> num;
sum += num;
cout << num << endl;
out_stream << num << " ";
}
out_stream << endl;
// We've read all the numbers in the group
avg = (double)sum / count;
cout << "Average for group = " << avg << endl;
out_stream << "Average for group = " << avg << endl;
}
}