L-10 MCS 260 Wed 12 Sep 2001

Below are the extended versions of the programs we discussed in class.
/* L-10 MCS 260 Wed 12 Sep 2001 : truth tables for and and or */

#include<stdio.h>

int main(void)
{
   int b1,b2;    /* two boolean variables */

   printf("\nTruth table for \nlogical and operator :\n");

   for (b1 = 0; b1 <= 1; b1++)
      for (b2 = 0; b2 <= 1; b2++)
         printf("  %d and %d is %d\n", b1, b2, b1 && b2);

   printf("\nTruth table for \nlogical or operator :\n");

   for (b1 = 0; b1 <= 1; b1++)
      for (b2 = 0; b2 <= 1; b2++)
         printf("  %d or %d is %d\n", b1, b2, b1 || b2);

   return 0;
}
The program below is robust as it handles also character input. We may come back to this example in a later chapter.
/* L-10 MCS 260 Wed 12 Sep 2001 : robust sum of positive numbers */

#include<stdio.h>

int main(void)
{
   int sum = 0;         /* the number computer generated */
   int n;               /* user input */
   char ch;             /* to clear the buffer */

   printf("Give in sequence of numbers\n");

   for (;;)             /* infinite loop */
   {
      printf("  Give a number (0 to stop) : "); 

      if (scanf("%d", &n) == 0)        /* skipping illegal input */
      {
         printf("Characters read : "); /* read characters until newline */
         while ((scanf("%c",&ch) > 0) && (ch != '\n')) 
           printf("%c", ch);
         printf("\nThis is not a number, please try again.\n");
         continue;
      }

      if (n < 0)
      {
         printf("Will ignore negative number.\n");
         continue;
      }
      else if (n == 0)
      {
         printf("Will exit the loop now.\n");
         break;
      }
      else
         printf("Input is correct.");

      printf("  Adding %d to %d.\n", n, sum);
      sum += n;
   } 

   printf("Sum of your numbers is %d.\n", sum);

   return 0;
}
Below is the completed switch statement. Observe the use of the goto. Here we wish to print out the balance each time a deposit or withdrawal is made. This is already a larger application, for which functions are appropriate.
/* L-10 MCS 260 Wed 12 Sep 2001 : illustration of switch in menu at bank */

#include<stdio.h>

int main(void)
{
   int balance = 0;     /* balance of the bank account */
   int choice;          /* choice made by user */
   int amount;          /* amount deposited or withdrawn from account */

   do
   {
      printf("\nChoose one of the following options : \n");
      printf("  0. leave this program;\n");
      printf("  1. make a deposit;\n");
      printf("  2. withdraw cash;\n");
      printf("  3. print the balance.\n");

      printf("Type 0, 1, 2, or 3 : "); scanf("%d", &choice);

      switch (choice)
      {
         case 0: 
           printf("Have a nice day.  Please come again.\n");
           break;                  /* break leaves the switch */
         case 1:
           printf("Give the amount of your deposit : ");
           scanf("%d", &amount);
           balance += amount;
           goto print;             /* we want to print the balance */
         case 2:
           printf("Give the amount of your withdrawal : ");
           scanf("%d", &amount);
           balance -= amount;      /* no break: we go to next case */
         case 3:
         print:
           printf("Your current balance is %d.\n", balance);
           break;
         default:
           printf("Invalid choice.  Please try again.\n");
      }


   } while (choice != 0);

   return 0;
}