user1707643
user1707643

Reputation:

What is an invalid initializer? Where is it in my code, and how do I correct it?

I am running my code on compilr.com and am using C as my language. I am trying to make a simple game where you have an inventory and start out with $100. You gain money by doing jobs secretly for other players. But, I'm getting an error message that says, "Invalid initializer." What is this and how do I fix it? here is my code:

#include<stdio.h>

int main()
{
    int player_cash[][3] = 100;
    int player[3];
    int job[][100] = {
        "Text me the address of player1",
        "I'll donate $100 to the your funds, if you steal the gold from player2 for me"
    };
            if (player_cash[1] > 5);
                do job[0]
        else if(player_cash[1]<5);

        return 0;
}

Upvotes: 0

Views: 11361

Answers (2)

Lundin
Lundin

Reputation: 213721

 int player_cash[][3] = 100;

You declare a 2D array of ints, then try to initialize it with a single int. Correct syntax is

 int player_cash[][3] = 
 { 
   {100}
 };

although int player_cash[][3] = { 100 }; will work fine too, just less stylistically correct.

int job[][100] = { "Text me 

ints are integers, they are not strings. So that code doesn't even make sense.

Also, make a habit of always using {} after an if-statement.

if (player_cash[1] > 5);
 do job[0]

will, because of the semi-colon, be treated as if you had written

if (player_cash[1] > 5)
{}
do job[0]

Upvotes: 2

pmr
pmr

Reputation: 59811

For starters, player_cash is a declaration of a 2 dimensional array. You try to initialize it from an integer literal. This wont work. Did you mean to simply declare an int? If you only want to store one quantity you don't need an array or even two dimensional array.

The same goes for you declaration int job[][100] but here you try to initialize it with string literals.

You really should read a book before you try to write C code. Just banging out stuff that looks like C code to you isn't going to get you anywhere.

Upvotes: 1

Related Questions