#include <stdio.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main(void)
{
int choice;
while ( (choice = get_choice()) != 'q') /*获得选项*/
{
switch (choice)
{
case 'a' :
break;
case 'b' :
break;
case 'c' : count();
break;
default :
break;
}
}
printf("Bye.\n");
return 0;
}
char get_choice(void) /*获得选项*/
{
int ch;
printf("Enter the letter of your choice:\n");
printf("a. advice b. bell\n");
printf("c. count q. quit\n");
ch = get_first();
while ( (ch < 'a' || ch > 'c') && ch != 'q')
{
printf("Please respond with a, b, c, or q.\n");
ch = get_first(); /*读取第一个字符*/
}
return ch;
}
char get_first(void) /*读取第一个字符*/
{
int ch;
ch = getchar();
while (getchar() != '\n') /*清除后面的*/
continue;
return ch;
}
void count(void)
{
int n,i;
printf("Count how far? Enter an integer:\n");
n = get_int(); /*获取输入*/
while ( getchar() != '\n')
continue;
}
int get_int(void) /*获取输入scanf*/
{
int input;
char ch;
while (scanf("%d", &input) != 1) /*获取输入scanf*/
{
while ((ch = getchar()) != '\n')
putchar(ch); // dispose of bad input
printf(" is not an integer.\nPlease enter an ");
printf("integer value, such as 25, -178, or 3: ");
}
return input;
}