0

Is there any alternative from using the .eof, by using cin as a condition for this?

while(cin.eof( )==false)
{
    cin >> number;
    sum += number;
    count++;
}
c00ks
  • 23
  • 3
  • Does this answer your question? [Why is iostream::eof inside a loop condition (i.e. \`while (!stream.eof())\`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – stark Apr 26 '20 at 18:48
  • 1
    You almost *never* want to use `.eof()`. – Mark Ransom Apr 26 '20 at 18:50
  • `while ( cin >> number )` then for any non number (assuming `number` is `double` or similar) it will fail. – ChrisMM Apr 28 '20 at 16:32

1 Answers1

1

Here's a better alternative:

while(cin >> number)
{
    sum += number;
    ++count;
}

See Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())) considered wrong?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93