How to brand this program enquire for input again if invalid input is entered (in C programming)? [duplicate]

https://stackoverflow.com/questions/42464437/how-to-make-this-programme-ask-for-input-once more-if-invalid-input-is-entered-in-c-p?fbclid=IwAR1BB7wkcELX0D_GkWAOVPe7m3of-gWMo60PztXTV1YqFD6jijQy1oYkR70

Loop Infinite

                                              #include                                                                        <stdio.h>                                                                        #include                                                                        <ctype.h>                                                                        int                                                  primary                        (                        void                        )                                                                        {                                                                        char                                                  ch                        ;                                                                        float                                                  num1                        ,                        num2                        ,                        answer                        ;                                                  printf                        (                        "Enter the operation of your choice:\north"                        );                                                  printf                        (                        "a. add      s. decrease\n"                        );                                                  printf                        (                        "m. multiply q. carve up\n"                        );                                                  printf                        (                        "q. quit\n"                        );                                                                        while                                                                        ((                        ch                                                =                                                  getchar                        ())!=                        'q'                        )                                                                        {                                                  ch                        =                        tolower                        (                        ch                        );                                                                        //Ignore whitespace                                                                        if                                                                        (                        ch                        ==                        '\northward'                        )                                                                        continue                        ;                                                                        else                                                                        {                                                                        switch                        (                        ch                        )                                                                        {                                                                        //Addition part                                                                        case                                                                        'a'                        :                                                                        //First number                                                  printf                        (                        "Enter first number: "                        );                                                                        //Check to encounter if input is a number                                                                        while                                                                        (                        scanf                        (                        "%f"                        ,&                        num1                        )==                        0                        )                                                                        {                                                  printf                        (                        "Invalid input. Please enter a number, such equally 2.v, -1.78E8, or 3: "                        );                                                  scanf                        (                        "%*s"                        );                                                                        }                                                                        //Second number                                                  printf                        (                        "Enter second number: "                        );                                                                        while                                                                        (                        scanf                        (                        "%f"                        ,&                        num2                        )==                        0                        )                                                                        {                                                  printf                        (                        "Invalid input. Delight enter a number, such equally 2.5, -ane.78E8, or three: "                        );                                                  scanf                        (                        "%*due south"                        );                                                                        }                                                                        //Do math for corresponding operation                                                  reply                                                =                                                  num1                                                +                                                  num2                        ;                                                                        //Impress out result                                                  printf                        (                        "%.3f + %.3f = %.3f\n"                        ,                                                  num1                        ,                        num2                        ,                        respond                        );                                                                        break                        ;                                                                        //Subtraction part                                                                        case                                                                        's'                        :                                                  printf                        (                        "Enter offset number: "                        );                                                                        while                                                                        (                        scanf                        (                        "%f"                        ,&                        num1                        )==                        0                        )                                                                        {                                                  printf                        (                        "Invalid input. Please enter a number, such as ii.5, -1.78E8, or 3: "                        );                                                  scanf                        (                        "%*southward"                        );                                                                        }                                                  printf                        (                        "Enter 2nd number: "                        );                                                                        while                                                                        (                        scanf                        (                        "%f"                        ,&                        num2                        )==                        0                        )                                                                        {                                                  printf                        (                        "Invalid input. Delight enter a number, such as 2.5, -1.78E8, or 3: "                        );                                                  scanf                        (                        "%*due south"                        );                                                                        }                                                  respond                                                =                                                  num1                                                -                                                  num2                        ;                                                  printf                        (                        "%.3f - %.3f = %.3f\n"                        ,                                                  num1                        ,                        num2                        ,                        answer                        );                                                                        break                        ;                                                                        //Multiplication office                                                                        case                                                                        'm'                        :                                                  printf                        (                        "Enter offset number: "                        );                                                                        while                                                                        (                        scanf                        (                        "%f"                        ,&                        num1                        )==                        0                        )                                                                        {                                                  printf                        (                        "Invalid input. Delight enter a number, such equally 2.5, -1.78E8, or 3: "                        );                                                  scanf                        (                        "%*s"                        );                                                                        }                                                  printf                        (                        "Enter second number: "                        );                                                                        while                                                                        (                        scanf                        (                        "%f"                        ,&                        num2                        )==                        0                        )                                                                        {                                                  printf                        (                        "Invalid input. Please enter a number, such as 2.v, -1.78E8, or 3: "                        );                                                  scanf                        (                        "%*s"                        );                                                                        }                                                  answer                                                =                                                  num1                                                *                                                  num2                        ;                                                  printf                        (                        "%.3f * %.3f = %.3f\n"                        ,                                                  num1                        ,                        num2                        ,                        answer                        );                                                                        break                        ;                                                                        //Division function                                                                        example                                                                        'd'                        :                                                  printf                        (                        "Enter offset number: "                        );                                                                        while                                                                        (                        scanf                        (                        "%f"                        ,&                        num1                        )==                        0                        )                                                                        {                                                  printf                        (                        "Invalid input. Delight enter a number, such equally ii.five, -1.78E8, or 3: "                        );                                                  scanf                        (                        "%*s"                        );                                                                        }                                                  printf                        (                        "Enter second number: "                        );                                                                        while                                                                        (                        scanf                        (                        "%f"                        ,&                        num2                        )==                        0                        )                                                                        {                                                  printf                        (                        "Invalid input. Please enter a number, such every bit ii.5, -1.78E8, or three: "                        );                                                  scanf                        (                        "%*south"                        );                                                                        }                                                                        //Check for if number is a zero                                                                        while                                                                        (                        num2                        ==                        0                        )                                                                        {                                                  printf                        (                        "Please enter a non-zero number, such as two.5, -1.78E8, or 3: "                        );                                                                        while                                                                        (                        scanf                        (                        "%f"                        ,&                        num2                        )==                        0                        )                                                                        {                                                  printf                        (                        "Invalid input. Please enter a number, such every bit 2.5, -1.78E8, or 3: "                        );                                                  scanf                        (                        "%*southward"                        );                                                                        }                                                                        }                                                  reply                                                =                                                  num1                                                /                                                  num2                        ;                                                  printf                        (                        "%.3f / %.3f = %.3f\northward"                        ,                                                  num1                        ,                        num2                        ,                        answer                        );                                                                        break                        ;                                                                        //For if a non-valid operation is entered                                                                        default                        :                                                  printf                        (                        "That is not a valid operation.\n"                        );                                                                        break                        ;                                                                        }                                                                        }                                                  printf                        (                        "Enter the performance of your choice:\northward"                        );                                                  printf                        (                        "a. add together      south. subtract\north"                        );                                                  printf                        (                        "m. multiply q. carve up\n"                        );                                                  printf                        (                        "q. quit\northward"                        );                                                                        }                                                  printf                        (                        "Good day.\due north"                        );                                                                        render                                                                        0                        ;}                                          

Pop posts from this blog

Assignment to develop a non-scientific calculator using C Language-81998

Image

Questions are taken from "C: How to Program" by Deitel and Deitel.

https://world wide web.eecs.wsu.edu/~cs150/tutorial/ CptS 150 Program Pattern and Development Tutorial Questions are taken from "C: How to Programme" by Deitel and Deitel. Acknowledgements I would like to thank Janet Raynor, Michelle Antolovich and Kael Rowan for their contribution to this tutorial. Without their hard work, this effort would non accept been possible. I would also like to thank the students for testing the tutorial and providing valuable feedback. -- Teck Choo Tan Introduction to C Programming Short Review Problems two.seven 2.ix 2.10 two.15 3.43 5.50d Programming Problem 2.20 TAKE THE QUIZ! Selection Short Review Problems 3.11 three.31 4.24b, iv.24e 4.30 Programming Issues iii.35 4.28 iii.20 3.23 Accept THE QUIZ! Loops Short Review Problems iii.10 4.5 4.6b Programming Problems 3.17 three.47b 4.10 4.12 4.xviii TAKE THE QUIZ! Functions Short Review Problems 5.seven 5.viii five.13 v.50 Programming Proble