reading from file

I need help on my lab, i just cannot get for the life of me.
This is the question and what I have did so far. Help
Store the following data in a file:
5 96 87 78 93 21 4 92 82 85 87 6 72 69 85 71 81 73

Write a program to calculate and display the average of each group of numbers. The data are arranged in the file so that each group of numbers is preceded by the number of data items in the group.


Use eof() for termination of the file.
Also write the average of each group to an output file.
Prompt the user for file name.


#include <iostream>
#include <fstream>
using namespace std;

int main ()
{

string filename;
int input1;
double avg1,avg2,avg3;

cout<<"Enter the name of the input file"<<endl;
cin >>filename;
ifstream inputfile;
inputfile.open(filename.c_str());
ofstream outfile ("lab7.out", ios::out);


if (!inputfile)
cout << "Error opening file" << endl;
else
{
inputfile>> input1;


while (!inputfile.eof())
{
switch (input1)
{
case 5: avg1 = input1/5;
outfile << "group 5 avg. "<< endl;
cout << "group 5 avg. " << endl;
break;
case 4: avg2 = input1/4;
outfile << "group 4 avg. "<< endl;
cout << "group 4 avg. " << endl;
break;
case 6: avg3 = input1/3;
outfile << "group 6 avg. "<< endl;
cout << "group 6 avg. " << endl;
break;

inputfile>> input1;
}
}
}

return 0;
}
use code tags for further posts. I thought it was ofstream if you wanted to write to a file, not ifstream?
I have something like this:
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
string path = "c:/hello.txt";
string line;


ofstream myfile;

myfile.open(path.c_str()); //create that file

 myfile << "Hello" << endl;

 myfile.close();



ifstream myfilx (path.c_str());

 if (myfilx.is_open())

 {

   while (! myfilx.eof() )

    {

     getline (myfilx,line);

     cout << line << endl;

      myfilx.close();
}
}
Last edited on
your right. im reading the values in from another file using ifstream, and i am outputting the results via ofstream to a new file.
Topic archived. No new replies allowed.