I am trying to populate a small array. When I output the array to the console, instead of numbers, I get a dark smiley face, a smiley face, a heart, a diamond, and a club.
That's because your array contains characters, not numbers. Characters 0-9 are non printable ones usually. If you want to store numbers, you'll need to make an array of ints instead.
Try this for line 17: board[counter] = 'a';// single quotes for a single character
An error on line 15: for (int counter=0; counter<9; counter++)// counter was uninitialized . Actually, this just generates a warning but disaster could result from counter starting at some "random" value.
Suggestion for line 6: for (int counter = 0; counter < size; counter++)
Why pass the size then not use it? This code makes the function work for an array of any size, not just for size = 9.
EDIT: computerquips method will work because an array name is converted to a pointer when an array is passed anyways. You would find that this function will work just as well:
1 2 3 4 5
void refresh(char* board, int size)
{
for (int counter = 0; counter < size; counter++)
cout << board[counter];
}
You are passing a pointer to the 1st element in the array in the above version.
fun2code,
Good call. The change to single quotes fixed the error issue. I made the other changes as well. Even though they weren't throwing errors, I took your point.
It confused you because you don't quite understand how arrays are treated in C (and thus, C++). C arrays are treated very similar to pointers. As a matter of fact, an array itself is generally just a pointer to the beginning of a contiguous array of allocated memory and can be treated as such. If you don't understand how pointers work, then perhaps you should sit down and learn them as they're a very important concept needed to utilize C and C++.
If I'm not mistaken, in ancient C, a pointer would be declared as int myPointer[]. This is no longer valid though, just a fun fact.
computerquip,
Thanks for the ummm... pointer. I'll be re-reading that section of the book today. My problem is that I took the classes a year ago and have done nothing with it since.