Sentinel control fragment

Hello,

I am new to programming and this forum, so if I am not following protocall here please let me know.

I have the following code fragment that was given to me and the question is:
"What is wrong with the following sentinel control?"

bool done = false;
while (!done)
{
cin >> x;
if (x == sentinelValue)
{
done = true;
}
// code to process x would go here
}

I think the answer is that the if statement does not check for the sentinel value, but I am not sure if that is the correct answer. Any input would be appriciated.

Thanks!


You have basically the right idea in your code segment. The loop will continue to run until the sentinel value is reached, for example if the sentinel value is -1, you could enter anything for x until a -1 was reached. I don't know the specifics of your code beyond your short piece but I think you have the right idea.
Last edited on
@ Azagaros

Basically this is the entire code provided by the teacher and he is asking what is wrong with this fragment, and he has provided nothing else to consider here. I have coded with sentinel and few times, however beside the fact the the if statement does not check for the sentinel I can not understand what else would be wrong with this, if anything.
The only thing I could ask, if I am to process the sentinel or not? The code segment you give does process the sentinel, so I cannot make any assumptions with the code without that knowledge but in most cases we don't need to process the sentinel it is designed to stop a process, like in the case a loop.
Topic archived. No new replies allowed.