Thread: help runtime error because of rand()

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    241

    help runtime error because of rand()

    Code:
    #include <iostream>
    
    int main()
    {
        int Age;
        int IDontFkingKnow;
        int array[16][16];
        std::cout<<"Please enter age: ";
        std::cin>>Age;
        while (Age<16, Age++)
        {
            IDontFkingKnow=rand();
            array[Age][IDontFkingKnow]=Age * IDontFkingKnow;
        }
        std::cout<<Age<<"*"<<IDontFkingKnow<<"="<<array[Age][IDontFkingKnow]<<" ";
        std::cout<<"\n";
        std::cin.get();
        return 0;
    }
    It compiles perfectly but how can I use rand to only go thru 0-15?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    thanks for the quick response

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    while (Age<16, Age++)
    You might want to check the format for a while() loop again. That statement is eqivalent to:

    while(Age++)

    which will result in an infinite loop if the user enters a positive number for their age. The 'comma operator' first evaluates what on its left and what's on its right, and then it replaces both terms by the result of what's on the right.
    Last edited by 7stud; 03-05-2006 at 04:00 AM.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    Quote Originally Posted by 7stud
    Code:
    while (Age<16, Age++)
    You might want to check the format for a while() loop again. That statement is eqivalent to:

    while(Age++)

    which will result in an infinite loop if the user enters a positive number for their age. The 'comma operator' replaces what's on its left and what's on its right, by the result of what's on the right.
    lol thanks didnt know that, that could be why it keeps crashing :-p

    but im currently working on a tic tac toe game, but i dont know how to use arrays that well...
    let alone know where to start
    Code:
    #include <iostream>
    #include <windows.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        char board[3][3];
        char X;
        char O;
        string option;
        
        cout<<"Want to play a game of tic tac toe?\n";
        cin>>option;
        if (option=="Yes"||option=="yes")
        {
            goto start_of_game;
        }
        if (option=="No"||option=="no")
        {
            goto end_of_game;
        }
        start_of_game:
            cout<<"["<<board<<"]["<<board<<"]["<<board<<"]\n";
            cout<<"["<<board<<"]["<<board<<"]["<<board<<"]\n";
            cout<<"["<<board<<"]["<<board<<"]["<<board<<"]\n";
            cin.get();
        end_of_game:
        cin.get();
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    goto start_of_game;

    goto end_of_game;
    That would be something to avoid like the plague.

    You really need to practice the basics with simpler programs:

    1) Declare an array of integers and use a for-loop to assign the numbers 0-9 to the elements of the array. Then output the contents of the array using another for-loop.

    2) Use a while loop to prompt the user for a number, and then display the number they entered back to them. Keep doing that until the user enters the number -1.

    3) Declare a small two dimensional array with 3 rows and 2 columns. Use two for-loops, one nested inside the other to get input from the user to enter into the array. Then display all the numbers entered into the array using two nested for loops.

    4) Read some tutorials on functions, and practice by creating some simple functions. You can use functions to do some of the things you did above in main().
    Last edited by 7stud; 03-05-2006 at 04:15 AM.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    I decided to start fresh
    Code:
    #declare array 0,1,2,3,4,5,6,7,8,9
    #include <iostream>
    
    int main()
    {
        int array[3][2];
        int input;
        int X;
        int O;
        while(input>=0)
        {
            std::cout<<"Please enter a number 0-9 or -1 to quit: ";
            std::cin>>input;
            std::cout<<input;
        for(X=0;X<4;X++)
        {
            for(O=0;O<3;O++)
    I have to start thinking harder
    Last edited by bikr692002; 03-05-2006 at 04:28 AM.

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    #declare array 0,1,2,3,4,5,6,7,8,9
    What?

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    241
    I dont know i dont know how to declare arrays so that seemed logical :'(

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int array[10];
    int array_initialized[10] = {1, 2, 3};
    int array_default_size[] = {1, 2, 3, 4, 5};
    int a[10], x;
    for(x = 0; x < 10; x ++) a[x] = x;
    See the tutorials or your book.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    #declare array 0,1,2,3,4,5,6,7,8,9

    I dont know i dont know how to declare arrays so that seemed logical :'(
    It did? Why? Maybe you should try:

    #Mr. Compiler please execute the program I am thinking of right now;
    Last edited by 7stud; 03-05-2006 at 03:00 PM.

  12. #12
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Code:
    int array[3][2];
    
    for(X=0;X<4;X++)
    {
         for(O=0;O<3;O++)
    This part isn't going to work. I'd suggest that you just use one dimension in your array, that will make life a lot easier I also recommend you to read a tutorial about arrays. When you create an array, you specify how many 'boxes' it should contain. You specify that it should have 3 boxes, but later you try to access the fourth box which will give you a crash.

    Keep struggeling, I remember how hard I thought it was to learn programming at first. And it was not easy.

    Good luck

    The Wazaa

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  5. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM