Page 1 of 2
Pointer Question..... C++
Posted: Sat Nov 08, 2008 7:03 pm
by villeballa89
Just wondering if I can use a pointer to take the input of a user and point to a different variable? Like if cin is 1 then the pointer goes to x and if its 2 it goes to y. If it's not pointers that I can do that with can someone tell me how?
Re: Pointer Question..... C++
Posted: Sat Nov 08, 2008 9:08 pm
by avansc
i would be glad to help, but im not sure what you mean. could you explain a little better.
Re: Pointer Question..... C++
Posted: Sat Nov 08, 2008 9:32 pm
by MakazeGamer
I think he is talking about something like this
Code: Select all
int Test1;
int Test2;
int* PointerTest = NULL;
int Input;
cout << "Enter 1 or 2: ";
cin >> Input;
if (Input == 1)
{
PointerTest = &Test1;
}
else if (Input == 2)
{
PointerTest = &Test2;
}
Re: Pointer Question..... C++
Posted: Sat Nov 08, 2008 9:46 pm
by avansc
MakazeGmaer wrote:I think he is talking about something like this
Code: Select all
int Test1;
int Test2;
int* PointerTest = NULL;
int Input;
cout << "Enter 1 or 2: ";
cin >> Input;
if (Input == 1)
{
PointerTest = &Test1;
}
else if (Input == 2)
{
PointerTest = &Test2;
}
that solutions works. but im not sure if thats what he is asking, welli cant say cause i dont know. but here is a other way.
Code: Select all
int x = 1234;
int y = 4321;
int **p = (int**)malloc(2*sizeof(int));
p[0] = &x;
p[1] = &y;
you can make a function that will do it dynamically, i can help if you need help.
so this is quite nice. since p is a pointer to a pointer.
so p[0] points to the memory address of x.
so just remember also that if you said something like p[0] + int, that is wrong, cause you have to dereverance the pointer first.
*p[0] + int will work fine.
you can also see if you printed p[0] you would get x's address in memory,
but if you printed *p[0] you would het x's value.
pritty nifty hey.
Re: Pointer Question..... C++
Posted: Sat Nov 08, 2008 10:42 pm
by Arce
avansc, that really
is a pretty nifty solution.
Re: Pointer Question..... C++
Posted: Sat Nov 08, 2008 11:16 pm
by villeballa89
okay I think you answered my question. To clarify, I was basically asking if the user could select which variable to store their answer in, depending on their input. Say,(in the case of tictactoe) if they put 1 then square1 would be the variable that is changed or if they say 2 then square2 and so on.
That explained better?
in any case, I'm pretty sure I got the answer I was looking for
Re: Pointer Question..... C++
Posted: Sun Nov 09, 2008 6:19 pm
by villeballa89
string xy1 = " ";//top left answer
string xy2 = " ";//top middle answer
string xy3 = " ";......
string **p = (string**)malloc(2*sizeof(string));// get a pointer to a pointer
*p[0] = xy1;
*p[1] = xy2;
*p[2] = xy3;
*p[3] = xy4;
*p[4] = xy5;
*p[5] = xy6;
*p[6] = xy7;
*p[7] = xy8;
*p[8] = xy9;
this is where it says the problem is when I compile.
says "expected constructor, destructor, or type conversion before '=' token"
Re: Pointer Question..... C++
Posted: Sun Nov 09, 2008 6:58 pm
by avansc
villeballa89 wrote:string xy1 = " ";//top left answer
string xy2 = " ";//top middle answer
string xy3 = " ";......
string **p = (string**)malloc(2*sizeof(string));// get a pointer to a pointer
*p[0] = xy1;
*p[1] = xy2;
*p[2] = xy3;
*p[3] = xy4;
*p[4] = xy5;
*p[5] = xy6;
*p[6] = xy7;
*p[7] = xy8;
*p[8] = xy9;
this is where it says the problem is when I compile.
says "expected constructor, destructor, or type conversion before '=' token"
first off ,
string **p = (string**)malloc(2*sizeof(string));// get a pointer to a pointer
is not right.
string **p = (string**)malloc(9*sizeof(string));// get a pointer to a pointer //
is right.
try that ans tell me fi it compiles.
Re: Pointer Question..... C++
Posted: Sun Nov 09, 2008 7:09 pm
by villeballa89
nope still same error
Re: Pointer Question..... C++
Posted: Sun Nov 09, 2008 7:17 pm
by avansc
villeballa89 wrote:nope still same error
pm sent
Re: Pointer Question..... C++
Posted: Sun Nov 09, 2008 7:40 pm
by avansc
just got the mail, will reply shortly
Re: Pointer Question..... C++
Posted: Sun Nov 09, 2008 7:44 pm
by avansc
okay first problem, you cant do all this
Code: Select all
string **p = (string**)malloc(9*sizeof(string));// get a pointer to a pointer //
p[0] = &xy1;
p[1] = &xy2;
p[2] = &xy3;
p[3] = &xy4;
p[4] = &xy5;
p[5] = &xy6;
p[6] = &xy7;
p[7] = &xy8;
p[8] = &xy9;
out side of main or a function!
if you move
this
Code: Select all
p[0] = &xy1;
p[1] = &xy2;
p[2] = &xy3;
p[3] = &xy4;
p[4] = &xy5;
p[5] = &xy6;
p[6] = &xy7;
p[7] = &xy8;
p[8] = &xy9;
into main, it works.
TTT solution in OO form.
Posted: Sun Nov 09, 2008 8:21 pm
by avansc
the code is incomplete, but shows a decent design.
NOTE: this code is not portable!
Code: Select all
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
class GameBoard
{
public:
GameBoard();
void draw();
char grid[3][3];
private:
};
GameBoard::GameBoard()
{
for(int y = 0;y < 3;y++)
{
for(int x = 0;x < 3;x++)
{
this->grid[y][x] = ' ';
}
}
}
void GameBoard::draw()
{
system("cls");
cout << "-------" << endl;
cout << "| | | |" << endl;
cout << "-------" << endl;
cout << "| | | |" << endl;
cout << "-------" << endl;
cout << "| | | |" << endl;
cout << "-------" << endl;
for(int y = 0;y < 3;y++)
{
for(int x = 0;x < 3;x++)
{
gotoxy(x*2+1,y*2+1);
cout << this->grid[y][x] << endl;
}
}
}
class GameRules
{
public:
GameRules();
bool xMove(GameBoard *gb, int x, int y);
bool oMove(GameBoard *gb, int x, int y);
private:
};
GameRules::GameRules()
{
}
bool GameRules::xMove(GameBoard *gb, int x, int y)
{
if(x > 3 || y > 3)
return false;
if(gb->grid[y-1][x-1] != ' ')
return false;
gb->grid[y-1][x-1] = 'X';
return true;
}
int main()
{
GameBoard *gb = new GameBoard();
GameRules *gr = new GameRules();
//cout << "Start of program." << endl;
gb->draw();
gr->xMove(gb, 1, 1);
gr->xMove(gb, 2, 1);
gb->draw();
cout << endl;
system("pause");
return 0;
}
for the move function i would just make it one funtion move
and add a char parameter so you would specify X or O. cuts down on code.
Re: Pointer Question..... C++
Posted: Sun Nov 09, 2008 8:58 pm
by villeballa89
right, I understand about the one function part but, other than that, I'm lost. Did you mean your code was decent design or mine was?
anyway, does C++ allow an array of referances?:
Re: Pointer Question..... C++
Posted: Sun Nov 09, 2008 9:01 pm
by avansc
villeballa89 wrote:right, I understand about the one function part but, other than that, I'm lost. Did you mean your code was decent design or mine was?
anyway, does C++ allow an array of referances?:
no you cant do that.
if you take my code and compile it it will take you a long wat to completing what you need to do.