I'm new to programming, and I'm trying to learn C++ by myself, and I'm following "Programming principles and practice using C++".

I was trying to do an exercise that, after various other steps, asked me to 

> 
> " ...change the body of the loop so that it reads just one double each time around. Define two variables to keep track of which is the smallest and which is the largest value you have seen so far. Each time through the loop write out the
>  value entered. If it’s the smallest so far, write the smallest so far after the number. If it is the largest so far, write the largest so far after the number".
> 
> 
> 

I wrote the following code so far:

```
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open() { cout<<"\nType a character to exit: "; char ch; cin>>ch; }

int main()
{
double val1 = 0, smallest = 0, largest = 0;
int flag = 0;
while (cin >>val1) {
    if (val1=='|')
        break;
    else
        cout <<val1 <<'\n';
    if (flag==0) {
        smallest = val1;
        cout <<smallest <<" it's the smallest value so far.\n";
    }
    if (val1<smallest) {
        smallest = val1;
        cout <<smallest <<" it's the smallest value so far.\n"; }
    else if (val1>largest) {
             largest = val1;
             cout <<largest <<" it's the largest value so far.\n"; }
    ++flag;
}

keep_window_open();

return 0;
}

```

My problem is that when I input a character, e.g. 'c', the program ends, although the program should end, hypothetically, only when I enter '|', and I get:

```
c

Type a character to exit:
Process returned 0 (0x0)     execution time : ...
Press any key to continue.

```

and `"keep_window_open()"` doesn't wait for a character to be entered. I just can't understand what happens, and why. Does anyone have a clue?