SydLexia.com Forum Index
"Stay awhile. Stay... FOREVER!"

  [Edit Profile]  [Search]  [Memberlist]  [Usergroups]  [FAQ]  [Register]
[Who's Online]  [Log in to check your private messages]  [Log in]
So who here's a C++ programmer


Reply to topic
Author Message
Hacker
Banned
Joined: Sep 13 2008
PostPosted: Nov 22 2010 08:27 pm Reply with quote Back to top

To put it bluntly I have no clue why I should use private classes and yet in this revision of a game we made earlier in the year we have to use public and private classes.

Public I get, private I don't

So I'd appreciate it if someone would either A.) be willing to help me with this program. Or B.) Explain why I should use them and a bit of how to
View user's profileSend private message
Douche McCallister
Moderator
Title: DOO-SHAY
Joined: Jan 26 2007
Location: Private Areas
PostPosted: Nov 22 2010 10:04 pm Reply with quote Back to top

My C++ is rusty but you want a private class to lower the ability of altering the variable or method against your knowledge. It's mainly just purists. You don't NEED to have them but if it is deemed neccessary by the teacher I'm sure he wants to see you implement both to make sure you develop around them.

You could post your code in here. Not sure how /much/ help I can be but it never hurts to have someone else look at it. Just make sure you encase it in the code/code tag.


Image
 
View user's profileSend private messageSend e-mail
Hacker
Banned
Joined: Sep 13 2008
PostPosted: Nov 22 2010 11:40 pm Reply with quote Back to top

Get ready for lazy coding!!!!
Tic tac Toe BTW
Code:

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class board{
    private:
        int iRw;
        int iCl;

    public:
        const int iRow(){return iRw;}
        const int iCol(){return iCl;}
        void set_values (int, int);
        char hold[100]; // used to stop the program from closing or as a replacement for the system pause command
        char cJunk;
        int iRowI; // row input
        int iColI; // column imput

};

void board::set_values (int a, int b){
    iRw = a;
    iCl = b;
}

int main()
{
board brd;

brd.set_values (3,3);




cout << brd.iRow() << endl;
char board[brd.iCol()][brd.iRow()];

 for(int row = 0; row < brd.iRow(); row++)
    {
        for(int col = 0; col < brd.iCol(); col++)
        {

        board[col][row] = '#'; // What the board tokens will look like

        }
    }

int c = 1;
int d = 2;

while (c < d){ // Here we enter the game loop

cout << "Would you like to play tic-tac-toe?"             << endl;
cout << "if so, press enter, if not then close this out." << endl;
cin.getline(brd.hold, 10);


    int e = 1;
    int f = 2;

        while ( e < f ) {
            e = 1;
            f = 2;


// End of board stuff
// Player 1 is opted to go

    for(int row = 0; row < brd.iRow(); row++)
    {
        cout << "               ";
        for(int col = 0; col < brd.iCol(); col++)
        {
            brd.cJunk= board[col][row];

                cout << " |_ " << brd.cJunk << " _| ";

        }

        cout << endl;
        cout << endl;
    }

int e = 0;
int f = 1;

while ( e < f ) {

 cout << "Player 1, go. Enter a row, hit enter, then enter a column and hit enter" << endl;

 cout << "Column: ";
 cin  >> brd.iColI;

 cout << "Row: ";
 cin  >> brd.iRowI;

board[brd.iColI][brd.iRowI] = 'X';
brd.cJunk = board[brd.iRowI][brd.iColI];

    for(int row = 0; row < brd.iRow(); row++)
    {
        cout << "               ";
        for(int col = 0; col < brd.iCol(); col++)
        {
            brd.cJunk = board[col][row];

                cout << " |_ " << brd.cJunk << " _| ";

        }
        cout << endl;
        cout << endl;
    }



cout << "Player 2 go" << endl;

 cout << "Column: ";
 cin  >> brd.iColI;

  cout << "Row: ";
 cin  >> brd.iRowI;

board[brd.iColI][brd.iRowI] = 'O';
brd.cJunk = board[brd.iColI][brd.iRowI];

    for(int row = 0; row < brd.iRow(); row++)
    {
        cout << "               ";
        for(int col = 0; col < brd.iCol(); col++)
        {
            brd.cJunk = board[col][row];

                cout << " |_ " << brd.cJunk << " _| ";

        }
        cout << endl;
        cout << endl;
    }


cout << endl;
cout << endl;

}


}
// End of game loop
        }

cin.getline(brd.hold, 1);
return 0;
}
View user's profileSend private message
Douche McCallister
Moderator
Title: DOO-SHAY
Joined: Jan 26 2007
Location: Private Areas
PostPosted: Nov 23 2010 12:03 am Reply with quote Back to top

I now understand why my professor always told us to make the variables something relevant or write comments.
----------------------------------
int e = 0;
int f = 1;

while ( e < f ) { ...
Whats the purpose of this? Doesn't this result in a infinite loop since the code never increments "e"?


Image
 
View user's profileSend private messageSend e-mail
GPFontaine
Joined: Dec 06 2007
Location: Connecticut
PostPosted: Nov 23 2010 11:51 am Reply with quote Back to top

I have not coded in C++ in 10 years now. Sorry that I can't be of help with the code. I can however be of help with posting code.

Pastie.org is your friend.

Example of your code at Pastie.org:
http://pastie.org/private/jjysokpxmzii2p6n8w0axq

While the CODE block works fine in this forum, for large amounts of it, it is nice to have syntax highlighting and easily allow for the flow of the conversation. For what it is worth before BZ goes nuts on me, this is a suggestion, not a demand.



 
View user's profileSend private messageVisit poster's website
Hacker
Banned
Joined: Sep 13 2008
PostPosted: Nov 23 2010 03:11 pm Reply with quote Back to top

Douche McCallister wrote:
I now understand why my professor always told us to make the variables something relevant or write comments.
----------------------------------
int e = 0;
int f = 1;

while ( e < f ) { ...
Whats the purpose of this? Doesn't this result in a infinite loop since the code never increments "e"?

Yeah it results in an infinite loop that can be modified later.
if I were to do
e = 2;
later in the code it would exit the loop.
The purpose for that loop is so the board won't reinitialize
The codes close to being finished but I still have quite a bit to work with
View user's profileSend private message
GPFontaine
Joined: Dec 06 2007
Location: Connecticut
PostPosted: Nov 23 2010 03:42 pm Reply with quote Back to top

I was just doing some quick reading about private vs public classes. While I don't code C++, I do code in other languages. To me it sounds like global vs non-global names.

Why would it matter?

Lets say you had 5 guys writing a program together. Each person writes a subroutine that serves a function. What is to say that they all won't call their classes the same name? Or you may just forget you used one name when you go to write another part of the code.

If you keep one set of variables private, then only the subroutine that needs them has access, while the the global ones can pass data between all the routines.

A quick example.
A video game with 10 guns. You may want to create rules for each gun. There is a good chance you will use the same terminology for them, but they have nothing to do with each other. Yet there may be a reason to notice if the gun is modified or upgraded. So you give some global access for those features but privatize the rest.



 
View user's profileSend private messageVisit poster's website
Hacker
Banned
Joined: Sep 13 2008
PostPosted: Nov 23 2010 03:58 pm Reply with quote Back to top

Ah okay, that helps

Here's an updated version of my code
Now it dies at a certain point >_<
--------------------------

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class board{
private:
static const int iMAX = 3;

int iRowI; // row input
int iColI; // column input

public:
int iMAXI(){return iMAX;}
void set_values (int, int);
char hold[100];

char cJunk;
int set_row(int iX)
{
iRowI = iX;
return 0;
}
int set_col(int iY)
{
iColI = iY;
return 0;
}
int get_col()
{
return iColI;
}
int get_row()
{
return iRowI;
}
int input_int()
{
int iTemp;
cin >> iTemp;
return iTemp;
}
};



int main()
{
board brd;

cout << " " << "Tic Tac Toe " << brd.iMAXI() << " in a row." << endl;
cout << endl;
char board[brd.iMAXI()][brd.iMAXI()];

for(int row = 0; row < brd.iMAXI(); row++)
{
for(int col = 0; col < brd.iMAXI(); col++)
{

board[col][row] = '#'; // What the board tokens will look like

}
}

int c = 1;
int d = 2;

while (c < d){ // Here we enter the game loop

cout << "Would you like to play tic-tac-toe?" << endl;
cout << "if so, press enter, if not then close this out." << endl;
cin.getline(brd.hold, 10);


int e = 1;
int f = 2;

while ( e < f ) {
e = 1;
f = 2;


// End of board stuff
// Player 1 is opted to go

for(int row = 0; row < brd.iMAXI(); row++)
{
cout << " ";
for(int col = 0; col < brd.iMAXI(); col++)
{
brd.cJunk = board[col][row];

cout << " |_ " << brd.cJunk << " _| ";

}

cout << endl;
cout << endl;
}

int e = 0;
int f = 1;
// right now we enter a loop so the board can't be reinitialized and thus reset
while ( e < f ) {

cout << "Player 1, go. Enter a row, hit enter, then enter a column and hit enter" << endl;

cout << "Column: ";
brd.set_row(brd.input_int());

cout << "Row: ";
brd.set_row(brd.input_int());

board[brd.get_col()][brd.get_row()] = 'X'; // <---- DIES RIGHT HERE!!!!
brd.cJunk = board[brd.get_col()][brd.get_row()];

for(int row = 0; row < brd.iMAXI(); row++)
{
cout << " ";
for(int col = 0; col < brd.iMAXI(); col++)
{
brd.cJunk = board[col][row];

cout << " |_ " << brd.cJunk << " _| ";

}
cout << endl;
cout << endl;
}



cout << "Player 2 go" << endl;

cout << "Column: ";
brd.set_row(brd.input_int());

cout << "Row: ";
brd.set_row(brd.input_int());

board[brd.get_col()][brd.get_row()] = 'O';
brd.cJunk = board[brd.get_col()][brd.get_row()];

for(int row = 0; row < brd.iMAXI(); row++)
{
cout << " ";
for(int col = 0; col < brd.iMAXI(); col++)
{
brd.cJunk = board[col][row];

cout << " |_ " << brd.cJunk << " _| ";

}
cout << endl;
cout << endl;
}


cout << endl;
cout << endl;

}


}
// End of game loop
}

cin.getline(brd.hold, 1);
return 0;
}
----------------------------
View user's profileSend private message
Douche McCallister
Moderator
Title: DOO-SHAY
Joined: Jan 26 2007
Location: Private Areas
PostPosted: Nov 23 2010 07:17 pm Reply with quote Back to top

int c = 1;
int d = 2;

while (c < d){ // Here we enter the game loop

cout << "Would you like to play tic-tac-toe?" << endl;
cout << "if so, press enter, if not then close this out." << endl;
cin.getline(brd.hold, 10);
-------------------------------------------------------------------------------------------
Why not remove the int c, d, e & f and just have it use a Y/N statement.
You could say something like Char Answer = Y While (Answer <> N)
That way the user can either say "y" to play or "n" to close the program.
Or when the game is over you can say y to play again or n to exit.

Also it would be much easier if I knew what iMaxI, cJunk etc mean or what they are supposed to do.


Image
 
View user's profileSend private messageSend e-mail
Hacker
Banned
Joined: Sep 13 2008
PostPosted: Nov 23 2010 07:37 pm Reply with quote Back to top

That was crap my teacher added.

All the data functions have to be private.
He told me it's unnecessary but that's how he wants it

iMAXI creates the number 3. to replace all the const int's I had that were 3

cJunk is essentially what becomes the board

as for the a b c d ETC.

That's all because right now I just need a base system, I'll fine tune it once I get it to run.

Which is what I need help with right now
View user's profileSend private message
Douche McCallister
Moderator
Title: DOO-SHAY
Joined: Jan 26 2007
Location: Private Areas
PostPosted: Nov 23 2010 07:52 pm Reply with quote Back to top

Where is iX and iY coming from?


Image
 
View user's profileSend private messageSend e-mail
Douche McCallister
Moderator
Title: DOO-SHAY
Joined: Jan 26 2007
Location: Private Areas
PostPosted: Nov 23 2010 08:09 pm Reply with quote Back to top

It might be a lot easier if you use a Char Array for X and O placement.
Example...X [row] [col], make sure you decrease the value entered to achieve the right array address, you also might want to change how you draw the board. I don't think that is going to work properly, It would probably be easier to just draw it in a sub.

Code:

cout << " _________________\n";
cout << "|      |       |       |  \n";
cout << "|  " << x[0][0] << "  |  " << x[0][1] << "  |  " << x[0][2] <<"  |\n";
cout << "|_____|_____|_____|\n";
cout << "|      |       |       |  \n";
cout << "|  " << x[1][0] << "  |  " << x[1][1] << "  |  " << x[1][2] <<"  |\n";
cout << "|_____|_____|_____|\n";
cout << "|      |       |       |  \n";
cout << "|  " << x[2][0] << "  |  " << x[2][1] << "  |  " << x[2][2] <<"  |\n";
cout << "|_____|_____|_____|\n";


Image
 
View user's profileSend private messageSend e-mail
Hacker
Banned
Joined: Sep 13 2008
PostPosted: Nov 23 2010 08:38 pm Reply with quote Back to top

iX and iY are being thrown in with int set_row and int set_col

and I am using a character array for the board.
Where I'm telling it to make the input the character 'X' is where it dies.

And that was after my teacher messed with it
View user's profileSend private message
Douche McCallister
Moderator
Title: DOO-SHAY
Joined: Jan 26 2007
Location: Private Areas
PostPosted: Nov 23 2010 09:40 pm Reply with quote Back to top

Not to sound like a dick but your code is incredibly confusing. When you ask for the Row and Column how is it taking the user entered value and placing it in your set get confusion.

Why not just have a variable and assign it by: cin >> row cin >> col, it looks like your adding subs just for the sake of having subs. If this is how the teacher wants it he seems like an idiot and I don't think I can help.

Also I will be moving this topic to the Other Peoples Projects forum after your next reply.


Image
 
View user's profileSend private messageSend e-mail
Hacker
Banned
Joined: Sep 13 2008
PostPosted: Nov 23 2010 09:58 pm Reply with quote Back to top

I had it as a simple cin >> thing
Then my teacher said I can't do that for your project and that all the data members need too be in the private class so he wrote up the get_whatever crap.

His assignment is to make tic tac toe
With all the data members being in the private class
and all the code in the public
View user's profileSend private message
Blackout
Title: Captain Oblivious
Joined: Sep 01 2007
Location: That Rainy State
PostPosted: Nov 23 2010 10:08 pm Reply with quote Back to top

All this techno wizardry just for tic tac toe? Here,Image, now print it out and go nuts!



 
View user's profileSend private messageAIM AddressYahoo MessengerMSN Messenger
Douche McCallister
Moderator
Title: DOO-SHAY
Joined: Jan 26 2007
Location: Private Areas
PostPosted: Nov 23 2010 10:23 pm Reply with quote Back to top

Ok well he's an idiot.
Back to my original question, how are you getting the input from "cout << Row:" into your procedure under it? I don't believe it's being passed to the procedure.


Image
 
View user's profileSend private messageSend e-mail
Hacker
Banned
Joined: Sep 13 2008
PostPosted: Nov 23 2010 10:45 pm Reply with quote Back to top

He was a software designer by profession. This is his first year teaching

This is the input for the row and column

cout << "Column: ";
brd.set_row(brd.input_int()); <--- input

cout << "Row: ";
brd.set_row(brd.input_int()); <---- input


The input is then stored with the set_row or set_col

int set_row(int iX)
{
iRowI = iX;
return 0;
}

It takes the input, turns it into iX (or iY for column) and then turns it into iRowI (or iColI)

then with this it's called into the int main()
brd.get_col()

board[brd.get_col()][brd.get_row()] = 'X';
That above is the line that causes the program to fail.
I'm trying to assign the inputted numbers as 'X'

now when I had it as this

Code:

#include <iostream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class board{
    private:
        int iRw;
        int iCl;

    public:
        const int iRow(){return iRw;}
        const int iCol(){return iCl;}
        void set_values (int, int);
        char hold[100];
        char cJunk;
        int iRowI; // row input
        int iColI; // column imput

};

void board::set_values (int a, int b){
    iRw = a;
    iCl = b;
}

int main()
{
board brd;

brd.set_values (3,3);




cout << brd.iRow() << endl;
char board[brd.iCol()][brd.iRow()];

 for(int row = 0; row < brd.iRow(); row++)
    {
        for(int col = 0; col < brd.iCol(); col++)
        {

        board[col][row] = '#'; // What the board tokens will look like

        }
    }

int c = 1;
int d = 2;

while (c < d){ // Here we enter the game loop

cout << "Would you like to play tic-tac-toe?"             << endl;
cout << "if so, press enter, if not then close this out." << endl;
cin.getline(brd.hold, 10);


    int e = 1;
    int f = 2;

        while ( e < f ) {
            e = 1;
            f = 2;


// End of board stuff
// Player 1 is opted to go

    for(int row = 0; row < brd.iRow(); row++)
    {
        cout << "               ";
        for(int col = 0; col < brd.iCol(); col++)
        {
            brd.cJunk= board[col][row];

                cout << " |_ " << brd.cJunk << " _| ";

        }

        cout << endl;
        cout << endl;
    }

int e = 0;
int f = 1;

while ( e < f ) {

 cout << "Player 1, go. Enter a row, hit enter, then enter a column and hit enter" << endl;

 cout << "Column: ";
 cin  >> brd.iColI;

 cout << "Row: ";
 cin  >> brd.iRowI;

board[brd.iColI][brd.iRowI] = 'X';
brd.cJunk = board[brd.iRowI][brd.iColI];

    for(int row = 0; row < brd.iRow(); row++)
    {
        cout << "               ";
        for(int col = 0; col < brd.iCol(); col++)
        {
            brd.cJunk = board[col][row];

                cout << " |_ " << brd.cJunk << " _| ";

        }
        cout << endl;
        cout << endl;
    }



cout << "Player 2 go" << endl;

 cout << "Column: ";
 cin  >> brd.iColI;

  cout << "Row: ";
 cin  >> brd.iRowI;

board[brd.iColI][brd.iRowI] = 'O';
brd.cJunk = board[brd.iColI][brd.iRowI];

    for(int row = 0; row < brd.iRow(); row++)
    {
        cout << "               ";
        for(int col = 0; col < brd.iCol(); col++)
        {
            brd.cJunk = board[col][row];

                cout << " |_ " << brd.cJunk << " _| ";

        }
        cout << endl;
        cout << endl;
    }


cout << endl;
cout << endl;

}


}
// End of game loop
        }

cin.getline(brd.hold, 1);
return 0;
}


Everything worked fine.
Even that above was unnecessary, but he told me it was wrong and then changed crap then told me to change more crap and it stopped working.
All in the name of an unnecessary way of coding -_-[/code]
View user's profileSend private message
Hacker
Banned
Joined: Sep 13 2008
PostPosted: Nov 23 2010 10:48 pm Reply with quote Back to top

I'm thinking it may be time to rewrite this thing entirely



 
View user's profileSend private message
Douche McCallister
Moderator
Title: DOO-SHAY
Joined: Jan 26 2007
Location: Private Areas
PostPosted: Nov 23 2010 10:54 pm Reply with quote Back to top

Hacker wrote:
He was a software designer by profession. This is his first year teaching

This is the input for the row and column

cout << "Column: ";
brd.set_row(brd.input_int()); <--- input

cout << "Row: ";
brd.set_row(brd.input_int()); <---- input


The input is then stored with the set_row or set_col

int set_row(int iX)
{
iRowI = iX;
return 0;
}

It takes the input, turns it into iX (or iY for column) and then turns it into iRowI (or iColI)

then with this it's called into the int main()
brd.get_col()

board[brd.get_col()][brd.get_row()] = 'X';
That above is the line that causes the program to fail.
I'm trying to assign the inputted numbers as 'X'

In your version you had cin >> iRowI in this new version you don't have a cin at all. My question again is how are the numbers being passed to the sub. Don't you need a cin or a getline or something to take the entered input? You're just going straight to the procedure.


Image
 
View user's profileSend private messageSend e-mail
Hacker
Banned
Joined: Sep 13 2008
PostPosted: Nov 23 2010 11:26 pm Reply with quote Back to top

Apparently not
brd.set_row(brd.input_int()); <------ this runs

int input_int()
{
int iTemp;
cin >> iTemp; <------- this
return iTemp;
}
which in turn is read by
brd.set_row() <------ this

It's an unnecessary way of saying

cin >> iRowI;

which I can't do because iRowI is private.
Because that's how he wants it >_<



 
View user's profileSend private message
Douche McCallister
Moderator
Title: DOO-SHAY
Joined: Jan 26 2007
Location: Private Areas
PostPosted: Nov 24 2010 12:15 am Reply with quote Back to top

I'll try dicking with the code when I get home tonight. Best case scenario is to make it work the way you know how, then piece by piece alter it out to his specifications.


Image
 
View user's profileSend private messageSend e-mail
Hacker
Banned
Joined: Sep 13 2008
PostPosted: Nov 24 2010 12:29 am Reply with quote Back to top

I should mention he also wants the main code in the public class
And thanks douche
View user's profileSend private message
Hacker
Banned
Joined: Sep 13 2008
PostPosted: Nov 24 2010 04:26 am Reply with quote Back to top

I got it working
View user's profileSend private message
Display posts from previous:      
Reply to topic

 
 Jump to: