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;
        }

```
What you can do is take a while loop and a small condition statement. 

```
char choice ='y';
while(choice=='y'||choice=='Y')
{
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");
        printf("Do you want to continue? Press y for yes and n for no");
        scanf("%c",&choice);
        break;
}

return 0;
}

```

Hope this helps! 
This is a very simple way to take care of your problem. When you are writing any program in C, always keep in mind the flow of your program. If you want your calculator to always run until you tell it to stop then always use a loop. It will keep iterating the snippet as many times as you want. In your example, a while or a do-while loop is preferable since you do not know how many times a user will need to call on the function.

A better way to write this code will be to write the entire switch case in a function and pass the numbers and operator to the said function. Then in the main() block you can use a while loop which will call the function and check for your response if you want to continue or terminate the program. It is always a good habit to start writing in functions, especially since you are just a beginner in C.