ender987
ender987

Reputation: 23

Homework C programming stack and heap with string

This is a homework assignment that I could use some help with. I seem to have everything working, but I am having issues with the the name of the games. As you can see my names are not showing up appropriately in the output nor while I am trying to debug, its a lot of gibberish and such. Could someone please explain what is causing this or give me an example on just one of the games_stack[i].name[i] to make it work appropriately?

Output

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
#define PAUSE system("pause")

//Games Struct
typedef struct
{
    char name[50]; // name of game
    int mScore; // metacritic score
    char rating; // ESRB rating
}GAMES;

int main(void) {

    GAMES games_stack[MAX];

    int numElements = 10;

    int i;

    //Populate array in stack
    //games_stack[0].name[0] = "Bioshock Infinite";
    games_stack[0].name[0] = "Bioshock Infinite";
    games_stack[0].mScore = 94;
    games_stack[0].rating = 'M';

    games_stack[1].name[1] = "Half-life 2";
    games_stack[1].mScore = 96;
    games_stack[1].rating = 'M';

    games_stack[2].name[2] = "Mario Kart Double Dash";
    games_stack[2].mScore = 87;
    games_stack[2].rating = 'E';

    games_stack[3].name[3] = "Legend of Zelda: Twilight Princess";
    games_stack[3].mScore = 96;
    games_stack[3].rating = 'T';

    games_stack[4].name[4] = "Rocket League";
    games_stack[4].mScore = 86;
    games_stack[4].rating = 'E';

    games_stack[5].name[5] = "Counter-Strike: Global Offensive";
    games_stack[5].mScore = 83;
    games_stack[5].rating = 'M';

    games_stack[6].name[6] = "Assassin's Creed II";
    games_stack[6].mScore = 80;
    games_stack[6].rating = 'M';

    games_stack[7].name[7] = "Batman: Arkham Asylum";
    games_stack[7].mScore = 91;
    games_stack[7].rating = 'T';

    games_stack[8].name[8] = "Middle-eart: Shadow of Mordor";
    games_stack[8].mScore = 84;
    games_stack[8].rating = 'M';

    games_stack[9].name[9] = "Portal";
    games_stack[9].mScore = 90;
    games_stack[9].rating = 'T';

    // Create an array on the heap to store the same number of populated elements from the stack array.

    GAMES *games_heap = (GAMES*)malloc(numElements * sizeof(GAMES));

    // if memory not allocated, exit

    if (games_heap == NULL)

    {

        printf("\n Memory not allocated");

        return EXIT_FAILURE;

    }

    //Copy the values from the stack array into the dynamic array.

    for (i = 0; i < numElements; i++)

    {

        games_heap[i].name[i] = games_stack[i].name[i];

        games_heap[i].mScore = games_stack[i].mScore;

        games_heap[i].rating = games_stack[i].rating;

    }

    // print the elements from both the array

    for (i = 0; i < numElements; i++)

    {

        printf("\n Stack : ");

        printf(" Name: %c\t MetaScore: %d\t ESRB Rating: %c", games_stack[i].name[i], 
            games_stack[i].mScore, games_stack[i].rating);

        printf("\n Heap : ");

        printf(" Name: %c\t MetaScore: %d\t ESRB Rating: %c", games_heap[i].name[i],
            games_heap[i].mScore, games_heap[i].rating);

    }
    PAUSE;
    // write contents of heap to binary file

    FILE *filePtr = fopen("games.bin", "w");

    if (filePtr == NULL)

    {

        printf("Unable to open file");

        return EXIT_FAILURE;

    }

    for (i = 0; i < numElements; i++)

    {

        if (i != numElements - 1)

            fprintf(filePtr, "%c %d %c\n", games_heap[i].name[i],
                games_heap[i].mScore, games_heap[i].rating);

        else

            fprintf(filePtr, "%c %d %c", games_heap[i].name[i],
                games_heap[i].mScore, games_heap[i].rating);

    }

    fclose(filePtr);

    return EXIT_SUCCESS;

}

//end of program

Upvotes: 0

Views: 263

Answers (1)

Nguyen Cong
Nguyen Cong

Reputation: 189

There are several problems with your code:

  • Your game name is a char array, so your game[i] is a character only. And you can not assign a string to a char. You better use strncpy.
  • You can only assign the value to an array when you initialize it, so games_heap[i].name[i] = games_stack[i].name[i] doesn't work. Moreover, in this case, you only copy 1 character.
  • Use %s to print a char array.

I add here a quick fix code to make it work basically. Sorry for not able to optimize it more since I am not an expert. Hope this help.

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

#define GAME_NAME_LENGTH 50
#define MAX 1000
#define PAUSE system("pause")

//Games Struct
typedef struct
{
    char name[GAME_NAME_LENGTH]; // name of game
    int mScore; // metacritic score
    char rating; // ESRB rating
}GAMES;

int main(void) {

    GAMES games_stack[MAX];

    int numElements = 10;

    int i;

    //Populate array in stack
    //games_stack[0].name[0] = "Bioshock Infinite";
    strncpy(games_stack[0].name, "Bioshock Infinite", GAME_NAME_LENGTH);
    games_stack[0].mScore = 94;
    games_stack[0].rating = 'M';

    strncpy(games_stack[1].name, "Half-life 2", GAME_NAME_LENGTH);
    games_stack[1].mScore = 96;
    games_stack[1].rating = 'M';

    strncpy(games_stack[2].name, "Mario Kart Double Dash", GAME_NAME_LENGTH);
    games_stack[2].mScore = 87;
    games_stack[2].rating = 'E';

    strncpy(games_stack[3].name, "Legend of Zelda: Twilight Princess", GAME_NAME_LENGTH);
    games_stack[3].mScore = 96;
    games_stack[3].rating = 'T';

    strncpy(games_stack[4].name, "Rocket League", GAME_NAME_LENGTH);
    games_stack[4].mScore = 86;
    games_stack[4].rating = 'E';

    strncpy(games_stack[5].name, "Counter-Strike: Global Offensive", GAME_NAME_LENGTH);
    games_stack[5].mScore = 83;
    games_stack[5].rating = 'M';

    strncpy(games_stack[6].name, "Assassin's Creed II", GAME_NAME_LENGTH);
    games_stack[6].mScore = 80;
    games_stack[6].rating = 'M';

    strncpy(games_stack[7].name, "Batman: Arkham Asylum", GAME_NAME_LENGTH);
    games_stack[7].mScore = 91;
    games_stack[7].rating = 'T';

    strncpy(games_stack[8].name, "Middle-eart: Shadow of Mordor", GAME_NAME_LENGTH);
    games_stack[8].mScore = 84;
    games_stack[8].rating = 'M';

    strncpy(games_stack[9].name, "Portal", GAME_NAME_LENGTH);
    games_stack[9].mScore = 90;
    games_stack[9].rating = 'T';

    // Create an array on the heap to store the same number of populated elements from the stack array.

    GAMES *games_heap = (GAMES*)malloc(numElements * sizeof(GAMES));

    // if memory not allocated, exit

    if (games_heap == NULL)

    {

        printf("\n Memory not allocated");

        return EXIT_FAILURE;

    }

    //Copy the values from the stack array into the dynamic array.

    for (i = 0; i < numElements; i++)

    {

        strncpy(games_heap[i].name, games_stack[i].name, GAME_NAME_LENGTH);

        games_heap[i].mScore = games_stack[i].mScore;

        games_heap[i].rating = games_stack[i].rating;

    }

    // print the elements from both the array

    for (i = 0; i < numElements; i++)

    {

        printf("\n Stack : ");

        printf(" Name: %s\t MetaScore: %d\t ESRB Rating: %c", games_stack[i].name, 
            games_stack[i].mScore, games_stack[i].rating);

        printf("\n Heap : ");

        printf(" Name: %s\t MetaScore: %d\t ESRB Rating: %c", games_heap[i].name,
            games_heap[i].mScore, games_heap[i].rating);

    }
    PAUSE;
    // write contents of heap to binary file

    FILE *filePtr = fopen("games.bin", "w");

    if (filePtr == NULL)

    {

        printf("Unable to open file");

        return EXIT_FAILURE;

    }

    for (i = 0; i < numElements; i++)

    {

        if (i != numElements - 1)

            fprintf(filePtr, "%s %d %c\n", games_heap[i].name,
                games_heap[i].mScore, games_heap[i].rating);

        else

            fprintf(filePtr, "%s %d %c", games_heap[i].name,
                games_heap[i].mScore, games_heap[i].rating);

    }

    fclose(filePtr);

    return EXIT_SUCCESS;

}

//end of program

Upvotes: 1

Related Questions