0

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;
}
  • 2
    Well for one, `100` isn't a valid initializer for a 2D array of integers. – chris Oct 04 '12 at 20:03
  • 1
    also the text you give for the job array isn't valid for the second 2d array. I recommend reading some of the first chapters of this C tutorial: http://www.iu.hio.no/~mark/CTutorial/CTutorial.html – Evert Oct 04 '12 at 20:04
  • How about a little information on what you were trying to do with each of these arrays? it's really not clear from the code... – Mike Oct 04 '12 at 20:10
  • 1
    Well, you could start by paying attention to the line number that the compiler helpfully includes in the error message. You could also spend some time learning the C language, because the stuff above only vaguely resembles it. – Jim Balter Oct 04 '12 at 20:12
  • 1
    @JimBalter There has been quite a few of these “I am running my code on compilr.com” questions recently and it appears that the error messages are not always all they could be, e.g. http://stackoverflow.com/questions/10234738/ . Of course the OP could start by using a real compiler if he expects to learn a difficult language such as C. – Pascal Cuoq Oct 04 '12 at 20:17
  • @PascalCuoq "the error messages are not always all they could be" -- I went to the trouble of creating a compilr.com acct to test this proposition and it's nonsense. Every compile-time error message includes a line and column number. Your link is a runtime error that is probably covered in the documentation somewhere. The problem isn't with compilr.com, it's with lazy sloppy people. – Jim Balter Oct 04 '12 at 21:21
  • 1
    @JimBalter Sorry, I should have looked harder for the question that I thought showed compilr.com-sloppiness. I meant http://stackoverflow.com/questions/12661775/what-does-failed-to-zip-binaries-mean (a program that should be statically rejected with a clear error message). I did not mean to imply that compilr.com messages are always bad, just that it's a new, interesting possibility to keep in mind when answering beginner questions. – Pascal Cuoq Oct 05 '12 at 05:36
  • @PascalCuoq Do you honestly think that compilr.com didn't give error messages for that code? C'mon! That question is from the same submitter here. I suggest that you submit that code to compilr.com yourself before ridiculously blaming it for the OP's failures. – Jim Balter Oct 05 '12 at 15:06
  • @PascalCuoq Here's the message that he didn't report: "main is normally a static function". In other words, the compiler treated main as a nested function. What compiler supports that? **gcc**. So here you are, blaming compilr.com for using gcc ... and this is supposed to be some new theory about beginners' questions? (Did you also blame ideone.com, which was noted at that question to produce the same result?) It is, instead, simply the fact that the OP's random approach toward C syntax landed him in gcc extension land. Next time, do a little research and stick to plausible consequences. – Jim Balter Oct 05 '12 at 15:20

2 Answers2

2
 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]
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • And then there's `if (player_cash[1] > 5); do job[0] else if(player_cash[1]<5);` ... this is what happens when people try to write a program without first spending any time learning the language. – Jim Balter Oct 04 '12 at 20:18
  • @JimBalter Writing semicolons after **if** is a C programmer tradition though, only way to stop writing that bug over and over, is to write a whole lot of C code :) Veteran C programmers develop this weird, sub-conscious semi-colon reflex and spot that bug in 1 second :) – Lundin Oct 04 '12 at 20:22
  • But the thing is, had there been a semi colon and then braces, then the compiler would have told you "else statement without a matching if". Adopting the brace style and using consistently everywhere, will mean that you write far less bugs like this. – Lundin Oct 04 '12 at 20:25
  • 'semicolons after if is a C programmer tradition' -- not after the *conditional*. And the `else if` has no block. And what is `do ...`? My point was that this code is jibberish, not to debate whether to use {} or semicolons ... you added that text to your answer after my comment. But on that score, if you use an auto-indenting editor you will never have bugs like this ... I never have, in 35 years of writing C. – Jim Balter Oct 04 '12 at 20:33
  • @JimBalter I'm assuming that the code should partially be regarded as pseudo code, since it is indeed gibberish. – Lundin Oct 04 '12 at 21:03
  • That's a clearly invalid inference. The OP wrote "I am running my code on compilr.com and am using C as my language ... here is my code:" – Jim Balter Oct 04 '12 at 21:14
1

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.

pmr
  • 58,701
  • 10
  • 113
  • 156
  • Hey, take it easy on me. I'm just starting out and I AM reading a book –  Oct 04 '12 at 20:25
  • @BrandonDamante Sorry, if I was sounding harsh. You code certainly doesn't look like you have learned the basic constructs yet. – pmr Oct 04 '12 at 20:27
  • That wasn't harsh. The OP's post was utter nonsense; I've never seen anything so bad at SO before. – Jim Balter Oct 04 '12 at 20:35