I'm busy writing Yahtzee. Now, I want to fill a const char[] with the field names: 1's, 2's, 3's, ..., "Full House" "Small Straight" etc. But I get this errors

#include <iostream>
#include <stdlib.h>

using namespace std;

/*
    1
    2
    3
    4
    5
    6
    3 of a kind
    carre
    full house
    small street
    big street
    topscore
    chance
*/

void RollDice(int DiceToRoll[]);
void ShowScores(int nPlayer);

int Dice[5];
int ScoreField[14][10]; // For max 10 player

const char FieldName[15][14] = {
    "Number 1's",
    "Number 2's",
    "Number 3's",
    "Number 4's",
    "Number 5's",
    "Number 6's",
    "Bonus",
    "Three-of-a-kind",
    "Four-of-a-kind",
    "Full House",
    "Small straight",
    "Large straight",
    "Yahtzee",
    "Chance"
};

int main()
{
    int nNoPlayers;

    cout << "Enter number of players" << endl;
    cin >> nNoPlayers;

    bool bGameOver = false;
    while( !bGameOver ) {

        for( int n = 0; n < nNoPlayers; n++ ) {

            cout << "Player " << n << "'s turn" << endl;
            cout << "Pick an option: " << endl
                << "1) Show upper scorefields" << endl
                << "2) Show lower scorefields" << endl
                << "3) (Re)roll dice" << endl;
            char cOption;
            cin >> cOption;

            if( cOption == '1' ) {
                ShowScores(n);
            }

        }

        cout << "Play another game (Y/N)?" << endl;
        char cAnotherGameYN;
        cin >> cAnotherGameYN;

        if( cAnotherGameYN == 'N' || cAnotherGameYN == 'n' ) {
            bGameOver = true;
        }
    }
    cout << "Game over";
}

// const char FieldName[15][14] = {
void ShowScores(int nPlayer) {

    for( int n = 0; n < 13; n++ ) {
        cout << FieldName[0][n] << endl;
    }
    cout << endl;

}

:

||=== Build: Debug in Yahtzee (compiler: GNU GCC Compiler) ===|
E:\Programming\Yahtzee\main.cpp|43|error: initializer-string for array of chars is too long [-fpermissive]|
E:\Programming\Yahtzee\main.cpp|43|error: initializer-string for array of chars is too long [-fpermissive]|
E:\Programming\Yahtzee\main.cpp|43|error: initializer-string for array of chars is too long [-fpermissive]|
E:\Programming\Yahtzee\main.cpp|43|error: initializer-string for array of chars is too long [-fpermissive]|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

What is wrong?

Recommended Answers

All 5 Replies

Your FieldNames each have a terminating null, so as the error indicates, one of the names is too long.

I entered this line: #include and made FieldNam a string. Now it works. Thanks

There's some knowledge about C++ left in my mind. That's how your reply helped me out.

Something else to consider. You've declared FieldName as 15 strings with the longest at 14 characters. In reality you have 14 strings with the longest at 15 characters. Simply reversing the limits of the array would have fixed the problem. Or, better yet use a simple string array string FieldName[14]

Or better yet, make it an array of char*, since a double quoted literal value is a char pointer to a constant on the heap.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.