Thread: Countdown in Program

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Countdown in Program

    Hi,

    i am creating a washing machine program and when you select a command for example "wash" it will wash. what i want it to do is when wash is selected cout down to zero and then return to the main screen.

    i have the code for the coutdown but i am unsure how to implement it into an if statement.

    here is the timer code.

    Code:
    void wait ( int seconds )
    {
      clock_t endwait;
      endwait = clock () + seconds * CLK_TCK ;
      while (clock() < endwait) {}
    }
    
    int main2 ()
    {
    
      int n;
      cout << "Washing...\n";
      for (n=10; n>0; n--)
      {
        printf ("%d\n",n);
        wait (1);
      }
      cout << "WASHING COMPLETE\n";
    
     }

    i would like this code to activate for example at this part of the program.

    Code:
    if (number==1)
    {
            system("cls");
            cout <<"WASHING" <<endl <<endl;
    }

    Can anyone help how to get it to work?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I'mnot sure I understand you but could you not just nest the loop inside the conditional statement.

    And why do you use cout and then suddenly printf in the loop?

    If you are going to use system then you are making your code Windows only anyway so you might aswell just include Windows.h and then use Sleep(x).
    Last edited by bumfluff; 09-04-2006 at 05:02 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    http://en.wikipedia.org/wiki/Finite_state_machine
    Basically, you have a series of states - Washing, Rinsing, Finished etc and a means of getting between the various states (like an elapsed time, or some event like "no water").
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    UK
    Posts
    13
    maybe i'm missing something but couldn'y you just do

    Code:
    if (number==1) {
            system("cls");
            cout <<"WASHING" <<endl <<endl;
    
            int result = main2();
    }
    also if you declare a function with a return value then...well, you must return a value

    Code:
    int main2 () {
    ........
    return (1);
    }
    is it just a CLI/console application?
    do you have your menu code done?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM