C Primer Plus学习 十五 菜单输入

/* menuette.c P218 --菜单技术 */
#include <stdio.h>
char get_choice(void);//选择菜单项
char get_first(void);//获取每行的第一个字符
int get_int(void);//获取一个整数
void count(void);//
int main(void)
{
int choice;
void count(void);


while ((choice=get_choice())!='q')
{
switch (choice)
{
case'a':printf("Buy low ,sell high.\n");
break;
case 'b':putchar('\a');/*ANSI*/
break;
case 'c':count();
break;
default:printf("Program eror!\n");
break;
}



}
printf("Bey.\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,ro q.\n");
ch=get_first();


}
return ch;
}
char get_first(void)//获取每行的第一个字符
{
int ch;
ch=getchar();
while (getchar()!='\n')
{
continue;
}
return ch;
}
int get_int(void)//获取一个整数
{
int input;
char ch;
while (scanf("%d",&input)!=1)
{
while ((ch=getchar())!='\n')
{
putchar(ch);//剔除错误的输入
}
printf("is not an integer.\nplease enter an");
printf("integer value,such as 25,-178,or 2:");
}
return input;
}
void count(void)//
{
int n,i;
printf("Count how far?enter an integer:\n");
n=get_int();
for (i=1;i<=n;i++)
{
printf("%d\n",i);
}
while (getchar()!='\n')
{
continue;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值