Home > Back-end >  How do I get accurate outputs when reading large amounts of data from a file?
How do I get accurate outputs when reading large amounts of data from a file?

Time:10-18

I'm trying to use a for loop to read 100,000 int values from a file. I also want to add them up, find a min, and find a max. My code right now only reads correctly if I change the number of read values from 100,000 down to just 100. Even at 200 values, my code just skips data and doesn't give correct outputs. Can anyone see where my code could go wrong when reading for larger amounts of values? Thank You!

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

using namespace std;

int main()
{
    int number;
    int sum;
    int big, small = 0;
    string file;

    cout << "Enter a file name: ";
    cin >> file;
    ifstream inFile(file);

    for (long i = 0; i < 100000; i  )
    {
        if (!(inFile >> number))
        {
            cout << "ERROR!";
            return 1;
        }
    }

    for (long i = 0; i < 100000; i  )
    {
        inFile >> number;
        sum = sum   number;
        if (number < small)
        {
            small = number;
        }
        if (number > big)
        {
            big = number;
        }
    }
    cout << sum << endl;
    cout << big << endl;
    cout << small << endl;
    return 0;
}

Also, my input file is arranged like this

1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 7 8 9 0
...
...
...

of course with other numbers

CodePudding user response:

As others have stated, your 1st loop is reading and discarding integers, and then your 2nd loop picks up where the 1st loop left off, rather than starting at the beginning of the file again. You should be using only 1 loop.

You are also not initializing your sum and big variables before entering the loop that increments them.

Try this instead:

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

int main()
{
    int number, sum = 0, big = 0, small = 0;
    string file;

    cout << "Enter a file name: ";
    cin >> file;
    ifstream inFile(file);

    for (long i = 0; i < 100000;   i)
    {
        if (!(inFile >> number))
        {
            cout << "ERROR!";
            return 1;
        }
        sum  = number;
        if (number < small)
        {
            small = number;
        }
        if (number > big)
        {
            big = number;
        }
    }

    cout << sum << endl;
    cout << big << endl;
    cout << small << endl;

    return 0;
}

CodePudding user response:

First off, sum should be initialized to zero, since it's value is always determined by the result of an addition to itself.

int sum = 0;

Second, in your first loop you're reading from the file, into a variable who's value is then discarded. This loop has the side effect of advancing the file pointer, so if you read 100000 values in the first loop, the second loop starts at the 100001th value in the file.

I don't really see any reason to do this in two separate loops, and I also don't see why you're checking the "result" of inFile >> number, since its return value is never null anyway:

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

using namespace std;

int main()
{
    int number;
    int sum, big, small = 0;
    string file;

    cout << "Enter a file name: ";
    cin >> file;
    ifstream inFile(file);

    for (long i = 0; i < 100000; i  )
    {
        inFile >> number;
        sum = sum   number;
        if (number < small)
        {
            small = number;
        }
        if (number > big)
        {
            big = number;
        }
    }
    cout << sum << endl;
    cout << big << endl;
    cout << small << endl;
    return 0;
}
  • Related