Understanding loops within loops C++


Apologies for the newbie question but I’m new to programming and learning C++. I’m working my way through C++ Primer and I’ve gotten to the If statement section where this code has been written.

#include <iostream>
int main()
{ 
    // currVal is the number we're counting, new values will be put into val
    int currVal = 0, val = 0;
    // Read first number and check there is data to process
    if (std::cin >> currVal) {
        int cnt = 1; // Store the count for the current value we're processing
        while (std::cin >> val) { // read the remaining numbers
            if (val == currVal)   // if the values are the same
                ++cnt;            // add 1 to count (cnt)
            else {  // Otherwise, print the count for the previous value
                std::cout << currVal << " occurs " << cnt << " timesn";
                    currVal = val; // remember the new value
                    cnt = 1;       // reset the counter
            }
        } // while loop ends here
          // rememeber to print the count for the last value in the flile
        std::cout << currVal << " occurs " << cnt << " times.n";


    } // Outermost if statement ends here
    return 0;
}

When compiled the logic is incorrect. For example, when I enter 51 25 14 51 51 51 25. I get the following:

51 occurs 1 times
25 occurs 1 times
14 occurs 1 times
51 occurs 3 times

I’ve tried breaking down the code but with loops within loops it’s making my head spin.

Again, apologies for the noob question but any help would be appreciated.

Thanks