dimanche 1 mai 2016

How To Terminate a Program By typing a letter

am creating a program that allows the user to enter a number and then adds, subtracts, divides or multiplies consecutively on the First number entered and also exits when x is pressed !

char o;
        float N1,N2,res;
        printf("Enter a Number Then Hit Enter> ");
        scanf("%f",&N1);
        res = N1;
        printf("\n****Value = %f****\n",res);
        while(1)
        {
            printf("\nEnter an operation (+,-,*,/) or enter (x) to exit Then Hit Enter> ");
            scanf("%*c%c",&o);

            if(o=='x')
            {
                break;
            }

            printf("Enter a Number Then Hit Enter> ");
            scanf("%f",&N2);
            switch(o)
            {
            case '+':
                res=res+N2;
                printf("\n****Value = %f****\n",res);
                break;
            case '-':
                res=res-N2;
                printf("\n****Value = %f****\n",res);
                break;
            case '*':
                res=res*N2;
                printf("\n****Value = %f****\n",res);
                break;
            case '/':
                res=res/N2;
                printf("\n****Value = %f****\n",res);
                break;

            default:
                printf("Illegal Operation Symbol ....Try again > ");
            }
        }
        enter code here
           return 0;
           }

Aucun commentaire:

Enregistrer un commentaire