1

I'm a first year student and im busy with a coding exercise but cant seem to crack the case.

when reading from number.dat which is 20 random numbers, i get them read into the array and I print them just for diagnostics at this stage but my print of the data in the array is completely messed up.

Any help is much appreciated.

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    ifstream in_stream;
    ofstream out_stream;

    int array_size = 20;
    int position = 0;
    double numbers[array_size];

    //Check input and output file.
    in_stream.open("Number.dat");
    if (in_stream.fail())
    {
        cout << "Input file opening failed";
        exit(1);
    }

    //array to read data from array as long as we dont reach the end of file marker
    while(! in_stream.eof() && position < array_size)
    {
        in_stream >> numbers[position];
        position++;
        cout << position << " = "
             << numbers[position] << endl;
    }

}
Peanut
  • 33
  • 2
  • 10
  • 2
    `in_stream >> numbers[position];` - wouldn't it be great to know whether or not that actually *succeeded* ? – WhozCraig Feb 10 '15 at 20:20
  • well... it seems to work but the output is not correct – Peanut Feb 10 '15 at 20:21
  • 1
    You may want to read through this as well. http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong Funny how you put "seems to work" and incorrect output in the same sentence... Can you post an example of what the file you are reading looks like in the question? – Retired Ninja Feb 10 '15 at 20:22
  • 2
    You are putting the number into the array then incrementing, then printing the next number in the array, which hasn't been read yet, put your increment (position++) after your cout. – Mo H. Feb 10 '15 at 20:22
  • Thank You muhammed, this sorted out my issue – Peanut Feb 10 '15 at 20:29

3 Answers3

5

There are two problems, one that's not the cause of this particular problem.

First, using while (!instream.eof()will cause you to read one time too many, because eof() doesn't become true until after the first attempted read after the last successful one.

But the immediate cause is that you're incrementing position between the reading and the printing, so you're printing a value that hasn't been set yet.

Try this:

while (position < array_size && in_stream >> numbers[position])
{
    cout << position << " = "
         << numbers[position] << endl;
    position++;
}
molbdnilo
  • 55,783
  • 3
  • 31
  • 71
1

You are reading parts of your array that haven't been filled yet, so you should be getting junk, try this:

    while(! in_stream.eof() && position < array_size)
    {
        in_stream >> numbers[position];
        cout << position << " = "
             << numbers[position] << endl;
        position++;
    }
Mo H.
  • 1,726
  • 1
  • 18
  • 27
1

as per the answers above , my issue was that i was incrementing my loop variable before printing, so it tried to print the next value in the array.

Thank you all for helping

while(! in_stream.eof() && position < array_size)
{
    in_stream >> numbers[position];
    cout << position << " = "
         << numbers[position] << endl;
    position++;
}
Peanut
  • 33
  • 2
  • 10