Thread: What is wrong with my type defination/randomization program?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    156

    What is wrong with my type defination/randomization program?

    By the way, this isn't a serious project. I'm just playing around with C++. I barely use typedef and/or #define. I'm just testing it out. Also, I know very little about pointers but I thought I knew enough to do this.........take a look:


    #include <iostream>
    #include <conio.h>
    #include <conio.c>
    #include <string.h>
    #include <time.h>

    typedef unsigned int digit;

    int randomize();
    digit *number;

    void main()
    {
    digit i, j;
    for (i=0;i<=6;i++) {
    randomize(); }
    cout << "\n";

    while (j < 10) {
    j++;
    cout << j << ", "; }

    getch();
    }

    int randomize()
    {
    srand(time(NULL));
    number=rand() % 25;
    cout << number << endl;
    return number;
    }
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    ok
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main()
    Incorrect, main returns int and nothing else.

    >cout << "\n";
    This shouldn't work because you included the new iostream header but you didn't specify that you are using cout.

    >j++;
    j hasn't been initialized, so it contains a garbage value at this point.

    >getch();
    Nonstandard, you can use cin.get() with the same results and your program will be more portable.

    >srand(time(NULL));
    This will have little effect because your loop will run so fast, all of the numbers will probably be the same. It would be much better to call srand once at the beginning of the program so that the seed is not reset with every iteration of the loop.

    >number=rand() % 25;
    Bzzzt! number is a pointer that doesn't point to anything, this will cause an access violation. This is also a poor way to use rand since the % operator works on the low order bits of the generator, and that usually results in not as random as you'd like numbers. A better way is to use the high order bits by dividing RAND_MAX by the number and then retrieving the random number in that range:
    number = rand() / ( RAND_MAX / 25 )

    >return number;
    digit is typedef'd as an unsigned int yet you return an int, number is of the digit type so you would do better to return digit.
    Code:
    #include <iostream> 
    #include <cstring> 
    #include <ctime>
    using std::cout;
    using std::cin;
    using std::endl;
    
    typedef unsigned int digit; 
    
    digit randomize();
    digit fill; 
    digit *number = &fill; 
    
    int main() 
    { 
      digit i, j = 0; 
      srand(time(NULL));
      for (i=0;i<=6;i++) { 
        randomize(); 
      } 
      cout << "\n"; 
    
      while (j < 10) { 
        j++; 
        cout << j << ", "; 
      }
      cin.get();
      return 0; 
    } 
    
    digit randomize() 
    {  
      *number = rand() / ( RAND_MAX / 25 ); 
      cout << *number << endl; 
      return *number; 
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    thanks man
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: [email protected]

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM