Pointer Question..... C++

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

villeballa89
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sun Aug 31, 2008 8:44 pm

Pointer Question..... C++

Post 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?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Question..... C++

Post by avansc »

i would be glad to help, but im not sure what you mean. could you explain a little better.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
MakazeGamer
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Wed Oct 29, 2008 10:48 pm

Re: Pointer Question..... C++

Post 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;
}
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Question..... C++

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

Re: Pointer Question..... C++

Post by Arce »

avansc, that really is a pretty nifty solution. :lol:
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
villeballa89
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sun Aug 31, 2008 8:44 pm

Re: Pointer Question..... C++

Post 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
villeballa89
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sun Aug 31, 2008 8:44 pm

Re: Pointer Question..... C++

Post 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"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Question..... C++

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
villeballa89
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sun Aug 31, 2008 8:44 pm

Re: Pointer Question..... C++

Post by villeballa89 »

nope still same error
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Question..... C++

Post by avansc »

villeballa89 wrote:nope still same error
pm sent
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Question..... C++

Post by avansc »

just got the mail, will reply shortly
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Question..... C++

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

TTT solution in OO form.

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
villeballa89
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sun Aug 31, 2008 8:44 pm

Re: Pointer Question..... C++

Post 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?:

Code: Select all

string& p[8];
p[0] = xy1;
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointer Question..... C++

Post 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?:

Code: Select all

string& p[8];
p[0] = xy1;
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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply