-1

I've been trying to print of the members of a struct I have created, however there are a few declarations errors that are showing saying my structs are undeclared. I have a separate function for printing the members of the struct. I have no idea on how to debug it... please help I have errors such as game1- undeclared (first use in this function) and expected = , ; asm or attribute before { token

#include <stdio.h>
#include <stdlib.h>

struct video_game
{
  char *name, *genre, *developer, *platformer, *app_purchase;
  int release_year, age_limit;
  float price;
};

void print_video_game_details(struct video_game* s)
{
  printf("\nTitle: %s\n", s->name);
  printf("Genre: %s\n", s->genre);
  printf("Developer: %s\n", s->developer);
  printf("Year of Release: %d\n", s->release_year);
  printf("Lower Age Limit: %d\n", s->age_limit);
  printf("Price: $%f\n", s->price);
  printf("In-app Purchase: %s\n", s->app_purchase);
}

int main(int agrc, char* agrv[])
{
  struct video_game game1
  {
    game1.name = "Candy Crush Saga";
    game1.genre = "Match-Three Puzzle";
    game1.developer = "King";
    game1.release_year = 2012;
    game1.platform = "Android, iOS, Windows Phone";
    game1.age_limit = 7;
    game1.price = 0.00;
    game1.app_purchase = "Yes";
  };
  
  struct video_game game2
  {
    game2.name = "Halo 4";
    game2.genre = "First Person Shooter";
    game2.developer = "343 Industries";
    game2.release_year = 2014;
    game2.platform = "Xbox 360, Xbox One";
    game2.age_limit = 16;
    game2.price = 69.95;
    game2.app_purchase = "No";
  };
  
  struct video_game game1
  {
    game3.name = "Uncharted 2: Among Thieves";
    game3.genre = "Action adventure RPG";
    game3.developer = "Naughty Dog";
    game3.release_year = 2012;
    game3.platform = "PS3";
    game3.age_limit = 16;
    game3.price = 30.00;
    game3.app_purchase = "No";
  };
  
  print_video_game_details(&game1);
  print_video_game_details(&game2);
  print_video_game_details(&game3);

  return 0;
}  
  
George Cavalevu
  • 119
  • 1
  • 2
  • 6
  • 7
    *Please* don't post images of code! Post the code! How are we supposed to quote from an image?! – unwind Jun 01 '15 at 09:13
  • Copy-paste coding often leads to errors, as one easily forgets to change some minor detail. Not that it was correct to begin with. – Some programmer dude Jun 01 '15 at 09:15
  • 1
    That's even funnier than the .png screenshot. What's next, a poll so we can enter our mail address and receive it engraved in granite ? – Quentin Jun 01 '15 at 09:15
  • @JoachimPileborg So you prefer images? – unwind Jun 01 '15 at 09:16
  • Also, like others said, always include the code in the question, code like in *text*. And if posting question about errors, always include the actual (complete and unedited) error log. – Some programmer dude Jun 01 '15 at 09:17
  • 1
    @unwind I mean `game1.` in all three structures. Not correct (as you note in your answer) but copy-pasting code like that is always hazardous. – Some programmer dude Jun 01 '15 at 09:17
  • @JoachimPileborg Aaah. Right. Good point, of course. – unwind Jun 01 '15 at 09:19
  • Sorry I'm using a raspberry pi and a separate monitor, and this code was just a little to much to type up from scratch... – George Cavalevu Jun 01 '15 at 09:21
  • 1
    In `print_video_game_details`, `video_game` is the name of the struct. The variable is called `s`, so you should print `s->name` instead of `video_game->name`. – M Oehm Jun 01 '15 at 09:26
  • 1
    The declaration `char* name, genre;` is probably not what you want: `genre` will be a single char. Use `char *name, *genre;` with an asterisk for each pointer. – M Oehm Jun 01 '15 at 09:28
  • Vote to close? 'Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**.' Emphasis isn't mine! – autistic Jun 01 '15 at 09:31

1 Answers1

2

Your instance creations (game1, game2 and game3) are not C, they are using some made-up syntax.

They should be something like

struct video_game game1 = {
  .name = "Candy Crush Saga",
  /* ... */
};

You need to define three variables of type struct video_game, and <type> <name> [= <initializer>] is (roughly) how variables are defined in C.

If you don't have C99, it must be:

struct video_game game1 = {
  "Candy Crush Saga",
  "Match-Three Puzzle",
  "King",
  "Android, iOS, Windows Phone",
  "Yes",
  2012,
  7,
  0.00
};

Things to notice, that you seem to be ignoring:

  • No names of fields inside the initializer, just values.
  • The order must be exactly the same as when the struct was declared; first five strings, then two integers, then a float.
  • Values are separated with commas, not semicolons.
unwind
  • 391,730
  • 64
  • 469
  • 606
  • sorry so what you're saying is that the way I declared my structs for game1 game2 and game3 is wrong? – George Cavalevu Jun 01 '15 at 09:33
  • @GeorgeCavalevu You are not declaring new structs you are declaring **variables** so you should declare them properly. game1-3 are variables *of type* `struct video_game` – Eregrith Jun 01 '15 at 09:36
  • 1
    @CoolGuy The `.name =` part is C99, yes. In C89, you just need to put your initializers in the right order. – unwind Jun 01 '15 at 09:45
  • @Eregrith I have edited the question but there is still the errors which i have mentioned above... – George Cavalevu Jun 01 '15 at 14:24
  • @GeorgeCavalevu Can't you see how your code is different from unwind's answer? – Eregrith Jun 01 '15 at 14:35
  • @Eregrith well I have changed it to his, but there's always an expected token error... however I have gone through the syntax and this should have worked. Is there anything i could do to make the code compile and run? – George Cavalevu Jun 01 '15 at 14:49
  • 1
    @GeorgeCavalevu If you are using the code in your answer, it's not the same. – Eregrith Jun 01 '15 at 14:50