0

C++ Give a text file consisting of digits and letters, your task is to find the int-type numbers from the file and calculates their sum. I tried to use

while (!file.eof()) {
file >> x; /// declared x as int
sum += x;
}

but it was an infinite loop. How can I do that? Thank you!

  • 1
    Who teaches `while (!file.eof())`? – Evg Apr 16 '22 at 08:15
  • Don't remember, probably it is possible to set letters as file stream separators instead of spaces, but it is possible to implemente whole parsing from scratch, like I just did [in following code](https://godbolt.org/z/aM8T5PYz1) for you, please put a look. Very first small block of code writes example text into file, this block is not needed if you have already existing file, remove this block. – Arty Apr 16 '22 at 09:19

0 Answers0