Thread: Code returning wrong numbers

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Code returning wrong numbers

    I'm just beginning to create the infamous tortoise and hare project, but cannot get my code to print out the correct corresponding numbers. Any ideas?

    Code:
    #include <iostream>
    #include <cstdlib> // contains prototypes for functions srand and rand
    #include <ctime> // contains prototype for function time
    
    using namespace std;
    
    int troll( void );
    int hroll( void );
    int main()
    {
    
       srand( time( 0 ) );
       int t = 0;
       int h = 0;
    
       
       cout << "turtle: " << troll() << endl;
       cout << "hare: " << hroll() << endl;
       
       if ( troll() >= 8 )
          ( t = 1 );
       else if ( troll() >= 6 )
          ( t = -6 );
       else if ( troll() <= 5 )
          ( t = 3 );
     
          
       else if ( hroll() >= 9 )
          ( h = 2 );
       else if ( hroll() >= 6 )
          ( h = 1 );
       else if ( hroll() == 5 )
          ( h = 12 );
       else if ( hroll() >= 3 )
          ( h = 9 );
       else if ( hroll() >= 1 )
          ( h = 0 );
          
       else if ( h < 1 )
          h = 0;
       else if ( t < 1 )
          t = 0;
    
       
       
       cout << "t: " << t << endl;
       cout << "h: " << h << endl;  
    
          
    return 0;
    
    } // end main
    // roll movement of tortoise and hare
    int troll( void ) {
    
       int troll = 0;
    
       troll = ( 1 + rand() % 10 );  // pick random number between 1 and 10
          
       return troll;
    } // end function troll
    
    int hroll( void ) {
    
       int hroll = 0;
       
       hroll = ( 1 + rand() % 10 );   // pick a random number between 1 and 10
    	 
       return hroll;
    } // end function hroll
    THE redheaded stepchild.

  2. #2
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    for example, I just ran it and it says
    turtle: 4
    hare: 9
    t: -6
    h: 0

    when it should read
    turtle: 4
    hare: 9
    t: 3
    h: 0
    THE redheaded stepchild.

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    One, just flat out stop using the same name for variables

    -6 is one of your options.
    Every time you call one of your if/else-if statements you are getting a new random number.

    I think what you are trying to get:

    Create two new int variables, and set them to

    Int x = troll();
    Int y = hroll();

    And then in your first cout statements output the x and y, and compare the x and y in your if’s and else-if statements.

  4. #4
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Aha! Enahs helps me once again. Simple explanation. Thank you!
    THE redheaded stepchild.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong with this piece of code?
    By psycho88 in forum C Programming
    Replies: 3
    Last Post: 12-03-2005, 11:28 PM
  2. Replies: 18
    Last Post: 11-04-2005, 02:41 PM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  4. what's wrong with the following code?
    By catalyst in forum C Programming
    Replies: 1
    Last Post: 11-07-2003, 04:30 AM
  5. what is wrong with this code?
    By Jennifer in forum Windows Programming
    Replies: 1
    Last Post: 01-06-2002, 08:50 AM