0

I am have created a simple calculator program using switch statements which has been successful. But I am having trouble creating a do while loop at the bottom which loops the calculator function I have tried to create, which is my main goal to ask the user if they want to repeat the calculator program using a do while loop. Any help with be appreciated.

#include <stdio.h>

char math;
float number1;
float number2;
void calculator();
int selection = 0;

int main()
{
    void calculator(){
        printf(" enter the math operation: ");
        scanf("%c", &math);

        printf("Enter two numbers: ");
        scanf("%f%f", &number1, &number2);

        switch(math)
        {
        case '+':
            printf("number1+number2=%.2f",number1+number2);
        break;

        case '/':
            printf("number1/number2=%.2f",number1/number2);
        break;

        case '-':
            printf("number1-number2=%.2f",number1-number2);
        break;

        case '*':
            printf("number1*number2=%.2f",number1*number2);
        break;

        default:
            printf ("Wrong character entered.");
        }
    }

Start of the do while function which asks the user if they want to repeat the program.

    do{
        printf{"\n\n - Do you want to repeat the program?"};
        printf("\n1  - Yes");
        printf("\n2  - No");
        scanf("%i", &selection );
    }
    while (selection != 2);
    calculator();
    return 0;
}

5 Answers 5

1

To just answer the main question, you want to always run the calculator first, within the loop, then ask to run again:

void calculator() {
  // calc stuff here
}

int main() {
  do {
    calculator();
    printf("\n\n - Do you want to repeat the program?");
    printf("\n1  - Yes");
    printf("\n2  - No");
    scanf("%i", &selection );
  } while (selection != 2);
}
Sign up to request clarification or add additional context in comments.

5 Comments

so do i put this before the actual code which the calculator function itself?
As @TuBui mentioned, it's better to put the calculator function outside of main. Then you can just have your repeat loop in the main with a call to the calculator.
i have done that but i seem to have one issue with the first printf statement stating "syntax error before '{' token, but it seems fine to me.
Your first printf statement had curly braces {} and I just copied it. I've fixed it now with round braces ().
ahh yes thanks, the IDE im using doesnt make that very noticable.
1
  1. Can not define a function with a function. Move void calculator(){ and its body outside of main().

2 Always check the result of scanf().

3 Insert a space before %c to consume previous EOLs.

scanf(" %c", &math);

.
4 Move calculator(); into `while loop as suggested by @Josh B & @koodawg

Comments

1

firstly, I recommended putting the definition of calculator() function outside function main()

secondly, I recommended not using global variable if possible. Just put the declaration of your selection variable into function main(), and the declaration of math, number1, number2 variables into function calculator()

Thirdly (this one actually answers your question), call function calculator() inside the do{}while loop

Comments

0

You're call to calculator is in the wrong place, you need;

do {
   calculator();
   ...
} while(sel != 2);

Comments

0

your call for calculator is placed in while.but,it should be placed in do.because,the do stmt gets executed before while.so,it is good if you place call for calculator() function inside do loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.