My assignment is as follows: I am given a text file with 8 rows of numbers containing 7 numbers in each row. I am to take these numbers, read and transfer them into a new file. Then taking data from new the rows of new file, I have to perform the following actions:
Find the highest and lowest number within each set of 7 numbers.
My professor has taught us up to basic file handling. I just know how to use loops, if and else, and the extent of the file handling you see within this code.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//MY VARIABLES
float avg;
int number, line, max, min;
int counter = 1;
int sum;
//OPENING FILES
ifstream fromFile;
ofstream toFile;
toFile.open("NumberInFile.txt");
fromFile.open("NumberFile.txt");
while (!fromFile.eof()) // READ EVERY NUMBER FROM THE FILE
{
if (counter <= 7) //LINE #1
{
cout << "Line 1: ";
int sum = 0;
do
{
fromFile >> number;
toFile << number << " ";
cout << number << " ";
line = 1;
counter++;
sum += number;
} while (counter <= 7);
toFile << "\n";
}
else if (counter > 7 && counter <= 14) //LINE #2
{
cout << "\nLine 2: ";
do
{
fromFile >> number;
toFile << number << " ";
cout << number << " ";
line = 2;
counter++;
} while (counter > 7 && counter <= 14);
toFile << "\n";
}
else if (counter > 14 && counter <= 21) //LINE #3
{
cout << "\nLine 3: ";
do
{
fromFile >> number;
toFile << number << " ";
cout << number << " ";
line = 3;
counter++;
} while (counter > 14 && counter <= 21);
toFile << "\n";
}
}
//CLOSES FILES
toFile.close();
fromFile.close();
system("pause");
return 0;
}