0

SOLVED: Per the two responses about having a fixed size, i do have a private var for size of array.

Simple solution: while(i < rhs.size) or a for loop


I am getting an infinite loop from this code. In main I am inputting 1 2 3 4 5

The problem is the even after reading in all 5 int, the system is still waiting for more input. If I input another int, it will enter the loop again.

template <typename T>
istream& operator>> <>(istream& in, MyArray<T>& rhs)
{
    int i = 0; 
    while(!in.eof()) //I tried: while(in >> rhs.theArray[i]) as well, no go
    {
        in >> rhs.theArray[i];
        i++;
    }
    return in;
}
  • Don't use eof. Try `while(in >> rhs.theArray[i])` instead. –  Mar 13 '12 at 17:55
  • 1
    When do you expect it to stop reading elements? Do you always want to read exactly 5 elements? Or read until end of line? – interjay Mar 13 '12 at 17:56
  • http://stackoverflow.com/questions/4533063/how-does-does-ifstream-eof-work –  Mar 13 '12 at 17:57

3 Answers3

0

it's safe to say that you should never write while(!in.eof()) {.

EOF is set after a read attempt, not before. Instead it is better to do something like this:

T x;
while(in >> x) {
    rhs.theArray[i] = x;
    ++i;
}

What this does is attempt to extract an item from the stream. If after the extraction the stream is still in a "good" state, the body of the loop is executed, if not, the invariant fails and the loop terminates.

In then end, this makes the loop condition "while I could successfully get another item..."

which is a much better way to approach this in general since it also handles conditions besides EOF.

Evan Teran
  • 80,654
  • 26
  • 169
  • 231
0

Since your rhs.Array is most likely a fixed size (unless you are doing some fancy overloading of operator[] to properly expand the underlying memory array) you should only take input for the maximum number of spaces that are in the size of your array, not use a while-loop that could theoretically cause a buffer overflow.

In general, I'd follow these two rules for taking a variable amount of input from the command-line:

  1. Define a maximum number of reads depending on the pre-defined space in your array to prevent buffer overflows.
  2. Check for an escape character if you want to exit early using something like istream::peak() ... keep in mind that EOF has to be explicitly send from a command-line using CTRL-D, command-line re-direction from a file, etc.... better choices for escape characters are end-of-line characters like newline, carriage-return (Windows), etc.
Jason
  • 30,174
  • 7
  • 55
  • 73
0

You should have an int var named size to store a fixed size, once you do that you can just read in for int i = 0; i < size and that should solve your problem.

  • Unless of course there is nothing to read ... then you just get stuck blocking for input. In other words with a fixed-size loop that doesn't have an escape sequence, you *must* input a fixed number of characters. Anything less will cause the program to hang waiting for your for-loop to complete it's fixed number of iterations. – Jason Mar 22 '12 at 04:21