The following is a similar program I had when having trouble:

    float x;        
    int n=0;
    cout << "enter some numbers separated by space; enter 1.2 when done: ";
    cin >> x;
    while (x!=1.2)
        cin >> x;

The problem is, when using certain numbers as the sentinel (in the above it's 1.2), the sentinel would not work, i.e. it would always wait for the next input, even though it's supposed to end. There was no pattern which numbers worked or not, e.g. maybe 2.2, 4.5, didn't work, but 5.5 worked. But chaning the type of x from float to double seemed to solve the problem. Could someone explain why? It seems to have to do with precision? Thanks.

I think 1.2 literal is double by default, so...
it has to be cast to float

while (x != (float)1.2)

or declared as float

while (x != 1.2f)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.