0

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;
}
Shajal Ahamed
  • 121
  • 1
  • 15
  • We requires everyone to provide a [mcve]. Your example with a lot of error-checking code is not a [mcve], and what is your file? – user202729 Apr 08 '18 at 02:51
  • (you can learn array yourself.) – user202729 Apr 08 '18 at 03:07
  • Could you please state the particular problem that you are having. As it stands there isn't a question. – Jade Apr 08 '18 at 03:53
  • Also, I think that you will find that it will take way less time if you get rid of al the duplicate code. If you find a bug or need to add something, you will have to you will have to do it 8 times. The techniques you will learn getting rid of the duplicates will serve you well for the rest of the class and the rest of your career. – Jade Apr 08 '18 at 03:57
  • You forgot to ask a question... – BessieTheCookie Apr 08 '18 at 16:53
  • If your professor taught you to write `while(!fromFile.eof())` which is [wrong](https://stackoverflow.com/q/5605125/9254539), you're a victim of bad C++ teaching and you could benefit a lot from a [good book](https://stackoverflow.com/q/388242/9254539). – BessieTheCookie Apr 08 '18 at 16:55

1 Answers1

-1

Do the following changes, and ask freely if you don't understand anything:

#include <iostream>
#include <fstream>
using namespace std;
int main()
    {
    //MY VARIABLES
    int number, max, min, sum;

    //OPENING FILES
    ifstream fromFile;
    ofstream toFile;
    toFile.open("SecondFile.txt"); //Don't overwrite the same file
    fromFile.open("NumberFile.txt");
    //IF FILE DIDN'T OPEN SUCCESSFULLY
    if (!fromFile)
    {
        cout << "There was an error" << endl;        
        exit(1);
    }
    //IF FILE OPENED SUCCESSFULLY
    else
    {
        cout << "No error, file opened successfully" << endl;
        toFile << "This file contains the calculations for this project" << endl;
    }                                                                     
    while (!fromFile.eof())   //WILL LOOP UNTIL IT HAS READ EVERY NUMBER FROM THE FILE
    {  
        for(int i=1; i<=8; i++){        //LOOP for counting rows
            cout <<"\nLine "<<i<<": ";
            int sum = 0;
            max = 0;
            min = 0;
            int j=1;
            do
            {
                fromFile >> number;
                if(max == 0 && min == 0){
                    max = number;
                    min = number;
                }
                if(number > max){
                    max = number;
                }
                else if(number < min){
                    min = number;
                }
                toFile << number << " ";
                cout << number << " ";
                j++;
                sum += number;
            } while (j <= 7);
            toFile << "Max: "<<max<<", Min: "<<min<<", Sum: "<<sum<<", Average: "<<sum/7<<endl;

        }
     }
    //CLOSES FILES
    toFile.close();
    fromFile.close();
    return 0;
}

Input File:

enter image description here

Output File:

enter image description here

M Umer
  • 75
  • 2
  • 10