-1

I am trying to program a game. In this game the user is supposed to enter the number of players and the amount of fields this game has. I tried to use an array to keep track of the score of all players. Therefore I did int score[abplayers], so there are different scores for every player. The problem is that when I try to define all scores to 0. I tried with 0.0, but I get

error: variable-sized object may not be initialized.

What did I do wrong?

Anthon
  • 69,918
  • 32
  • 186
  • 246

2 Answers2

0

abplayers is a variable, so score is a variable sized array. The compiler won't know how much memory it takes, so refuses to initialize it that way.

You can make it a fixed size array, or you can keep it variable, but define a function to assign values, certain to check that no assignment goes past the bounds, abplayer.

donjuedo
  • 2,475
  • 18
  • 28
0

First, your score type is int so you cannot assign '0.0', you have to asign '0'.

To initialize an array you have to create the array. As the compiler does not know the value of abplayers it does not know the size of the array score and cannot create the array.

The solution is to initialize all the scores to '0' once the users enters the number of players.

Gerard Abello
  • 658
  • 3
  • 7