Tortoise/Hare clock question

This is a discussion on Tortoise/Hare clock question within the C++ Programming forums, part of the General Programming Boards category; This is a problem from Deitel & Deitel's C++ How to Program(pg. 386,prblm. 5.17). It is a tortoise/Hare race simulation. ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    Tortoise/Hare clock question

    This is a problem from Deitel & Deitel's C++ How to Program(pg. 386,prblm. 5.17). It is a tortoise/Hare race simulation. In the beginning of the program description it states, "There is a clock that ticks once per second. With each tick of the clock, your program should adjust the position of the animals according....".

    My question is how do you set up a loop that executes once per second?

    This hasn't been taught in the book and any examples haven't been given so I'm stuck at the beginning.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Code:
    #include <windows.h>
    
    for (...)
    {
         Sleep(100); // 100 miliseconds
         ...
    }
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Sounds good and possibly the only and or best solution, but if you sleep for one second each time you do not take into account for the time of execution of the process between the loop. This will make each cycle through the loop slightly more than one second.

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Sounds good and possibly the only and or best solution, but if you sleep for one second each time you do not take into account for the time of execution of the process between the loop. This will make each cycle through the loop slightly more than one second.
    True, but Iīm pretty sure that you will not notice that (cycle)delay.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Can you help me debug this code now that it is almost complete?
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <windows.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        
      srand(time(0));
      
      int position_tortoise, position_hare=1;
      
      
      while (position_tortoise && position_hare <70){
        Sleep(100);
        
        int tortoise_decision, hare_decision=0;
        
        tortoise_decision=rand()%10+1;
        hare_decision=rand()%10+1;
        
        if (tortoise_decision<=5)
            position_tortoise=position_tortoise+3;
        if (hare_decision<=2)
            position_hare=position_hare;
        if ((tortoise_decision>5)&&(tortoise_decision<=7)){
            position_tortoise=position_tortoise-6;
            if (position_tortoise<1)
                    position_tortoise=1;
        }
        if ((hare_decision>2)&&(hare_decision<=4))
            position_hare=position_hare+9;
        if (tortoise_decision>7)
            position_tortoise=position_tortoise+1;
        if ((hare_decision>4)&&(hare_decision<=5)){
            position_hare=position_hare-12;
            if (position_hare<1)
               position_hare=1;
        }
        if ((hare_decision>5)&&(hare_decision<=8))
            position_hare=position_hare+1;
        if ((hare_decision>8)){
            position_hare=position_hare-1;
            
        for (int print_position=1;print_position<=70;print_position++){
            if ((position_tortoise==position_hare)&&(position_hare==print_position))
                cout << "OUCH";
            if (position_tortoise==print_position)
                cout << "T";
            if (position_hare==print_position)
                cout << "H";
            if (!((position_tortoise==position_hare)&&(position_tortoise==print_position)&&(position_hare==print_position)))
                cout << " ";
        }
      cout << endl;
        
      
        
          
      
      }
      if ((position_tortoise==position_hare))
        cout << "It's a tie!"<<endl;
      if ((position_tortoise>position_hare))
        cout << "Tortoise wins!"<<endl;
      if ((position_hare>position_tortoise))
        cout << "Hare wins!"<<endl;  
     
       system("PAUSE");	
      return 0;
    }
    Errors:

    C:\Dev-Cpp\mainth.cpp
    mainth.cpp: In function `int main(int, char**)':
    mainth.cpp:74: parse error at end of input

    make.exe: *** [mainth.o] Error 1

    Execution terminated

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You have a &#125; missing from somewhere in there.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    Actualy its and extra } i belive
    i see 4 { and } 5

  8. #8
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    On a sidenote, Sleep(100) sleeps for 100ms which is 1/10 of a second, not one second.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Iamien
    Actualy its and extra i belive
    i see 4 and 5
    Err no, I count 6 open and 5 close, or at least the find feature of my editor does.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  2. Outside influences on clock cycles? (clock_t)
    By rsgysel in forum C Programming
    Replies: 4
    Last Post: 01-08-2009, 06:15 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. ANN: The Fourth Contest: Alarm Clock, sign up here
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 59
    Last Post: 08-10-2002, 01:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21