Thread: This Is Really Weird...Changing Value of Int

  1. #1
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050

    This Is Really Weird...Changing Value of Int

    I have a program that is creating random numbers...at least this is the best randomization I've seen out of any program (I've tried a lot of randomizing number algorithms and most of them aren't very effective). This program works perfectly fine unless I manipulate this one array of integers. The name of it is
    " int num_t [ ] . Whenever I put the number 50, 100, 140, 150, 15 it gives me a memory referrencing error when I run the .exe. Whenever I put the number 160, 165 it doesn't display anything but no errors occurr when running the program. Whenever I put the numbers 200, 300, 170, 190, 400, 499, 500 it runs just as it should. Here is the code for the program...I have no idea why this is happening.

    Code:
    #include <iostream> 
    #include <time.h> 
    #include <conio.c> 
    #include <windows.h> 
    
    using namespace std; 
    
    void store();
    void display();
    
    int num_t[170];   /* with 170 being in there the program runs
                                    fine, but whenever I put in certain numbers it
                                    messes up as I explained */
    
    int x = 0, y = 0, z = 0;
    
    int main() 
    { 
        bool ACTIVE = true;    
        time_t hold_t[50];
         
        hold_t[x] = time(NULL); 
       
        while(ACTIVE) 
        {
        num_t[y] = hold_t[x];  
        store();
        x++;
        y++;        
        if(x > sizeof(hold_t))
         { 
           ACTIVE = false;       
         } 
            
        }
       
    display();    
                                              
    getch();           
    return 0; 
    } 
    
      void store()
       {
          num_t[z] = num_t[y];      
          z++; 
       }
       
       void display()
        {      
          z = 0;
          for(int pass = 0; pass < 51; pass++)
           {
             system("CLS"); 
             cout << num_t[z];
             Sleep(500);
             z++;
            }
         }
    I've tried chaing around the values for hold_t and pass, but I narrowed the error down to when I change the value for num_t. I guess all in all it doesn't really matter, except that my curiousity has the best of me. I have absolutely no idea why it's doing this.
    Last edited by TechWins; 05-12-2002 at 02:55 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if(x > sizeof(hold_t))
    If you're expecting this to be the same as

    if( x > 50 )

    Then that's your problem. sizeof() counts bytes, not elements.

    If you really want to count the number of elements in an array, do

    if(x > sizeof(hold_t)/sizeof(hold_t[0]) )

    And no, this doesn't work when the array is passed as a pointer inside a function. It only works when the array itself is in scope.

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    TRY THIS

    I dont know if this will help... It is a password generator I made getting random values out of arrays.
    What is C++?

  4. #4
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking go go go!

    Don't use arrays. Try making dynamic objects - they can be much more effective.

  5. #5
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    caffine + billholm = 'BAD';
    What is C++?

  6. #6
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Thanks for the link, Vicious, but what Salem told me fixes the problem and thank you too Salem.

    billholm, I'll take that into consideration.

  7. #7
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    yah

    I figured my link wasnt what you needed...

    But its good publicity.

    What is C++?

  8. #8
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    HAY

    Just what are you up to anyway...

    What are you making? hm....

    What is C++?

  9. #9
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking Hehehe

    Yah I think I had too much caffeine the other night.

    But it helped me finish coding my polymorphic linked list class and used it to generate my random numbers effectively

  10. #10
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    What are you making? hm....
    Well, I was making a variation of a pac-man game, but I put that game to a halt because I think it looks like crap being console based (text only). For that program I was simply 'screwing around' so to speak. I got the idea from the 'Clock 2' thread earlier today. After today, though, I planon going back and updating my maze game, Escape to Safety. But who knows with me!?

    Two more weeks and I'll have all the time in the world to program...woohoo.




    I really gotta stop posting and get started on my essay lol

  11. #11
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Text based PacMan will kill you with all the possibilities.
    Ive tried
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM