1

I am a beginner in C... I have made a made a calculator type program which uses four basic functions in C using if-else loop. I want when the program comes to end(after the user has added, subtracted etc. etc. then there is a option "Y/N" so that the program can be restarted???" Here is the sample of the code

  #include<stdio.h>
int main()
{
    int choi;
    printf("*****Interactive Calculator*****");
    printf("\n\nChoose an option...");
    printf("\n\n1. Addition\n");
    printf("\n2. Subtraction");
    printf("\n\n3. Multiplication");
    printf("\n\n4. Division");
    printf("\n\nPlease Enter your Choice : ");
    scanf("%d",&choi);
if(choi==4)
    {
        float a=0,b=0,c=0;
        printf("\nEnter Divident :");
        scanf("%d",&a);
        printf("\nEnter the Divisor :");
        scanf("%d", &b);
        c=a/b;
        printf("\nThe Quotient is : %d\n\n",c);
        char choice;
        printf("Do you want to try it again?(Y/N) ");
        scanf("%c", &choice);
        // I want a code here so that the program can be restarted
        getch();
        return 0;
    }
    else
    {
        printf("\nErr#404-Invalid Character! Please Enter 1,2 or 3 !\n\n");
    }
end:
getch();
return 0;
}
user3500780
  • 11
  • 1
  • 8
  • 1
    Its called a *loop*, and should be covered in any C text or tutorial. – WhozCraig Apr 05 '14 at 09:48
  • 3
    And and....avoid `goto`. – Jayesh Bhoi Apr 05 '14 at 09:49
  • 3
    @Jayesh ...at least as a beginner, until you know where it is appropriate to use it. – glglgl Apr 05 '14 at 09:52
  • Another option, to use sparingly and when you have more experience with C, is to call `main()` from inside `main()`. Note: *I believe this is not possible in C++.* – pmg Apr 05 '14 at 09:57
  • @pmg Yeah, in C++ it's forbidden by standard to call `main()`. I'm surprised that it's not by C's standard as well. – Nemanja Boric Apr 05 '14 at 09:58
  • @glglgl from my point goto is good but when...way of using `goto` make it good.like http://stackoverflow.com/questions/245742/examples-of-good-gotos-in-c-or-c – Jayesh Bhoi Apr 05 '14 at 10:02
  • 1
    and and....at least as a beginner avoid `goto`. :) when you have grip then you automatically find the best way to use `goto` – Jayesh Bhoi Apr 05 '14 at 10:06
  • I never included goto in my orig in my program, I was trying diff methods and accidently posted it :P – user3500780 Apr 05 '14 at 10:36

6 Answers6

2

The best way would be to do some sort of a while loop.

int goAgain=1;
while (goAgain==1) {
   ... //Normal code here
   printf("Again?")
   scanf("%c",&again)
   if (again=='N') {
       goAgain=0;
   }
}

Or you could use a do-while loop as well

do {
       ... //Normal code here
       printf("Again?")
       scanf("%c",&again)
} while (again=='Y')

Basically, this will keep looping over the bit of code over and over until the person types N to end it.

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
2

A do-while loop would be most suitable for this purpose.

int main() {
    char choice;
    do {
        // Calculator stuff here...

        printf("Do you want to try it again? (Y/N) ");
        scanf("%c", &choice);
    } while (choice == 'Y');
}

Edit: As it turns out, there is another problem with the program above, which is that scanf() reads a character but leaves a Newline character in the buffer. Therefore, if the user types YEnter, the program will repeat once (choice == 'Y' the first time), then exit (choice == '\n' the second time).

It is therefore necessary to keep reading until the Newline has been consumed.

int main() {
    char choice;
    do {
        // Calculator stuff here...

        printf("Do you want to try it again? (Y/N) ");
        choice = getchar();
        while (choice != '\n' && getchar() != '\n') {};
    } while (choice == 'Y' || choice == 'y');
}
200_success
  • 7,286
  • 1
  • 43
  • 74
0

char continue = 'Y'

while (continue == 'Y') {
   ... //Normal code here
   printf("Again?")
   scanf("%c",&continue)

}
JajaDrinker
  • 652
  • 6
  • 15
0

you can try like this, avoid go to

 #include<stdio.h>
 int main()
 {
      int choi;
      while(true)
      {
          printf("*****Interactive Calculator*****");
          printf("\n\nChoose an option...");
          printf("\n\n1. Addition\n");
          printf("\n2. Subtraction");
          printf("\n\n3. Multiplication");
          printf("\n\n4. Division");
          printf("\n\n5. Exit");
          printf("\n\nPlease Enter your Choice : ");
          scanf("%d",&choi);
          if(choi==1)
          {
          }
          else if(choi==2)
          {
          }
          else if(choi==3)
          {
          }
          else if(choi==4)
          {
          }
          else if(choi==5)
          {
           return 0;  //exit(0);
          }
          else
          {
               printf("\nErr#404-Invalid Character! Please Enter 1,2,3,4 or 5 !\n\n");
          }   
      }
 return 0;
 }
Himanshu
  • 4,327
  • 16
  • 31
  • 39
0

By using do-while loop, which is generally used for menu-driven programs.

#include<stdio.h>
int main()
{
    int choi;
char choice;

do{
printf("*****Interactive Calculator*****");
    printf("\n\nChoose an option...");
    printf("\n\n1. Addition\n");
    printf("\n2. Subtraction");
    printf("\n\n3. Multiplication");
    printf("\n\n4. Division");
    printf("\n\nPlease Enter your Choice : ");
    scanf("%d",&choi);
if(choi==4)
    {
        float a=0,b=0,c=0;
        printf("\nEnter Divident :");
        scanf("%d",&a);
        printf("\nEnter the Divisor :");
        scanf("%d", &b);
        c=a/b;
        printf("\nThe Quotient is : %d\n\n",c);
        char choice;
        printf("Do you want to try it again?(Y/N) ");
        scanf("%c", &choice);
        // I want a code here so that the program can be restarted
        getch();
        return 0;
    }
    else
    {
        printf("\nErr#404-Invalid Character! Please Enter 1,2 or 3 !\n\n");
    }

printf("Want to continue (y/n)?");
scanf("%d", &choice);  // Enter the character
}while (choice == 'y' || choice == 'Y');
end:
getch();
return 0;

P.S.: I would suggest you to use switch case, instead of if-else statements to do the job.

Himanshu Aggarwal
  • 1,803
  • 2
  • 24
  • 36
-2

You can also use a goto:

   int main() {
     ...
     if (c=='y') {
       main();
     } else { 
       goto end;
     }
     end:
       ...
   }
EvenLisle
  • 4,672
  • 3
  • 24
  • 47
Susahrut
  • 11
  • 2
  • 1
    Although calling the main function is not prohibited in C, I cannot come up with an example where it wouldn't be bad practice. Goto is also quite inferior to loops in this example, and should be used only if its use promotes readability, structure and efficiency more than the use of other constructs. An example where goto is a feasible solution: http://stackoverflow.com/questions/788903/valid-use-of-goto-for-error-management-in-c – EvenLisle Apr 05 '14 at 10:21