Home > database >  I can't print out the name from the struct in c
I can't print out the name from the struct in c

Time:08-04

#include <stdio.h>

struct MovieData
{
    char *Title;
    char *Director;
    int *YR; //year released
    int *min;
};

void displayMovie(struct MovieData);

int main(void)
{
    struct MovieData movie1, movie2, movie3;

    movie1.Title = "Spider-Man: No Way Home";
    movie1.Director = "Jon Watts";
    movie1.YR = 2021;
    movie1.min = 159;

    movie2.Title = "Captain Marvel";
    movie2.Director = "Anna Boden, Ryan Fleck";
    movie2.YR = 2019;
    movie2.min = 123;

    movie3.Title = "Black Widow";
    movie3.Director = "Cate Shortland";
    movie3.YR = 2021;
    movie3.min = 133;

    printf("\nMovie 1\n");

    displayMovie(movie1);

    printf("\nMovie 2\n");

    displayMovie(movie2);

    printf("\nMovie 3\n");

    displayMovie(movie3);

    return 0;
}


void displayMovie(struct MovieData movie)
{
    printf("Title: \n", movie.Title);
    printf("Director: \n", movie.Director);
    printf("Year Released: \n", movie.YR);
    printf("Runtime: \n", movie.min);
}

I was able to print out the bottom portion but the movie that I physically typed isn't printing out to the user, where did I go wrong or did I deviate from something? This program is in C and the user should see about 3 movies printed out to them showing them the title, director, year released, and the runtime in minutes.

CodePudding user response:

This should suffice.

I don't think you need pointers to int, also specify the types to be printed.

#include <stdio.h>

struct MovieData
{
    char *Title;
    char *Director;
    int YR; //year released
    int min;
};

void displayMovie(struct MovieData);

int main(void)
{
    struct MovieData movie1, movie2, movie3;

    movie1.Title = "Spider-Man: No Way Home";
    movie1.Director = "Jon Watts";
    movie1.YR = 2021;
    movie1.min = 159;

    movie2.Title = "Captain Marvel";
    movie2.Director = "Anna Boden, Ryan Fleck";
    movie2.YR = 2019;
    movie2.min = 123;

    movie3.Title = "Black Widow";
    movie3.Director = "Cate Shortland";
    movie3.YR = 2021;
    movie3.min = 133;

    printf("\nMovie 1\n");

    displayMovie(movie1);

    printf("\nMovie 2\n");

    displayMovie(movie2);

    printf("\nMovie 3\n");

    displayMovie(movie3);

    return 0;
}


void displayMovie(struct MovieData movie)
{
    printf("Title: %s \n", movie.Title);
    printf("Director: %s \n", movie.Director);
    printf("Year Released: %d \n", movie.YR);
    printf("Runtime: %d \n", movie.min);
}
  • Related