Old Feb 21, 2004 | 12:32 AM
  #3  
aznd000d's Avatar
aznd000d
yoooooooooooo
 
Joined: Oct 2003
Posts: 148
Likes: 0
From: Riverside, CA
Default

well, I have to write a program that will take info from the input.txt file and output it to either groupA.txt or groupB.txt files.

the input.txt file looks like this:
A 4.45 3.56 4.12 3.78 4.24 3.96 4.13 4.05 3.68 4.34
B 4.10 4.24 3.78 3.94 4.37 4.06 4.28 3.99 4.00 3.85
B 3.69 3.82 4.56 4.89 4.67 4.04 4.37 3.88 3.89 4.56
A 4.61 4.50 4.63 4.28 3.71 3.75 3.93 4.62 4.48 4.04
A 3.96 4.13 4.05 3.68 4.34 3.69 3.82 4.56 4.89 4.67
A 4.06 4.28 3.99 4.00 3.85 3.72 4.18 4.25 3.58 4.38
B 3.75 3.93 4.62 4.48 4.04 4.45 3.56 4.12 3.78 4.24
A 4.04 4.37 3.88 3.89 4.56 4.61 4.50 4.63 4.28 3.71
B 3.72 4.18 4.25 3.58 4.38 4.13 3.96 4.37 4.49 3.62
A 4.45 3.56 4.12 3.78 4.24 3.75 3.93 4.62 4.48 4.04
B 3.75 3.93 4.62 4.48 4.04 3.96 4.13 4.05 3.68 4.34
B 4.04 4.37 3.88 3.89 4.56 3.78 4.24 3.96 4.13 4.05

The letters infront of each line represent each type of info.
lines staring with A should be gathered into groupA.txt file and calculate it's average, and the same thing for lines starting with B.

the output files should like this:
groupA.txt
4.45 3.56 4.12 3.78 4.24 3.96 4.13 4.05 3.68 4.34 AVG: 4.031 ounces
4.61 4.50 4.63 4.28 3.71 3.75 3.93 4.62 4.48 4.04 AVG: 4.255 ounces
3.96 4.13 4.05 3.68 4.34 3.69 3.82 4.56 4.89 4.67 AVG: 4.179 ounces
4.06 4.28 3.99 4.00 3.85 3.72 4.18 4.25 3.58 4.38 AVG: 4.029 ounces
4.04 4.37 3.88 3.89 4.56 4.61 4.50 4.63 4.28 3.71 AVG: 4.247 ounces
4.45 3.56 4.12 3.78 4.24 3.75 3.93 4.62 4.48 4.04 AVG: 4.097 ounces

groupB.txt
4.10 4.24 3.78 3.94 4.37 4.06 4.28 3.99 4.00 3.85 AVG: 4.061 ounces
3.69 3.82 4.56 4.89 4.67 4.04 4.37 3.88 3.89 4.56 AVG: 4.237 ounces
3.75 3.93 4.62 4.48 4.04 4.45 3.56 4.12 3.78 4.24 AVG: 4.097 ounces
3.72 4.18 4.25 3.58 4.38 4.13 3.96 4.37 4.49 3.62 AVG: 4.068 ounces
3.75 3.93 4.62 4.48 4.04 3.96 4.13 4.05 3.68 4.34 AVG: 4.098 ounces
4.04 4.37 3.88 3.89 4.56 3.78 4.24 3.96 4.13 4.05 AVG: 4.090 ounces

-----------------------------------------------------------------------------------
ok, this is what I wrote, it compiles, but it doesn't run correctly.

Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main ()
{

     ifstream fin;
     ofstream fout;

     fin.open ("input.txt");
     if (fin.fail())
          {
          cout << "File 'input.dat' failed to open.";
          exit (1);
          }

     double weight;
     int count = 0;
     double sum = 0;
     double average;
     char group;


     while (!fin.eof())

          {
          fin >> group;

          if (group == 'A')

               {
               fout.open("groupA.txt");
                    while (count < 10)

                         {    
                         fin >> weight;

                         sum = sum + weight;
                         fout << weight << " ";
                         count = count + 1;
                         }
              average = sum / count;
              fout <<"average = " << average <<endl;

              count = 0;
              sum = 0;
              }




           if (group == 'B')
                {
                fout.open("groupB.txt");
                     while (count < 10)
                     {
                     fin >> weight;
                     fout << weight << " ";
                     sum = sum + weight;
                     count = count + 1;
                     }

                average = sum / count;
                fout  << "Average = " << average << endl;

                count = 0;
                sum = 0;
                }
            }


     fin.close();
     fout.close();
     fout.close();
}
Reply