lesson 9 get yn c
[[lesson_9_get_yn_c]] last edit on Aug 10, 2005 3:50 AM by rescued_by_c

[user02@FC4 Lesson09]$ cat -n get_yn.c
   1  #include <stdio.h>
   2
   3  int main(void)
   4  {
   5     int done = 0;    // Set to true when Y or N is encountered
   6     char letter;
   7
   8     while (! done)
   9       {
  10          printf("\nType Y or N and press Enter to continue: ");
  11          scanf("%c", &letter);
  12
  13          if ((letter == 'Y') || (letter == 'y'))
  14             done = 1;
  15          else if ((letter == 'N') || (letter == 'n'))
  16             done = 1;
  17          else
  18             printf("\a");   // Sound the speaker's bell for
  19                             // invalid character
  20       }
  21     printf("The letter you typed was %c\n", letter);
  22
  23     return 0;
  24  }
[user02@FC4 Lesson09]$ gcc -o get_yn get_yn.c
[user02@FC4 Lesson09]$ ./get_yn

Type Y or N and press Enter to continue: a

Type Y or N and press Enter to continue:
Type Y or N and press Enter to continue: y
The letter you typed was y
[user02@FC4 Lesson09]$ ./get_yn

Type Y or N and press Enter to continue: No
The letter you typed was N
[user02@FC4 Lesson09]$ ./get_yn

Type Y or N and press Enter to continue: abc

Type Y or N and press Enter to continue:
Type Y or N and press Enter to continue:
Type Y or N and press Enter to continue:
Type Y or N and press Enter to continue: xyw

Type Y or N and press Enter to continue: The letter you typed was y
[user02@FC4 Lesson09]$