0
// Array for one's place
char arrayOnes[10] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}

In the code above I'm having trouble declaring this array. I either get > error: too many initializers for ‘char [10]’

3
Contributors
2
Replies
23
Views
4 Years
Discussion Span
Last Post by mike_2000_17
1

You are trying to initialize a 2d array when you onlty declared a 1d array. You can change it to

char arrayOnes[][10] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}

If you can use strings I would suggest doing this

std::string arrayOnes[10] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
3

The type char arrayOnes[10]; is an array of 10 characters. In your initializer, you are defining an array of strings (which are themselves arrays of characters). This would be the correct version:

const char* arrayOnes[10] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

The above defines an array of pointers to const-characters. A pointer to a const-character is the type of string literals like "one".

Of course, in more modern C++, we don't really use those kinds of C-style arrays anymore. In C++11, you would do this:

std::vector< std::string > arrayOnes = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.