what is wrong with this code

This is a discussion on what is wrong with this code within the C++ Programming forums, part of the General Programming Boards category; I have coded a craps game but there are problems in the program and am not sure what they are. ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    what is wrong with this code

    I have coded a craps game but there are problems in the program and am not sure what they are. I am new to c++ so keep that in mind. The initialize array function should be pointing to the first element of the my 2 dimensional array.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <ctime>
    
    using namespace std;
    
    static const int maxRoll = 50;
    static const int maxGame = 3;
    
    class Craps { //Begin CRAPS CLASS
    private:
    public:
    int rolls[maxGame][maxRoll];//2dim
    int *pFirst; //pointer
    Craps(); //constrctor
    void DoubleArray();
    static void InitializeApp();//Bonus Assignment
    };//END CRAPS CLASS
    
    Craps::Craps()//scope res operator //for loop
    {
    int i, j;
    
    for(i = 0 ; i < maxGame ; ++i)
    {
    for(j = 0 ; j < maxRoll ; ++j)
    {
    rolls[i][j] = 0;
    }
    }
    pFirst = &rolls[0][0];
    }
    
    void Craps::DoubleArray()
    {
    int i, j;
    
    for(i = 0 ; i < maxGame ; ++i)
    {
    for(j = 0 ; j < maxRoll ; ++j)
    {
    if (i ==0)
    rolls[i][j] =j;
    else if (i == 1)
    rolls[i][j] = j+50;
    else if (i == 2)
    rolls[i][j] = j+100;
    }
    }
    }
    int RollDie()
    {
    return (1 + rand() %6);
    }
    
    int RollDice()
    {
    return RollDie() + RollDie();
    }
    
    bool PlayGame(int allRolls[][50], int rollCounts[], int gameNum)
    {
    int point = RollDice();
    int roll;
    int rollCount = 0;
    
    rollCounts[gameNum] = 0;
    if (7 == point)
    {
    return true;
    }
    
    do
    {
    roll = RollDice();
    
    allRolls[gameNum][rollCount] = roll;
    ++rollCount;
    ++rollCounts[gameNum];
    
    if (7 == roll)
    {
    return false;
    }
    }
    while (roll != point);
    return true;
    }
    
    void InitializeArray(Craps *pc, int initVal)//function that takes an intger pointer to the first element of two
    //dimensional array holding all the rolls.
    {
    int i,j;
    
    for(i = 0 ; i < maxGame ; ++i)
    {
    for(j = 0 ; j < maxRoll ; ++j)
    {
    pc->rolls[i][j] = initVal;
    }
    }
    
    *pc->pFirst = initVal;
    
    pc->DoubleArray();
    }
    
    
    void Craps::InitializeApp() //definition of static member function InitializeApp
    //This function takes no parameters and does nothing but seed my random number generator.
    {
    srand(time(0));
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    int allRolls[3][50];
    int rollCounts[3];
    int i;
    int rolls;
    
    
    //Calling the static member function of the Craps class
    Craps::InitializeApp();
    
    for (i = 0 ; i < 3 ; ++i)
    {
    PlayGame(allRolls, rollCounts, i);
    
    cout << "Game number " << i+1 << " had " << rollCounts[i] << " rolls:\n\t";
    for (rolls = 0 ; rolls < rollCounts[i] ; ++ rolls)
    {
    cout << allRolls[i][rolls] << " ";
    }
    cout << endl;
    }
    
    Craps c1;
    Craps c2;
    Craps c3;
    int j;
    
    InitializeArray(&c1, -1); //uses address operator(&) to pass the address of c1 to the InitializeArray function
    InitializeArray(&c2, 2); //uses address operator(&) to pass the address of c2 to the InitializeArray function
    InitializeArray(&c3, 0); //uses address operator(&) to pass the address of c3to the InitializeArray function
    
    for(i = 0 ; i < maxGame ; ++i)
    {
    for(j = 0 ; j < maxRoll ; ++j)
    {
    cout << setw(3) << c1.rolls[i][j] << " ";//set the width for display
    }
    cout << endl;
    }
    
    cout << "\n\n";
    
    for(i = 0 ; i < maxGame ; ++i)
    {
    for(j = 0 ; j < maxRoll ; ++j)
    {
    cout << setw(3) << c2.rolls[i][j] << " "; //set the width for display
    }
    cout << endl;
    }
    
    cout << "\n\n";
    
    for(i = 0 ; i < maxGame ; ++i)
    {
    for(j = 0 ; j < maxRoll ; ++j)
    {
    cout << setw(3) << c3.rolls[i][j] << " "; //set the width for display
    }
    cout << endl;
    }
    cout << endl << *c1.pFirst << endl;//points to the first element of the two dimensional array
    cout << endl << *c2.pFirst << endl;//points to the first element of the two dimensional array
    cout << endl << *c3.pFirst << endl;//points to the first element of the two dimensional array
    
    system("PAUSE");
    return 0;
    }
    Last edited by Salem; 01-05-2011 at 04:52 AM. Reason: Restored!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So what is going on that you don't want to go on? You appear to have a cut-down version of the rules for craps, whether by design or necessity I don't know. Your counting is off in the PlayGame function, since the first roll is not added to the list or counted (so if you win with an initial roll of 7, the game will report that you took 0 rolls). You've got big lists of numbers that you're not doing anything with, so nothing happens to them.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    What is this for? Why craps? I want to know before I assist you. Is this a programming test for a job by chance? Let's just say it looks too familiar to hand out help just yet.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by gammanu882 View Post
    there are problems in the program and am not sure what they are.
    Then how do you know that there are problems?

    You need to ask for help with something specific. What is your question?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    Thanks for the responses...no this isn't for a job. I am stuck on my Inititalize Array function and just want looking for some guidance that's all.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, what about it? You go through and make all the numbers the same, and then you call DoubleArray() which makes all the numbers different. I suppose you could just pick one, rather than trying to do both, somehow.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Don't remove your code when you solve the problem -_-
    Other people will benefit from your problem and the solution.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    Elysia is right, don't edit your posts down to nothing once you have the answer.
    A forum is a place for shared learning - which would be a lot harder if everyone kept deleting their posts.

    Oh, and I put the original back!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my code?
    By x2x3i5x in forum C Programming
    Replies: 6
    Last Post: 09-28-2009, 11:52 AM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  3. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 05:32 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21