Populating An Array

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.

Here is the code to populate and print the array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
    char board[9];

    for (int counter = 0; counter < 9; counter++)
    {
        board[counter] = counter + 1;
    }

    for (int counter = 0; counter < 9; counter++)
    {
        cout << board[counter];
    }
}
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.
Thanks, firedraco. It's the little things.
Now I am trying to pass the array to a function. Here is the code:

refresh(board[]);

I get this error:

error: expected primary-expression before '[' token
Last edited on
closed account (D80DSL3A)
Function prototype: void refresh(int arr[], int size);
Function call: refresh(board, 9);
closed account (S6k9GNh0)
The above is true. However, you can also do something like:

void refresh(int *arr, int size);

If this doesn't confuse you, you're doing fine. ":D
OK, I went with fun2code's suggestion. The call is no longer giving me an error, but inside the loop to populate the array, I get this error:

error: invalid conversion from 'const char*' to 'char'

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

void refresh(char board[], int size)
{
    for (int counter = 0; counter < 9; counter++)
    {
        cout << board[counter];
    }
}

int main()
{
    char board[9];
    for (int counter; counter<9; counter++)
    {
            board[counter] = "a";
    }
    refresh(board, 9);
}


computerquip,
Yes, you confused me.
Last edited on
closed account (D80DSL3A)
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.
Last edited on
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.
You can't throw errors, just exceptions :D
Last edited on
You can't throw errors, just exceptions :D


Good catch!
closed account (S6k9GNh0)
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.
Last edited on
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.

Thanks again.
Topic archived. No new replies allowed.