Hi folks I think this is my first post so please let extend my greetings and salutations.
and now of course I'd like a little help. :P
I'm trying to get my crappy code to work but I'm having a little trouble.
the program prints a ASCII small multidimensional array in the form of a 7x7 square of '*' characters, my intention is to have a pointer that starts in the centre and marks it's location with a '?' character. the user inputs a direction and the '?' moves around the board. I taught I was close to accomplishing this but the but as it turns out I am unable to get the function to make permanent changes to the pointer location. It al;ways returns to the centre when the program returns to main.
Should I be using a pointer to a pointer???
here's the code anyway. I'd be greatful for your opinions/advice
#include <cstdlib>
#include <iostream>
usingnamespace std;
char print(char board[7][7]);
char move(char *p);
int main(int argc, char *argv[])
{
char board[7][7]; //declare board and it's dimensions
for(int a=0;a<7;a++){ //
for(int b=0;b<7;b++){ //
board[a][b]='*';} //loop to fill board with astrix's
} //
char *p; //Declare pointer p
p = &board[3][3]; //place p at board centre
*p = '?'; //mark centre using pointer p
print(board); //print board
for(int x=0;x<100;x++){ //loop to keep program running (100 moves).
move(p);
print(board); //call print board
}
system("PAUSE");
return EXIT_SUCCESS;
}
char print(char board[7][7]){
system("cls"); //clear screan to avoid clutter
for(int a=0;a<7;a++){ // for loop to print board
for(int b=0;b<7;b++){
cout << board[a][b];
}
cout << "\n"; //newline to keep visual dimensions
}
}
char move(char *p)
{
char direct; //declare wariable to hold user input
cin >> direct; //get use input
if(direct=='w'){*p = '*'; //evaluate input against predefined direction
p = p - 7; //replace marker with board unit
*p = '?';} //place a new marker on destination square
elseif (direct == 's'){*p = '*';
p = p + 7;
*p = '?';}
elseif (direct == 'a'){*p = '*';
p = p - 1;
*p = '?';}
elseif (direct == 'd'){*p = '*';
p = p + 1;
*p = '?';}
else {cout << "please enter the correct controls \n"; // prompt user to enter corect control
system("pause"); //pause to allow messege to be read
return (*p);
}
}
I don't think you can change the value of a pointer inside a function without returning anything, you are thinking of passing a value by reference. What I think you can do is change the pointers location inside the function then change the value outside the function.
for(int x=0;x<100;x++) //loop to keep program running (100 moves).
{
*p = '*' // sets value to '*'
p = move(p);
*p = '?' // sets current position of your pointer to value '?'
print(board); //call print board
}
char move(char p)
{
char direct; //declare variable to hold user input
cin >> direct; //get use input
if(direct=='w') //evaluate input against predefined direction
p = p - 7;
elseif (direct == 's')
p = p + 7;
elseif (direct == 'a')
p = p - 1;
elseif (direct == 'd')
p = p + 1;
elsebreak;
return (p); // this will return a position not a value
}
I didn't get a chance to test this because i am on a computer that doesn't have a C++ compiler.
Someone that knows more then me please come in and help me understand this better as well, I haven't worked with passing pointers as to functions very much.
@slambofett: That would almost work, except your function needs to take and return char*, not just char.
@OP: You can't change the value of the pointer (i.e. p = p - 2;) You can change the value it is pointing to (i.e. *p = 'q';)
Slambofett's way would probably work the best, although you would need to add checks to make sure that the person isn't going out of bounds or anything.
Yes, you need to use a pointer to a pointer for that. So...
1 2 3 4 5 6 7 8 9 10 11
//Incomplete code, but you should be able to update the rest of the code:
char*p = &board[3][3];
move(&p);
char move(char** p){
**P = '*';
*p = *p-7;
**p - '?'
}
I don't think you can change the value of a pointer inside a function without returning anything
That's exactly what a pointer to a pointer (or a reference to a pointer), allows you to do.
#include <cstdlib> //don't know if this is needed (Dev-c++ default entry)
#include <iostream>
#define WIDTH 20 //defenition for dimensions
#define HEIGHT 20 //
usingnamespace std;
char print(char board[WIDTH][HEIGHT]); //print function prototype
int main(int argc, char *argv[])
{
char board[WIDTH][HEIGHT]; //declare board and it's dimensions
for(int a=0;a<HEIGHT;a++){ //
for(int b=0;b<WIDTH;b++){ //
board[a][b]='*';} //loop to fill board with astrix's
} //
char *p; //Declare pointer p
p = &board[WIDTH/2][HEIGHT/2]; //place p at board centre
*p = '?'; //mark centre using pointer p
print(board); //print board
for(int x=0;/*x<100*/;x++){
print(board); //loop to keep program running (infinite).
char direct; //declare wariable to hold user input
cin >> direct; //get use input
if(direct=='w'){*p = '*'; //evaluate input against predefined direction
p = p - WIDTH; //replace marker with board unit
*p = '?';} //place a new marker on destination square
elseif (direct == 's'){*p = '*';
p = p + WIDTH;
*p = '?';}
elseif (direct == 'a'){*p = '*';
p = p - 1;
*p = '?';}
elseif (direct == 'd'){*p = '*';
p = p + 1;
*p = '?';}
else {cout << "please enter the correct controls \n"; // prompt user to enter corect control
system("pause"); //pause to allow messege to be read
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
char print(char board[WIDTH][HEIGHT]){
system("cls"); //clear screan to avoid clutter
for(int a=0;a<HEIGHT;a++){ // for loop to print board
for(int b=0;b<WIDTH;b++){
cout << board[a][b];
}
cout << "\n"; //newline to keep visual dimensions
}
}
@Slambofett: thank you for your suggestion but I don't think I explained the symptoms of my problem in enough detail. the pointer p was marking the square correctly but the pointer itself was returning to the centre at the start of every go.
I was ending up with something like this at the end of 4 trys
******
**?***
*?*?**
**?***
******
you got me thinking though, about where the pointer move was being implemented so I decided to scrap the entire "move" function and I placed it's code in main, inside the infinite loop.
now the changes are persistent and the '?' appears to move around the board like an object.
@firedraco: I understand that I can't change the value of the pointer but I was attempting to change the position of it. I was only changing the value of the pointer's address so I'd have a visual representation of where the pointer is.
@jRaskell: I've seen this in a book somewhere but I have no Idea how to implement it correctly :(