0

Declare a structure which describes an individual video game. Video games have a title, genre, platform, developer, year of release, lower age limit, a price and whether or not they support in-app purchases. You will need to pick appropriate data types of each piece of information to be stored in the structure.

  • Name the structure: Video_Game.
  • Declare three video game structure variables locally in the main function, called game1, game2 and game3.
  • Assign to the members of game1, the details for the Candy Crush Saga (King, 2012) example above.
  • Assign to the members of game2, the details for the Halo 4 (343 Industries, 2014) example above.
  • Assign to the members of game3, the details for your favourite game… if you don’t play games, check your smart phone… surely you play something on there… if not, check the relevant app store top charts and find a game that could become your new favourite!
  • Next, declare a function called print_video_game_details() which takes in a parameter that is a pointer to video game structure declared earlier. In this function, print out the details of the game passed to the function, in the style shown above for the Candy Crush Saga and Halo 4 examples.
  • Next, call the print_video_game_details function three times from main, passing in the address of game1, game2 and game3, after the members have all been set.

My code so far:

#include <stdio.h>
#include <string.h>

struct video_game
{
   char* title;
   char* genre;
   char* developer;
   int year;
   char* platform;
   int lower_age;
   float price;
   char* inapp_purchase;
}game1, game2;

void print_video_game_details()
{
    for(int i =1; i<=3; i++)
    {
        printf("Title: %s", game[i].title); // game[i] is showing an error "undeclared"
        printf("Genre: %s", game[i].genre);
        printf("Developer: %s", game[i].developer);
        printf("year of release: %d", game[i].year);
        printf("platform: %s", game[i].platform);
        printf("lower age: %d", game[i].lower age);
        printf("price: %f", game[i].price); //is showing an error "incompatible"
        printf("inapp_purchase: %s", game[i].inapp_purchase);
    }
}

int main(void)
{
    game1.title = "Candy crush saga";
    game1.genre = "Match-Three Puzzle";
    game1.developer = "King";
    game1.year = "2012";
    game1.platform = "Android, ios, Windows Phone";
    game1.lower_age = "7";
    game1.price = "$0.00";
    game1.inapp_purchase = "yes";
    print_video_game_details();
}

I am not able to print out the structure as it won't compile.

ERRORS:

prog.c: In function 'print_video_game_details':
prog.c:27:33: error: 'struct video_game' has no member named 'lower'
 printf("lower age: %d", game[i].lower age);
 ^
prog.c:27:40: error: expected ')' before 'age'
 printf("lower age: %d", game[i].lower age);
 ^
prog.c: In function 'main':
prog.c:39:15: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
 game[0].year = "2012";
 ^
prog.c:41:20: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
 game[0].lower_age = "7";
 ^
prog.c:42:16: error: incompatible types when assigning to type 'float' from type 'char *'
 game[0].price = "$0.00";
 ^
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
memg
  • 3
  • 4
  • 2
    You're missing a closing bracket ( } ) at the end of main. And if it won't compile, you should post the error message the compiler is giving you. – fefe Oct 21 '15 at 01:47
  • Make sure you are actually calling your `print_video_game_details` method at the end of main, before the closing bracket. – AlexPogue Oct 21 '15 at 01:57
  • @fefe Thank you. i have edited this snippet. could you please have a look at it now? – memg Oct 21 '15 at 01:58
  • One more helpful hint, `game[i]` refers to index `i` in array named `game`, but you don't have any arrays in your program. For now, make `print_video_game_details()` print information about just game1. – AlexPogue Oct 21 '15 at 02:01
  • @AlexPogue dear sir, the thing is that i have to use this code to print 3 sets of game details, thus i have put game i instead of game 1... – memg Oct 21 '15 at 02:08
  • Sure, we can do that. First we'll have to make an array. You can do that outside any of your functions. Like `struct video_game game[3]` for an array of 3 video_games. – AlexPogue Oct 21 '15 at 02:14
  • Then note that arrays are zero-indexed, so you'll want to start your for loop at `i = 0`, and go while `i < 3`. – AlexPogue Oct 21 '15 at 02:15
  • @AlexPogue could you please elaborate ? as i am a beginner to programming... Thank you – memg Oct 21 '15 at 02:15
  • Sure, under the line `}game1, game2;`, write `struct video_game game[3];` – AlexPogue Oct 21 '15 at 02:16
  • Then your for loop will change to `for(int i =0; i<3; i++)`. So it will count like so: 0, 1, 2. – AlexPogue Oct 21 '15 at 02:18
  • Then in your main function, instead of setting `game1` attributes, you'll want to set `game[0]` attributes. So basically, change all the `game1`s in main to `game[0]`s – AlexPogue Oct 21 '15 at 02:19
  • @AlexPogue Sir i tried it but got all these errors ? that is just posted in the code above – memg Oct 21 '15 at 02:24
  • @memg You are trying to assign strings (e.g. "2012") to an integer. I think you might want to read an introductory text on C types, arrays, and expressions before asking for help with basic syntax. – EBlake Oct 21 '15 at 02:32

1 Answers1

1

The issues with your code:

  1. In the line: printf("lower age: %d", game[i].lower age);, lower age should be lower_age.
  2. You should define an array like struct video_game game[1]; if you plan on using the for loop in print_video_game_details()
  3. For loop limits should start at i = 0 and go while i < [however many games there are]
  4. Main should define game[0], game[1], etc., rather than game1, game2, etc.
  5. game[0].year is an int, so don't put the 2012 in quotes
  6. game[0].price is a float, so leave off the quotes and remove the dollar sign.

Here is the code we've discussed in the comments. It works with one video game. You can increase the video_game game[x] number and the for loop in order to support multiple video games.

#include <stdio.h>
#include <string.h>

struct video_game
{
    char* title;
    char* genre;
    char* developer;
    int year;
    char* platform;
    int lower_age;
    float price;
    char* inapp_purchase;
}game1, game2;

struct video_game game[1];

void print_video_game_details()
{
    int i;
    for(i = 0; i < 1; i++)
    {
        printf("Title: %s\n", game[i].title);
        printf("Genre: %s\n", game[i].genre);
        printf("Developer: %s\n", game[i].developer);
        printf("year of release: %d\n", game[i].year);
        printf("platform: %s\n", game[i].platform);
        printf("lower age: %d\n", game[i].lower_age);
        printf("price: %f\n", game[i].price);
        printf("inapp_purchase: %s\n", game[i].inapp_purchase);
    }
}

int main(int argc, char* argv[])
{
    game[0].title = "Candy crush saga";
    game[0].genre = "Match-Three Puzzle";
    game[0].developer = "King";
    game[0].year = 2012;
    game[0].platform = "Android, ios, Windows Phone";
    game[0].lower_age = 7;
    game[0].price = 0.0;
    game[0].inapp_purchase = "yes";
    print_video_game_details();
}
AlexPogue
  • 757
  • 7
  • 14