Monday, 11 November 2013

How to repeat a program till the user wants to end it!

Here I am making a simple program-plug-in (as I call it) to execute the program more than once, if the user wants it. The task is to ask the user if he/she wants to execute the program again after one run. 

Suppose I have a program to find the factorial of a number. 
#include<iostream.h>
#include<conio.h>

long Factorial(long n)
{
     if(n==1){return 1;}
     else{return (n*Factorial(n-1));}
}

int main()
{
     long x;
     cout<<"Enter the number you want to find the factorial of : ";
     cin>>x;
     cout<<x<<"! = "<<Factorial(x)<<endl;
     return 0;
}

After one run, program automatically terminates and If I want to run it again, I will have to open it again. To solve this there are many methods. Im gonna show you two of them. 


  • Solution 1 - The viable one
                  We could use the old school 'GOTO' statement here. Just go ahead and include a label at the beginning of the program execution, and at the end of a run, ask if the user wants to run the program again, by a character choice,and if he says yes, just add a 'goto' statement there.
Syntax:                      label_name:
                                                        (program)
                                              
                                             goto label 


Here is the modified code after inclusion of goto:
#include<iostream.h>
#include<conio.h>

long Factorial(long n)
{
     if(n==1){return 1;}
     else{return (n*Factorial(n-1));}
}

int main()
{
     START:
     long x;
     char choice;
     cout<<"Enter the number you want to find the factorial of : ";
     cin>>x;
     cout<<x<<"! = "<<Factorial(x)<<endl;
     cout<<"Do you want to go again? [y/n] : ";
     cin>>choice;
     if (choice == 'y')
     {
         goto START;
     } 
 return 0;
}


This is a perfectly viable solution and no one cant say that it wont work. But, goto statement is considered harmful by many people since it breaks the beauty of structured programming. You can use it, of course, but there is also a generally preferred alternative for 'goto' statement - the iterators!

  • Solution 2 - The recommended one
                   The generally recommended alternative for using 'goto' is to use loops like do-while. In this case we are putting the whole program inside a giant do-while loop. We use it here because of its exit-controlled behavior. After each run, we keep asking the user if he/she wants to go again, and as long as the user says yes, the do-while loop keeps iterating again and again. 
Syntax:                   do{
                                                program_statements;

                                             }while(statement);

Here is the program code after the inclusion of do-while loop:
#include<iostream.h>
#include<conio.h>

long Factorial(long n)
{
    if(n==1){return 1;}
    else{return (n*Factorial(n-1));}
}

int main()
{
    char choice;
    long x;
    do
    {
       cout<<"Enter the number you want to find the factorial of : ";
       cin>>x;
       cout<<x<<"! = "<<Factorial(x)<<endl;
       cout<<"Do you want to go again? [y/n]"<<endl;
       cin>>choice; 
    }while ( choice!='n'&&choice=='y' && choice=='Y');
}
 

Keep in mind that the loop always comes inside the main() function whether or not the program has external classes or functions. Also remember that we may use any loop instead of do-while.


That's all for today, folks! Happy Coding!!!

No comments:

Post a Comment

You might also like