基于菜单的程序使用do while

时间:2013-12-27 03:56:55

标签: c loops menu switch-statement do-while

#include <stdio.h>
#include <conio.h>

void main()
{
    int choice,cala,calb,add,product,subtract,mod,bininput, c, k,perfectinput, perfecttotal, perfecti,primei, primeinput, flagprime,factinput,factorial,facti;
    float divide;
    do
    {
    printf("********** Main Menu **********\n(1).Calculator\n(2).Decimal to Binary convertor\n(3).Perfect number finder\n(4).Prime number or composite number\n(5).Factorial Calculator\n(6).To exit\n");
    scanf("%d",choice);

    switch (choice)
    {
    case 1:
                    clrscr();
                    printf("********** Calculator **********\n\nEnter numbers:-\n");
                    scanf("%d%d" , &cala,&calb);

                    add = cala + calb;
                    subtract = cala - calb;
                    product = cala * calb;
                    divide = cala / (float)calb;
                    mod = cala % calb;

                    printf("Sum = %d\n",add);
                    printf("Sub = %d\n",subtract);
                    printf("product = %d\n",product);
                    printf("divide = %2f\n",divide);
                    printf("mod = %d\n",mod);
                    printf("(0). Return to main menu");
                    printf("\n");
                break;

            case 2:
                clrscr();

                printf("********** Decimal to Binary convertor **********\n\nEnter an integer in decimal number system:-");
            scanf("%d", &bininput);

            printf("%d in binary number system is:\n", bininput);
            printf("\n");

            for (c = 10; c >= 0; c--)
            {
                k = bininput >> c;

                if (k & 1)
                    printf("1");
                else
                    printf("0");
            }
            printf("\n(0). Return to main menu\n");
            printf("\n");
            printf("\n");
            break;

        case 3:
            clrscr();

            printf("********** Perfect number finder **********\n");
            printf("Enter a number:- ");
            scanf("%d" ,&perfectinput);
            perfecttotal=0;
            perfecti=1;
            for(perfecti=1;perfecti < perfectinput;perfecti++)
            {
                if(perfectinput%perfecti == 0)
                perfecttotal=perfecttotal+perfecti;
            }
            if(perfecttotal==perfectinput)
                printf("%d is a perfect number\n\n\a\a",perfectinput);
            else
                printf("%d is not a perfect number\n\n\a",perfectinput);
            printf("(0). Return to main menu\n");
            break;

        case 4:
            clrscr();


            printf("********** Prime number or composite number **********\n");
            printf("Enter a number:- ");
            scanf("%d" ,&primeinput);
            flagprime=0;
            primei=2;
            while(primei <= primeinput/2)
            {
                if (primeinput%primei == 0)
                {
                    flagprime=1;
                    break;
                }
                primei++;
            }
            if(flagprime==0)
                printf("%d is a prime number\n\n",primeinput);
            else
                printf("%d is a composite number\n\n",primeinput);
            printf("(0). Return to main menu\n");
            break;

        case 5:
            clrscr();

            printf("********** Factorial Calculator **********\n");
            printf("Enter a number:- ");
            scanf("%d" ,&factinput);
            factorial=1;
            for(facti=factinput;facti>0;facti--)
            {
                factorial=factorial*facti;
            }
            printf("Factorial of %d = %d\n\n",factinput,factorial);
            printf("(0). Return to main menu\n");
            break;

        case '6':
                clrscr();
                printf("Program terminated.\nPress any key to exit");
                break;
        default: printf("wrong input");
        }
    }while(choice != 6);
    printf("bye");
    getch();
}

1 个答案:

答案 0 :(得分:2)

你忘记了&amp; ...

scanf("%d",&choice);