Produce the sum of all white space separated integers in a given file How's my code?  Works perfectly for what I'm trying to do :)
#include&lt;iostream&gt;
#include&lt;string&gt;
#include&lt;vector&gt;
#include&lt;algorithm&gt;
#include&lt;cmath&gt;
#include&lt;fstream&gt;
#include&lt;assert.h&gt;
using namespace std;
inline void keep_window_open() { char ch; cin &gt;&gt; ch; }



int main() {
    int sum{ 0 }, number;
    vector&lt;int&gt; numbers;
    string word;
    fstream file;
    file.open(&quot;file1.txt&quot;, fstream::in);

    while (true) {
        file &gt;&gt; number;
        if (file.eof()) {
            numbers.push_back(number);
            break;
        }
        else if (file.fail()) { 
            file.clear();
            file &gt;&gt; word;
        }
        else if (file.bad()) exit(1);
        else numbers.push_back(number);
    }

    for (int x : numbers) sum += x;

    cout &lt;&lt; sum &lt;&lt; &quot;\n&quot;;


}

Here's the file I'm reading from:
words 32 jd: 5 alkj 3 12 fjkl: 23 / 32
hey 332 2 jk 23 k 23 ksdl 3
32 kjd 3 klj 332 c 32
You collect all numbers but then use the collection only for the sum. Better just update the sum. And I find the whole trying int and if fails use word a bit complicated. How about just always reading words and then reading numbers from them? (The &gt;&gt; writes zero if there's no number.)
#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
using namespace std;

int main() {
    int sum = 0;
    ifstream file(&quot;file1.txt&quot;);
    string word;
    while (file &gt;&gt; word) {
        int number;
        istringstream(word) &gt;&gt; number;
        sum += number;
    }
    cout &lt;&lt; sum &lt;&lt; endl;
}