# C Programming: How can you repeat the program?



## Coma

Move it to another function which returns a boolean value that says whether to run it again or quit.

Then, do this in main:
while (yourfunc() == true) {
yourfunc();
}


----------



## Slime

I'm sorry I'm very noob, I've learned a little bit of C just for this simple program. how do I move it to another function ??
thansk a lot!


----------



## Barbaroti

You could put your code between a a while, so it would continue to run until being told to stop.

Example (Just for an idea):

Code:



Code:


#include

main

//declare variables
bool run=true

while bool(run==true) // Will keep running the program until being told to stop
printf(1. keep running)
printf(2. stop)
scanf(%d,  &choice)

switch(choice)
case1: break;
case2: run=false; break; // bool variable was changed, code won't run after this
default: printf(not an option); break;
// sw ends
//while ends
//main ends


----------



## Madog

Or, you do it the better way..

Code:



Code:


     do
        {
          cout << "Would you like to exit? " ;
                 cin >> repeat;
        }
        while((repeat == 'y') || (repeat == 'Y'));

You will need a boolean variable but its exactly what you were talking about..
What happens if anything besides y or Y is entered the program closes, so even exit could be entered and it will close.

You can use this command prompt command to clear the console when the program repeats itself. You would place this at the very bottom do while loop if you want it to clear the screen each time the program repeats.

Code:



Code:


             system("cls");


----------



## 0x62 0x70

Quote:


> Originally Posted by *Slime*
> 
> I'm sorry I'm very noob, I've learned a little bit of C just for this simple program. how do I move it to another function ??
> thansk a lot!


generally if you want to continue something, but you don't have a fixed-number of iterations you want to put it through (you aren't aware of any bounds), you would put that function in a while loop until something "triggers" it to break out of the loop. in this case, the most basic thing you can do is to read for an input (such as you hitting ENTER after your main code is done executing), and evaluate it. in this case, if you hit ENTER, then that should tell the while loop to continue to go. if you hit anything else, then you can have it exit the program.

so for example, in a C program, in its most basic structure, it structured as:

Code:



Code:


int main()
{
    // create your trigger here
    int userDoesNotWantToExit = 1; // true

    // enter the while loop (this is where you put your code that you want to repeat some indefinite time)
    while( userDoesNotWantToExit )
    {
        // your code here
        ...

       // after code is completed, you MUST update the condition yourself
       // grab input from keyboard and have it check for a newline (to see if enter has been hit)
       // else, exit the program
       // this should be an if-else (if the input is newline, then 'userDoesNotWantToExit ' = 1, else 0)
       // if the input is not a newline then the int is 0 and the while condition is not executed again

        return 0;
}

IF you want to execute something at least ONCE, then you would use a do-while as stated above. this guarantees that you'll at least perform some function (such as the first time a prompt for calculating some integers is displayed) once.

in this case though, the while loop is entirely equivalent because you're not changing the boolean value in between when its first declared, and when the function is actually called (there's no code in between that could possibly change the value before it goes into the while loop - nor anything in between that the while loop depends upon).


----------



## Slime

Sorry I can't get it to work...

Can you compile this and correct me please ?
I got it to loop but when I enter an alphabetic value it keeps repeating till infinity

I'd just want to correct that...

Code:



Code:


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   int choix;
   double Rpm, Result;

   // Presentation
   printf("This program will **** (censored)\n\n");

   // List of calculation choices
   printf(" 1. Hello             2. Blablabla              3. Third\n");
   printf(" 4. Another blabla\n\n");

   while(1)
   {
     printf("Option #");
     scanf("%d", &choix); 
     printf("Enter the ", choix);

     switch (choix) // choix awaits for a scanned number from the input (keyboard) at ("%d", &choix)
     {  
        case 1:
           printf("Rpm: ");
           scanf("%lf", &Rpm);
           Result = 360000/Rpm;
           printf("The value is = %.lf dollars.\n\n",Result);
           break;
        case 2:
           printf("Rpm: ");
           scanf("%lf", &Rpm);
           Result = 240000/Rpm;
           printf("A whole %.lf peanuts.\n\n",Result);
           break;
        case 3:
           printf("Rpm: ");
           scanf("%lf", &Rpm);
           Result = 160000/Rpm;
           printf("Aaaaaaaaaaaaa = %.lf turds.\n\n",Result);
           break;
        case 4:
           printf("Rpm: ");
           scanf("%lf", &Rpm);
           Result = 180000/Rpm;
           printf("Numer 4 mofo = %.lf .\n\n",Result);
           break;

        default: // If the user enters an invalide option, the following message will show up:
           printf("number, from 1 to 4, corresponding to what you want to know. The number you entered was not valid.\n\n");
     } // End case
   } // End while(1)
   getch();
   return 0;
}


----------



## Slime

Quote:


> Originally Posted by *Madog*
> 
> Or, you do it the better way..
> 
> Code:
> 
> 
> 
> Code:
> 
> 
> do
> {
> cout << "Would you like to exit? " ;
> cin >> repeat;
> }
> while((repeat == 'y') || (repeat == 'Y'));
> 
> You will need a boolean variable but its exactly what you were talking about..
> What happens if anything besides y or Y is entered the program closes, so even exit could be entered and it will close.
> You can use this command prompt command to clear the console when the program repeats itself. You would place this at the very bottom do while loop if you want it to clear the screen each time the program repeats.
> 
> Code:
> 
> 
> 
> Code:
> 
> 
> system("cls");


Thanks!!! I'll that after my bug is solved


----------



## sktfreak

Quote:


> Originally Posted by *Slime*
> 
> Sorry I can't get it to work...
> Can you compile this and correct me please ?
> I got it to loop but when I enter an alphabetic value it keeps repeating till infinity
> I'd just want to correct that...


There's an even more simplistic way to do it. Just add the option to exit in your menu.
This code will compile as well.

Code:



Code:


int main(int argc, char *argv[])
{
   int choix;
   double Rpm, Result;

   // Presentation
   printf("This program will **** (censored)\n\n");

   // List of calculation choices
   printf(" 1. Hello             2. Blablabla              3. Third\n");
   printf(" 4. Another blabla    5. Exit\n\n");

   while(1)
   {
     printf("Option #");
     scanf("%d", &choix); 
     printf("Enter the ", choix);

     switch (choix) // choix awaits for a scanned number from the input (keyboard) at ("%d", &choix)
     {  
        case 1:
           printf("Rpm: ");
           scanf("%lf", &Rpm);
           Result = 360000/Rpm;
           printf("The value is = %.lf dollars.\n\n",Result);
           break;
        case 2:
           printf("Rpm: ");
           scanf("%lf", &Rpm);
           Result = 240000/Rpm;
           printf("A whole %.lf peanuts.\n\n",Result);
           break;
        case 3:
           printf("Rpm: ");
           scanf("%lf", &Rpm);
           Result = 160000/Rpm;
           printf("Aaaaaaaaaaaaa = %.lf turds.\n\n",Result);
           break;
        case 4:
           printf("Rpm: ");
           scanf("%lf", &Rpm);
           Result = 180000/Rpm;
           printf("Numer 4 mofo = %.lf .\n\n",Result);
           break;
       case 5:
           return 0;
           break;

        default: // If the user enters an invalide option, the following message will show up:
           printf("number, from 1 to 5, corresponding to what you want to know. The number you entered was not valid.\n\n");
     } // End case
   } // End while(1)
   return 0;
}


----------



## BigLisy

As you suggested in the original post, you could very simply make the main function return itself at the end.


----------

