Thread: sentinel controlled repetition example

  1. #1
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83

    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.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    /* Variable definition */
    int acc_no, bal_st, new_bal, tot_chg, tot_credit, credit_lim, counter;
    
    counter = 1; 
    
    /* Collect data */
    while(acc_no != -1) {    /* sentinel value =-1 */
                 printf("Enter account no (-1 to end): \n");
                 scanf("%d", &acc_no);
                 printf("Enter beginning balance: ", "\n");
                 scanf("%d", &bal_st);
                 printf("Enter total charges: ", "\n");
                 scanf("%d", &tot_chg);
                 printf("Enter total credits: ", "\n");
                 scanf("%d",  &tot_credit);
                 printf("Enter credit limit: ", "\n");
                 scanf("%d", &credit_lim);
    
                 new_bal = bal_st + tot_chg - tot_credit; /* Calculate new bal */
                 counter++;
                 }
                 if(new_bal > credit_lim) {              /* test if more than lim */                     
                            printf("Account No.: %d\n", acc_no);
                            printf("Credit Limit: %d\n", credit_lim);
                            printf("Balance: %d\n", new_bal);
                            printf("Credit Limit Exceeded\n");
                            }
                            
                 
    printf("end of entries\n");
    system("PAUSE");
    return 0;
    }
    I'm suspecting that my while loop is set up wrong, but I have just started learning C, so I am confused.

    Can anyone give me any pointers to fix this?

    --dave

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Try putting all of the if statement inside the while loop.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Dave, the first thing to fix is the program's indentation. It has lead your logic far astray.

    << Don't they teach decent indenting styles anymore?? >>

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       /* Variable definition */
       int acc_no, bal_st, new_bal, tot_chg, tot_credit, credit_lim, counter;
    
       counter = 1; 
    
       /* Collect data */
       while(acc_no != -1) {    /* sentinel value =-1 */
                 printf("Enter account no (-1 to end): \n");
                 scanf("&#37;d", &acc_no);
                 printf("Enter beginning balance: ", "\n");
                 scanf("%d", &bal_st);
                 printf("Enter total charges: ", "\n");
                 scanf("%d", &tot_chg);
                 printf("Enter total credits: ", "\n");
                 scanf("%d",  &tot_credit);
                 printf("Enter credit limit: ", "\n");
                 scanf("%d", &credit_lim);
    
                 new_bal = bal_st + tot_chg - tot_credit; /* Calculate new bal */
                 counter++;
       }
       if(new_bal > credit_lim) {              /* test if more than lim */                     
                 printf("Account No.: %d\n", acc_no);
                 printf("Credit Limit: %d\n", credit_lim);
                 printf("Balance: %d\n", new_bal);
                 printf("Credit Limit Exceeded\n");
       }
                            
                 
       printf("end of entries\n");
       system("PAUSE");
       return 0;
    }
    Clearly, if you want the first prompt to appear, the part in blue above, needs to go inside the closing
    brace of the while statement.

    Then this line seems incorrect:
    Code:
                 new_bal = bal_st + tot_chg - tot_credit; /* Calculate new bal */
    I'm thinking:
    Code:
                 new_bal = bal_st - (tot_chg + tot_credit); /* Calculate new bal */
    since charges against an account should be deducted, not added.
    Last edited by Adak; 09-25-2008 at 09:35 AM.

  4. #4
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    Thanks for that, now does show when limit is exceeded

    but still ignores sentinel value...

    Code:
    /* Collect data */
    while(acc_no != -1) {    /* sentinel value =-1 */
                             printf("Enter account no (-1 to end): \n");
                             scanf("&#37;d", &acc_no);
                             printf("Enter beginning balance: ", "\n");
                             scanf("%d", &bal_st);
                             printf("Enter total charges: ", "\n");
                             scanf("%d", &tot_chg);
                             printf("Enter total credits: ", "\n");
                             scanf("%d",  &tot_credit);
                             printf("Enter credit limit: ", "\n");
                             scanf("%d", &credit_lim);
    
                             new_bal = (bal_st - tot_credit) + tot_chg; /* Calculate new bal */
                             counter++;
                             if(new_bal > credit_lim) {              /* test if more than lim */                     
                                        printf("Account No.: %d\n", acc_no);
                                        printf("Credit Limit: %d\n", credit_lim);
                                        printf("Balance: %d\n", new_bal);
                                        printf("Credit Limit Exceeded\n");
                            }
                         }
                 
    printf("end of entries\n");
    return 0;
    }
    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
    Last edited by droseman; 09-25-2008 at 06:59 AM. Reason: code correction

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by droseman View Post

    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.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by droseman View Post
    Thanks for that, now does show when limit is exceeded

    but still ignores sentinel value...

    Code:
       /* Collect data */
       while(acc_no != -1) {    /* sentinel value =-1 */
                             printf("Enter account no (-1 to end): \n");
                             scanf("&#37;d", &acc_no);
    
                        if(acc_no != -1)   {
                             printf("Enter beginning balance: ", "\n");
                             scanf("%d", &bal_st);
                             printf("Enter total charges: ", "\n");
                             scanf("%d", &tot_chg);
                             printf("Enter total credits: ", "\n");
                             scanf("%d",  &tot_credit);
                             printf("Enter credit limit: ", "\n");
                             scanf("%d", &credit_lim);
    
                             new_bal = (bal_st - tot_credit) + tot_chg; /* Calculate new bal */
                             counter++;
                             if(new_bal > credit_lim) {              /* test if more than lim */                     
                                        printf("Account No.: %d\n", acc_no);
                                        printf("Credit Limit: %d\n", credit_lim);
                                        printf("Balance: %d\n", new_bal);
                                        printf("Credit Limit Exceeded\n");
                             }//end of if
                        }//end of if     
       }//end of while
              
       printf("end of entries\n");
       return 0;
    }//end of function
    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.
    Just makes everything easier.
    Last edited by Adak; 09-25-2008 at 09:30 AM.

  8. #8
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    That worked exactly as it should, thank you very much, and thanks to all contributors for their assistance. I have learnt a lot

    Right, on to functions and pointers...

    --dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sentinel controlled loop
    By arjay in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 04:30 AM
  2. Problem with sentinel values
    By Golfduffer in forum C Programming
    Replies: 3
    Last Post: 08-30-2007, 01:19 AM
  3. SENTINEL question
    By codeHer1 in forum C Programming
    Replies: 2
    Last Post: 03-24-2005, 09:34 AM
  4. rb tree Sentinel NIL
    By dinjas in forum C Programming
    Replies: 2
    Last Post: 03-06-2005, 12:28 AM
  5. Sentinel while loop and errors.
    By lollypop in forum C Programming
    Replies: 12
    Last Post: 10-19-2003, 12:40 PM