Warning: Noob post.
Messing around with a few concepts learned in this website's tutorial and I cant wrap my mind around why I cannot rename one element of an array to which I am pointing. The code structure is a
do-while
loop in main that sets two
rand
shorts
, then calls two functions. The first fills a pointer pointing to a
char
array, the second prints it out how I want it to.
Here's where I think the problem is, the first function:
1 2 3 4 5 6 7 8 9 10
|
void FillMap () {
for (n=0; n<(x * c); n++) {
a[n] = '.';
}
for (n= (x-1); n!=x; n++) {
a[n] = '@';
}
}
| |
But, the issue created here could be in my
main ()
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
int main () {
do {
srand (time (NULL));
x = rand () % 10 + 4;
c = rand () % 8 + 6;
a = new char [x*c];
FillMap ();
PrintMap ();
std::cout << std::endl;
std::cin >> end;
} while (end == false);
return 0;
}
| |
The specific issue I'm having is that I want
PrintMap ()
to print out the array that's being pointed to, but with an '@' that can be moved around. Right now, I cant even get the '@' symbol to appear in the array, and I don't know why. Right now, I just want it to print out a '@' at the array's rand element.
For those of you who enjoyed NetHack, yes this C++ beginner is testing concepts on this a model of this game.
I would love advice on how to do this an easier way, but for the purpose of mastering basic concepts, I am much more interested in why my primitive solution isn't working, so responses to either end would be appreciated.