Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C Programming: How can you repeat the program?
New Posts  All Forums:Forum Nav:

C Programming: How can you repeat the program?

post #1 of 10
Thread Starter 
Hello folks. I made a little program (my first one) and it serves well it's purpose. The problem is that when it gives the result, as soon as we hit a key on the keyboard, it exits.
I'd rather have it repeating it's main function.

Thank you very much for any help
Edited by Slime - 11/11/11 at 2:23pm
post #2 of 10
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();
}
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
Akiyama Mio
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6420 @ stock, 0.98v Asus P5N-E SLI Gainward GTX 460 1GB @ 800/1600/1900 2x2GB Kingston @ 800MHz 5-5-5-15 2T 
Hard DriveOptical DriveOSMonitor
WD 250GB, 320GB SATA/3, 16MB Cache, Seagate 1TB LG GSA-H62N 18x SATA Ubuntu 9.10 x86 & Win7 x86 Asus VW222U 
KeyboardPowerCase
Logitech Classic Corsair 650HX NZXT Apollo Black 
  hide details  
Reply
post #3 of 10
Thread Starter 
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!
post #4 of 10
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:
#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
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
post #5 of 10
Or, you do it the better way..
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:
             system("cls");
post #6 of 10
Quote:
Originally Posted by Slime View Post

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:
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).
Edited by 0x62 0x70 - 11/11/11 at 12:04pm
Mr Jiggawatts
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T@4GHz@1.46V Ultra LLC/3GHz CPU-NB@1.33V Asus Crosshair V Formula MSI GTX560-TI 1GB TF II Patriot Viper Xtreme 1600 8GB (2x4GB) 8-9-8-24 
Hard DriveOSMonitorKeyboard
Intel X25-V 40GB, Kingston SSDNow 64GB Windows 7 Ultimate LG Flatron W2353V Razer Arctosa 
PowerCaseMouse
Antec HCG-900W Antec Lanboy Air Yellow Razer Abbysus 
  hide details  
Reply
Mr Jiggawatts
(13 items)
 
  
CPUMotherboardGraphicsRAM
1090T@4GHz@1.46V Ultra LLC/3GHz CPU-NB@1.33V Asus Crosshair V Formula MSI GTX560-TI 1GB TF II Patriot Viper Xtreme 1600 8GB (2x4GB) 8-9-8-24 
Hard DriveOSMonitorKeyboard
Intel X25-V 40GB, Kingston SSDNow 64GB Windows 7 Ultimate LG Flatron W2353V Razer Arctosa 
PowerCaseMouse
Antec HCG-900W Antec Lanboy Air Yellow Razer Abbysus 
  hide details  
Reply
post #7 of 10
Thread Starter 
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:
#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;
}


Edited by Slime - 11/11/11 at 2:27pm
post #8 of 10
Thread Starter 
Quote:
Originally Posted by Madog View Post

Or, you do it the better way..
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:
             system("cls");

Thanks!!! I'll that after my bug is solved wink.gif
post #9 of 10
Quote:
Originally Posted by Slime View Post

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:
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;
}

Edited by sktfreak - 11/12/11 at 8:43am
post #10 of 10
As you suggested in the original post, you could very simply make the main function return itself at the end.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C Programming: How can you repeat the program?