Question: Solved. Thanks!

I am a beginner in C programming. I wrote a simple calculator code and its working fine. Here's the code-

```
    #include<stdio.h>

int main() {

    char operator;
    double firstNumber,secondNumber;

    printf("Enter an operator (+, -, *,/): \n");
    scanf("%c", &operator);

    switch(operator)
    {
        case '+':
            printf("Enter two numbers: ");
            scanf("%lf %lf",&firstNumber, &secondNumber);
            printf("%.2f + %.2f = %.2f",firstNumber, secondNumber, firstNumber + secondNumber);
            break;

        case '-':
            printf("Enter two numbers: ");
            scanf("%lf %lf",&firstNumber, &secondNumber);       
            printf("%.2f - %.2f = %.2f",firstNumber, secondNumber, firstNumber - secondNumber);
            break;

        case '*':
            printf("Enter two numbers: ");
            scanf("%lf %lf",&firstNumber, &secondNumber);
            printf("%.2f * %.2f = %.2f",firstNumber, secondNumber, firstNumber * secondNumber);
            break;

        case '/':
            printf("Enter two numbers: ");
            scanf("%lf %lf",&firstNumber, &secondNumber);
            printf("%.2f / %.2f = %.2f",firstNumber, secondNumber, firstNumber / secondNumber);
            break;

        default:
            printf("Error! operator is not correct.\n");
    }

    return 0;
}

```

I want that if the user enters a wrong operator then the program instead of ending goes back to the starting. Any idea of how to do it? 
Thanks.

Answer: 
I create an array of character with the valid operators, at the end of the switch condition we check if its valid operator if not it will ask the user to enter the operator another time:

```
# include<stdio.h>
# include <string.h>

        int main()
        {

            char operator;
            double firstNumber, secondNumber;
            char operators[] = "-+*/";
            bool isOperator = false;

            while (!isOperator)
            {
                printf("Enter an operator (+, -, *,/): \n");
                scanf("%c", &operator);

                switch (operator)
                    {
                        case '+':
                            printf("Enter two numbers: ");
                    scanf("%lf %lf", &firstNumber, &secondNumber);
                    printf("%.2f + %.2f = %.2f", firstNumber, secondNumber, firstNumber + secondNumber);
                    break;

                        case '-':
                            printf("Enter two numbers: ");
                    scanf("%lf %lf", &firstNumber, &secondNumber);
                    printf("%.2f - %.2f = %.2f", firstNumber, secondNumber, firstNumber - secondNumber);
                    break;

                        case '*':
                            printf("Enter two numbers: ");
                    scanf("%lf %lf", &firstNumber, &secondNumber);
                    printf("%.2f * %.2f = %.2f", firstNumber, secondNumber, firstNumber * secondNumber);
                    break;

                        case '/':
                            printf("Enter two numbers: ");
                    scanf("%lf %lf", &firstNumber, &secondNumber);
                    printf("%.2f / %.2f = %.2f", firstNumber, secondNumber, firstNumber / secondNumber);
                    break;

                    default:
                            printf("Error! operator is not correct.\n");
                }
                char* res;
                res = strchr(operators,operator);
                if (res != NULL)
                {
                    isOperator = true;
                }
                else
                {
                    isOperator = false;
                }
            }

            return 0;
        }

```
You can do that by using a do while loop.

`int main()
{
 char ch;
 do
 {
 /* code for calculator*/
 ...
 ...
 printf("Enter your choice(y/n)?\n");
 scanf("\n%c", &ch);
 }while(ch == 'y' || ch == 'Y');
 return 0;
}`