码迷,mamicode.com
首页 > 其他好文 > 详细

菜单技术

时间:2014-12-16 13:38:22      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:c语言   菜单技术   

<span style="font-size:18px;">#include <stdio.h>
#include <stdlib.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':
            printf("Buy low,sell high.\n");
            break;
            case 'b':
            putchar('\a');
            break;
            case 'c':
            count();
            break;
            default:
            printf("Program error!\n");
            break;
        }
    }
    printf("Bye!\n");
    return 0;
}

void count (void)
{
    int n,i;
    printf("Count how far ? Enter integer:\n");
    n = get_int();
    for( i = 0; i < n ; i++)
    {
        printf("%d\n",i);
    }
    while(getchar()!='\n')
    continue;
}

char get_choice(void)
{
    int ch;
    printf("Enter the letter of your choice:\n");
    printf("a.advice        b.bell\n");
    printf("c.count         d.quit\n");
    ch = get_first();
    while(((ch < 'a')||ch >'c') && ch != 'q')
    {
        printf("Please respond with a,b,c,d,and 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);<span style="white-space:pre">				</span>/* 剔除错误的输入 */
        printf(" is not an integer.\n Please enter an integer value,such as 25,-178,or 3:");
    }
    return input;
}
</span>

菜单技术

标签:c语言   菜单技术   

原文地址:http://blog.csdn.net/u011046042/article/details/41958247

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!