Hello,

I'm working on an assignment that requires me to do the following:

"A file contains 6 numbers per line and contains several records. Another file contains 3 numbers per line and several records. Write a program to input each of the numbers, find the highest number, the lowest number, their total and average. Output the numbers, the highest, lowest, total and average to another file."

Output.txt needs to look like this for each numbers per line:

1 2 3 4 5 6
Min: 1
Max: 6
Total: 21
Avg: 3

The problem I'm having with my code is instructing the computer to read the numbers per line, find the desired math results, output it the aforementioned format, and then repeat the algorithm in the next set of numbers. This is my code so far:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main ()
{
    //Declaring variables
    ifstream inFile;
    ofstream outFile;
    int NumPerLine, num, count=0; 
    int sum=0, min=0, max=0, Total=0, Avg=0;
    int row=0, col=0, x, y;

    //Opening files
    inFile.open("input1.txt");
    outFile.open("output.txt");

    //************************
    //Code data manipulation
    //************************

    cout << "Processing data."
         << endl;

    //Read/Write Integer 
    inFile  >> NumPerLine;
    outFile << NumPerLine
            << endl;

    //While loop reads data  
    while (!inFile.eof())
    {
        inFile  >> num;
        outFile << num << "\t";
        sum = sum + num;
        count++;
    }//Needs to be solved: how do I run math processes(max,min,avg,total)w/ numbers
     //at end of line and THEN continue reading and repeating math processes for next line of numbers?

     /*Math processes 

     //Determining Average
       Avg = sum / count;
     //Determining Sum
       sum = num + sum;
     //Determining Max
       if(x>y)
                max=x;
                else
                max=y;
     //Determining Min
       if(x<y)
                min=x;
                else
                min=y;*/

     //************************
     //End of code data manip
     //************************

    //Close files
    inFile.close();
    outFile.close();

    return 0;

Please help! Thank you.

Edited 4 Years Ago by greencode

You need to first read the entire line as a single string, and then read off the individual numbers in it.

Because, when you read numbers from the file, it will ignore any non-number character (including the new-line character) that it encounters. So, there is no way to tell when the line ends. So, you have to first use another method to read the entire line, then read off the numbers from that.

To read one line from the file, you can use a string and the getline function, as so:

#include <string>
// .. same as in your code...

int main() {
  // ... 

  string oneLine;
  getline(inFile, oneLine);
  cout << "The first line is:" << endl;
  cout << oneLine << endl;

  //..
};

Once you have an entire line read from the file, you have to read the individual numbers. To read numbers from a string, you can use a class called stringstream (in header <sstream>) which acts the same as a file-stream class. So, you can do this:

#include <sstream>
// .. same as before ..

int main() {
  //...

  // after reading a line into 'oneLine':
  stringstream inStrStream(oneLine);  // create string-stream from the line-string.
  cout << "The individual numbers are:" << endl;
  while(!inStrStream.eof()) {
    inStrStream >> num;
    cout << num << endl;
  };

  // ..
};

I think you can figure out how to use the above codes to do your assignment.

I'm sure stringstreams are not available for you, so you probably need to use a more mundane approach.

But it depends entirely on if you use C++ strings or C-strings.

This article has been dead for over six months. Start a new discussion instead.