Thread: Data Storage Question, and Dynamic variables?

  1. #1
    #junkie
    Join Date
    Oct 2004
    Posts
    240

    Post Data Storage Question, and Dynamic variables?

    Ok iv got 2 questions off the bat, and would also like my code to be looked at to see if you can see any major problems offhand. I mean like style problems, but first, questions hehe.

    1.) 'Data Storage'
    I am making a little text based game just to get the syntax of basic functions into my head, its all going fine but i came up to a little problem. In this game you roll Six dice, well to keep track of each die i used an array like "array[7]" and used 1-6 to keep track of the dice. 0 is an empty value just cuz this seemed easier lol. Well now i need to check the data to see if any of the dice randomly generated exist 3 times. Here is an example.
    I roll: 3, 1, 2, 3, 5, 3.
    I need to check how many times each number exists (numbers 1-6, on dice ofcourse.). But i cant quite think of a good way to do it. in C++ that is lol. Im not too sure my array data storage is the best the way im using it already. So how would i do that? A simple loop running through each number and die ( number[1] dice[1] ) and storing its number then after the loop is done checking how many numbers exist, then looping through the number array and seeing if any of the values in each element is more than 3 would work i think.. (still with me?) but is all this the best way?

    2.) 'Dynamic Variables'
    Is there such an operator to allow umm joining? like "myStringVar = word1 $+ word2" would be "myStringVar = word1word2", or in such a case, dynamic variable naming, like "myVarOne $+ x = SomeValue", where x would be for example a number running in a loop. So if the loop ran 6 times you would have 6 variables. "myVarOne1, myVarOne2, myVarOne3" ect.


    3.) 'My Horrible Code'
    If you got a sec, view my horrible code lol. Keep in mind i am a TOTAL C++ NUB lol. And that this code IS NOT DONE! The bottom is left undone, i relize there are loops that have to break in them yet. But none the less just little things or big things. If your confused by anything, like what the point of the game is, or what for example "x" is representing, dont worry about it. Im not in need of examination yet, but since im posting i figured id post and see whats happends. Thanks!







    *Warning, n00b code below. Shield your Eyes*
    Code:
    #include <iostream>
    #include <iomanip>
    #include <ctime>
    #include <algorithm>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    
    int Dice_Ret(int numOfDie);
    int Random_Range(int lowest_number, int highest_number);
    int Rand_Test();
    int menu();
    void Game_Stage1();
    
    int main()
    {
        cout<<"Welcome to Zonc Alpha\nDisplaying Main Menu..\n";
        int exit = 0;
        while(1){
            switch(menu()){
                case 1:
                    Rand_Test();
                    break;
                case 2:
                    Game_Stage1();
                    break;
                case 4:
                    cout<<"Exiting...\n";
                    exit = 1;
                    break;
                case 5:
                    cout<<"Random Dice Generating: "<<Random_Range(1,6)<<"\n\n";
                    break;
                default:
                    cout<<"Invalid Response, Please choose again.\n";
                    break;
            }
            if(exit == 0)
                continue;
            else
                break;
        }        
        cout<<"Exited.\n";
    }
    int Rand_Test()
    {
        int arreh[7];
        arreh[1] = 0, arreh[2] = 0, arreh[3] = 0, arreh[4] = 0, arreh[5] = 0, arreh[6] = 0;
        int c, r = 60000;
        cout<<"Out of "<<r<<" rolls of one die, these are the numbers generated.\n";
        for(int a = 0; a <= r; a++){
            c = Random_Range(1,6);
            arreh[c] = arreh[c]++;
        }
        for(int a = 1; a <= 6; a++){
            cout<<a<<" Was Generated \""<<arreh[a]<<"\" Times.\n";
        }    
    }     
       
    int Random_Range(int x, int y)
    {
        if(x > y){
            swap(x,y);
        }
        int range = y - x + 1;
        return x + int(range * rand()/(RAND_MAX + 1.0));
    }        
    int Dice_Ret(int numofDie)
    {
    }
    int menu()
    {
        cout<<"\n\n";
        cout<<"]----------------------------[\n";
        cout<<" 1: Displays the dice randomization test\n";
        cout<<" 2: Enter Beta Zonc 1\n";
        cout<<" 3: Display Menu Again\n";
        cout<<" 4: Exit\n";
        cout<<" 5: Roll Die\n";
        cout<<"]----------------------------[\n";
        int a;
        cout<<"What do you choose: ";
        cin>>a;
        return a;
    }    
    void Game_Stage1() {
        int dice[1] = 0, dice[2] = 0, dice[3] = 0, dice[4] = 0, dice[5] = 0, dice[6] = 0;
        unsigned int totalscore = 0, side = 0, totalcurrent = 0, input, zonc = 0;
        cout<<"\n\nWelcome to Zonc Beta 1, This is a single player game.\n"
        "The object of this is to test the coding of the game.\nIf you dont know how to play,\ninstructions should be in the main menu.\n"
        "if not, well your out of luck then because Zeus hasent coded them yet.\n\n";
        cout<<"Please choose a Gameplay Goal (the number you win if you reach, must be higher than 1k): ";
        unsigned int goal;
        cin>>goal;
        while((goal <= 1000) || (goal > 50000)){
            cout<<"\n\nError, Your Goal is too small, Please input a new Goal: ";
            cin>>goal; 
        }    
        cout<<"\n\nCommands;\nQ - Type at anytime to exit.\nRoll - Rolls the dice, assuming you can by the rules.\n"
        "Keep - Keeps a single die, use multiple times for multiple die.\nScore - Displays your total score (including your current roll and on the side).\n";
        int continue = 1, roll = 1; // Roll checks if the user has put away atleast one die, allowing them to roll again. On be default.
        while(continue){
    //abandoned this type of tracking multiples of the same dice number.        int zc-one = 0, zc-two = 0, zc-three = 0, zc-four = 0, zc-five = 0, zc-six = 0; //Zonc Check Variables, each one stands for a dice number, a loop checks for 3 of one number.
            for(int a = 1; a <= 6; a++)
                dice[a] = Random_Range(1,6);
            cout<<"Rolling..\nDisplaying Roll Results..\n"
            "Dice1: "<<dice[1]<<", Dice2: "<<dice[2]<<", Dice3: "<<dice[3]<<", Dice4: "<<dice[4]<<", Dice5: "<<dice[5]<<", Dice6: "<<dice[6]<<"\n";
            for(int a = 1; a <= 6; a++){ //Check if the player Zonced
                zonc = 1;
                if((dice[a] == 1) || (dice[a] == 5))
                    zonc = 0;
                
            }            
            
        }    
    }
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Actually, the way you have it set up to figure out how many times a specific number is rolled is pretty good. Unless you start using more advanced objects like STL containers I don't think theres a better way.

    For your second question, no you cannot dynamically name variables unfortunately.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    1.) 'Data Storage'
    I think you're on the right track here.
    If I understand what you're doing, then after the first six rolls, you have:
    dice[1] = 3
    dice[2] = 1
    dice[3] = 2
    dice[4] = 3
    dice[5] = 5
    dice[6] = 3

    And, counting the "number of numbers"you have:
    number[1] = 1
    number[2] = 1
    number[3] = 3
    number[4] = 0
    number[5] = 1
    number[6] = 0

    If you want to keep track of the data like that in the number[] array, then you don't have to use another loop to go-back and count. You can increment number[n], when you roll a number-n.

    2.) 'Dynamic Variables'
    strcat(pString1, pString2); // Adds pString2 to the end of pString1
    (for C-style strings)

    sprintf(pNewString,"%d was rolled %d times", X, Y); // X & Y replace %d 2 places in pNewstring.
    (for C-style strings)

    The new kewword is used to dynamically create a new varaible (which is accessed via a pointer).
    pMyNewArray = new char[100];
    If you use new, look up delete. They are used in pairs. delete frees-up memory dynamically allocated by new.

  4. #4
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    dynamically create a variable? what exactly do you mean by this?

    I know of the new and delete command, but do you mean they used to assign a location in the heap to a data type (and possibly data at the same time), while assigning the location to the pointer?
    That is how i understand new, what exactly do you mean by dynamic in the use of "pMyNewArray = new char[100];"? Sorry im nub lol, just trying to understand . I understand that assigns the address to pMyNewArray, and the heap location with the data type char and an array of 100 elements used for old strings. Correct?
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Doug, I think he was asking about if it was possible to dynamically name a variable, not dynamically create a variable. Dynamically creating a variable is simply creating and alloting space for a variable at run time instead of compile time, it looks like you got the gist of what it does more or less Zeusbwr.

  6. #6
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    oh ok, i was confused by his usage of dynamic. Thanks for explaining PJ
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick question about dynamic arrays.
    By Lawn Gnomusrex in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2008, 03:22 PM
  2. A question related to dynamic memory allocation
    By spiit231 in forum C Programming
    Replies: 2
    Last Post: 03-11-2008, 12:25 AM
  3. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  4. Another Dynamic Memory Question
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 03-02-2005, 12:10 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM