sentinel controlled repetition example

This is a discussion on sentinel controlled repetition example within the C Programming forums, part of the General Programming Boards category; Hi
the following program is meant to take input from the user, check if the balance (new_bal) is more than ...

sentinel controlled repetition example

Hi

the following program is meant to take input from the user, check if the balance (new_bal) is more than the credit limit (credit_lim) and show the account number (acc_no), balance and credit limit, and print that the limit has been exceeded immediately after the input data, then loop back to the first prompt.

I have used sentinel controlled repetition, but it seems to completely ignore the sentinel value if I put it in, and when I put in values exceeding the limit, I don't get them printed under.

Surely if you enter -1, then it skips all the code and prints "end of entries"...?

to adak: Thanks for pointing out my indentation, when I did it logically, it made much more sense

--dave

That indentation is still not right, but let's run with it. The while condition will only be executed at the beginning of the loop. i.e. What happens is you check the sentinel, it's not -1, you read in the input, which sets it to -1, you now still need to wait for the loop to go all the way round to the beginning again before it is retested.

Is it possible that you think of while in more of the human concept than in the programming language concept? In human concept, we can say something like "while the water isn't boiling keep the gas on full, then add pasta and turn the gas down to a simmer" and you don't actually think "check if it's boiling, no - continue chopping onions", you just realize as part of your general awareness of what's going on around you that the pot has started to boil.

Computers do not have "general awareness", and need to be told exactly what to do at any particular moment in time. In this case, while(...) only checks if the condition is true when it gets to the actual line of the while(...), not throughout the code within the loop. So if you want to skip over something when someone enters -1 as account number, then you need to add an if-statement just after that either breaks the loop [in which case, your loop probably should be a while(1) or while(true)], or skips over the rest of the code in the while-block.

Surely if you enter -1, then it skips all the code and prints "end of entries"...?

to adak: Thanks for pointing out my indentation, when I did it logically, it made much more sense

--dave

I don't see any code that sets the sentinel value to -1. Is it off the page, somewhere?
With a while() loop, you usually have to "prime the pump", that is, set some variables to their
correct starting values, immediately before the program begins the while() loop.

Like matsp mentioned, even though you just had a line of code saying:

Code:

while(sentinel Value != -1) {

That doesn't mean that if you enter that value a few lines later, that the program will
do as you ask, THEN. Oh no!

You must re-test the sentinel value AFTER you have the account number entered by the user.

I put some simple addition logic along these lines, into the above code of yours. I didn't
test it, but it should be fine.

The while test condition is still needed to break out of the loop, here. You also need
some code to stop the program from working on the account number -1, however.

If you indent so that the start of the expression is vertically lined up with it's closing
brace, you'll find it *very* helpful:

Code:

if(whatever) {
for(i = 0; i < whoknows; i++) {
while(array[i] < somevalue) {
//some code here
} //vertically lined up with the while, which it closes
} //vertically lined up with the for loop, which it closes
} //same, for the if expression.