memg
memg

Reputation: 3

Using functions to print structures

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.

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";
 ^

Upvotes: 0

Views: 2009

Answers (1)

AlexPogue
AlexPogue

Reputation: 777

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();
}

Upvotes: 1

Related Questions