Thread: Help With Functions and Random Numbers

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    Help With Functions and Random Numbers

    Hi, i was working on this for my class and i can't seem to get this to work. I am not quite done yet but i have a few problems. After my rules and game function, i would like it to return to the home screen (call the main) again but it dosen't seem to do that. When i click 1 for the rules it displays the rules then plays the game instead of returing home. Then i the random number dice are not number between 1 to 6. Why ? Thanks

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    using namespace std;
    
    int Dice (int dice1, int dice2)
    {
    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);
    dice1 = rand() % (5 + 1)+1;
    dice2 = rand() % (5 + 1)+1;
    return (dice1, dice2);
    }
    
    int rules()
    { 
        system("cls");
    cout << " The Rules Are Quite Simple. We Roll Two Dice.\n -If Even, Player 1 Gets a Point\n If Odd, Player 2 Gets a Point\n";
    system("Pause");
    int main ();
    }
    
    int game()
    { 
        int times;
        system("cls");
        cout << "How Many Times Would You Like to Roll?";
        cin >> times;
        int Dice (int dice1, int dice2);
        int main ();
    }
    
    
    
    int main()
    
    {
        int key;
    cout << "Menu\n Press 1 for Rules\n Press Any Key to Play\n"  ;
    cin >> key;
    if (key==1)
       {rules();}
       else
       {game();}
    int dice1;
    int dice2 ;   
    cout << "First Dice = " << dice1 <<  endl;
    cout << "Second Dice = " << dice2 << endl;
    
    system("pause");
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by CaliJoe View Post
    Hi, i was working on this for my class and i can't seem to get this to work. I am not quite done yet but i have a few problems. After my rules and game function, i would like it to return to the home screen (call the main) again but it dosen't seem to do that. When i click 1 for the rules it displays the rules then plays the game instead of returing home. Then i the random number dice are not number between 1 to 6. Why ? Thanks

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    using namespace std;
    
    int Dice (int dice1, int dice2)
    {
    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);
    dice1 = rand() % (5 + 1)+1;
    dice2 = rand() % (5 + 1)+1;
    return (dice1, dice2);
    }
    
    int rules()
    { 
        system("cls");
    cout << " The Rules Are Quite Simple. We Roll Two Dice.\n -If Even, Player 1 Gets a Point\n If Odd, Player 2 Gets a Point\n";
    system("Pause");
    int main ();
    }
    
    int game()
    { 
        int times;
        system("cls");
        cout << "How Many Times Would You Like to Roll?";
        cin >> times;
        int Dice (int dice1, int dice2);
        int main ();
    }
    
    
    
    int main()
    
    {
        int key;
    cout << "Menu\n Press 1 for Rules\n Press Any Key to Play\n"  ;
    cin >> key;
    if (key==1)
       {rules();}
       else
       {game();}
    int dice1;
    int dice2 ;   
    cout << "First Dice = " << dice1 <<  endl;
    cout << "Second Dice = " << dice2 << endl;
    
    system("pause");
    
    }
    A few things are wrong with this... I've never heard of "calling main" if it some how has been returning to main I believe it is a bug. What you should do is loop. Looping is very easy and very common. Also, declare all of your variables at the start of the function. So put dice1/dice2 right after key... Once you move out of main you can't touch dice1/dice2 unless you've passed them to a function by calling it IN main... and your call to your function Dice in the game() function tries to use those variables but it isn't called from main it is called from game() where the variables dice 1 and dice 2 do not exist. What you want to do is pass 2 ints that you create in the game() function to the Dice function.

    As for looping... a do/while loop should be sufficient. If you don't understand most of what I said you may want to read your book over again... or look up information on looping and variable scope.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Numbers
    By JamesG89 in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2007, 03:00 PM
  2. Will we ever have truly random numbers?
    By jalnewbie in forum C Programming
    Replies: 7
    Last Post: 11-30-2006, 02:14 PM
  3. Random Numbers
    By punter in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2006, 06:06 PM
  4. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 12:29 PM
  5. time Delays and Random functions
    By markphaser in forum C++ Programming
    Replies: 17
    Last Post: 02-20-2006, 07:10 PM

Tags for this Thread