im making a simple text game in c just for fun, but is having some problems with structs and/or Visual Studio 2010. I’ve started a empty C++ project, but my main main file is main.c.
Here is the code:
int main()
{
struct Player
{
char name[256];
int sum;
};
struct Player player;
strcpy(player.name, "John");
player.sum = 0;
struct Player cpu;
strcpy(cpu.name, "Bob");
cpu.sum = 0;
printf("\n\n\n");
system("PAUSE");
return 0;
}
Now, the compiler is complaining alot! One of them:
Syntax error: missing ; before type (struct Player cpu line)
Rest is related to that cpu is not a struct and therefor non of the members gets recon by the compiler.
What have i done wrong with my struct?
In C, you have to declare all of your local variables first, at the beginning of the scope. You should move the
struct Player cpu;declaration on the line right afterstruct Player player;