-1

I'm very new to C programming, and I need a help! I have made a program of calculator with the help of Switch Statement and I want this program to be in a loop, So that it ask the menu (i.e Enter your Choice: Divide,Multiplication,Addition etc) again and again with the user. Also I want, an End option with the Cases in Menu, which will close the program. I don't know how to code that End option which will make the Program Close. Please Help!

#include<stdio.h>
#include<conio.h>

int main()
{
  int a,b,choice;
  float sum,mul,div,sub,quo;
  printf("\n\t\t\t\t CALCULATOR");
  printf("\nEnter the First Number: ");
  scanf("%d",&a);

  printf("Enter the Second Number: ");
  scanf("%d",&b);

  printf("\n Enter Your Choice");
  printf("\n\n1.Sum");
  printf("\n2.Multiplication");
  printf("\n3.Division");
  printf("\n4.Subtraction");
  printf("\n5.Quotient");
  printf("\nYOUR CHOICE: ");
  scanf("%d",&choice);
  switch(choice)
  {
    case 1:
    sum=a+b;
    printf("Sum= %f\n",sum);
    break;
    case 2:
    mul=a*b;
    printf("Multiplication= %f\n",mul);
    break;
    case 3:
    div=a/b;
    printf("Division= %lf\n",div);
    break;
    case 4:
    sub=a-b;
    printf("Subtraction= %f\n",sub);
    break;
    case 5:
    quo=a%b;
    printf("Quotient= %f\n",quo);
    break;
    default:
      printf("\n Unavailable Choice");
  }
return 0;
getch();
}
Devanshu
  • 1
  • 3

3 Answers3

1

Assuming you add option number 6 to close the calculator, you can add the following case to quit the program -

case 6: 
    // print bye message or whatever
    exit(0); 
    break;

You can read about the exit function and what the arguments passed to it mean. Usually a 0 indicates successful exit from the program.

You also need to include stdlib.h if you want to use exit.

Ajay Brahmakshatriya
  • 8,993
  • 3
  • 26
  • 49
  • In my experience, `exit` is most appropriate for graceful crashing. When in `main`, simply return your error code and let your stack get cleaned up. – wbadart Nov 07 '17 at 05:23
  • @wbadart cleaning of the stack is not necessary for C since there are no destructors and the address space is anyway getting destroyed. It is just a matter of choice. – Ajay Brahmakshatriya Nov 07 '17 at 05:24
  • @wbadart: graceful crashing is achieved with `abort();`. The `exit` function is for normal exits. – Basile Starynkevitch Nov 07 '17 at 05:32
  • @Devanshu, what is the exact problem? Does the program keep running even after inputting `6`? – Ajay Brahmakshatriya Nov 07 '17 at 06:05
  • Do I need to Declare it too, Anywhere? – Devanshu Nov 07 '17 at 06:07
  • No, you just need to include `stdlib.h` as `#include ` at the top. – Ajay Brahmakshatriya Nov 07 '17 at 06:07
  • @Devanshu can you [edit](https://stackoverflow.com/posts/47150456/edit) the question and add your new code? Please do not tamper the original question. Add a new para showing what code you are trying right now (with the loop around the switch case). – Ajay Brahmakshatriya Nov 07 '17 at 06:14
  • Can you help me with Second problem, How to put those in Loop? – Devanshu Nov 07 '17 at 06:27
  • @Devanshu yes, enclose everything from the first `printf` to the end of switch-case in a `while(1) {` and `}`. Basically write `while(1) {` before you print `CALCULATOR` and `}` where your switch-case ends. – Ajay Brahmakshatriya Nov 07 '17 at 06:29
  • Thanks a lot, Well can you tell me, How this while(1) worked? – Devanshu Nov 07 '17 at 06:54
  • @Devanshu You can read about [while loops](https://www.tutorialspoint.com/cprogramming/c_while_loop.htm). But I would suggest you read a good book on C instead of trying to learn by trial and error. You can look at [this list of books on C](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Ajay Brahmakshatriya Nov 07 '17 at 06:58
1

I would suggest introducing a loop condition variable, eg. CarryOn, initialise it to 1, and use that in a while loop. Let the loop continue as long as the variable is 1. When the user selects "End", set the variable to 0 in the case. That will end the loop.

Let me clarify it with some example code outline:

int CarryOn = 1;
while (CarryOn == 1)
{
  // ...your original code
  // ...
  // add to switch
  case 6: // 6 will be the "End" / "Exit" option
    CarryOn = 0;
    break;
}
binarybarbarian
  • 65
  • 1
  • 2
  • 10
0

You will need to have a while loop.

while(true){
printf("\n\t\t\t\t CALCULATOR");
  printf("\nEnter the First Number: ");
  scanf("%d",&a);
....
//return 0; remove the return statement
getch();

 }

and also add the menu item 6) Exit to your printf and handle that option as described in the one of the answers above.

case 6: 
    // print bye message or whatever
    exit(0); 
    break;
akshaya pandey
  • 997
  • 6
  • 16
  • Ok,You've Opened the Loop, but You haven't Closed it, I think. – Devanshu Nov 07 '17 at 06:38
  • Also,it saying True is Undeclared – Devanshu Nov 07 '17 at 06:40
  • I have declared it as a Integer and Now it's working, btw I don't think It's the Good way to do this, Is it? – Devanshu Nov 07 '17 at 06:43
  • if you want it to be repeated till user selects the exit option, you need a loop. The may be multiple ways to loop. E.g: //do not add the end options. int loop=1; while(loop){ you code here. printf("\n\t\t\t\t CALCULATOR"); printf("\nEnter the First Number: "); scanf("%d",&a); .... //return 0; remove the return statement printf("Do you want to continue (Y/N)"); getch(); //user can enter Y or N. //accordingly set the loop to 0 or 1. //0 will exit the loop. //1 will continue the loop. } Its simply a case of your personal preference and how you prefer to do it – akshaya pandey Nov 07 '17 at 06:45