-1

So I have this assignment where I have a struct called Song. With this struct I am doing a dynamic allocation to an array with the Song struct.

So I have this problem when I am trying to add another song to the arrays in my struct. When using case 4 to add another song to the array, it changes the value of the variables in struct Song to trash values. I am not sure why it does that. The expected outcome should be that the array expands and adds the song to aSong. Printing the aSong array after using case 4 is where the trouble begins.

I get no compiler errors. The program just prints out garbage values.

Here is the code (I know I can make it look better by putting the code in functions instead):

#pragma warning(disable : 4996)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "lab3.h"

//Global variables
struct Song *aSong;
int howMany = 0;

int menu(struct Song *songs) {

    int answer = 0;

    printf("Choose from the menu: \n");
    printf("1. Song menu.\n");
    printf("2. Exit\n");
    scanf("%d", &answer);

    switch (answer)
    {
    case 1:

        printf("Choose from the menu: \n");
        printf("1. Add song. \n");
        printf("2. Randomize list.\n");
        printf("3. Print list.\n");
        printf("4. Add another song.\n");
        printf("5. Go back\n");
        scanf("%d", &answer);

        switch (answer)
        {

        case 1:
            printf("How many songs would you like to add right now?: \n");
            scanf("%d", &howMany);
            getchar();

            aSong = (struct Song *) malloc((sizeof(struct Song) * howMany));

            for (int i = 0; i < howMany; i++) {
                //Adds songs to the array. Depends on how many the user wants to add
                printf("Enter a songname: \n");
                fgets(aSong[i].titel, SIZE, stdin);
                fflush(stdin);
                getchar();
                printf("Enter the artist/band: \n");
                fgets(aSong[i].artist, SIZE, stdin);
                fflush(stdin);
                getchar();
                printf("Enter which year the song was released: \n");
                scanf("%d", &aSong[i].releaseD);
                fflush(stdin);
                getchar();
            }
            printf("Music added!\n");
            getchar();
            menu(&songs);
            break;

        case 3:
            printf("-------------------------------\n");
            printf("Songs stored: \n");
            //Prints the songs
            for (int i = 0; i < howMany; i++) {
                printf("\nSong titel: %s Band/Artist: %s Release year: %d\n", aSong[i].titel, aSong[i].artist, aSong[i].releaseD);
            }
            printf("-------------------------------\n");
            menu(&songs);
            getchar();
            break;

        case 4:
            //Add another song to the array
            printf("Add another song: \n");
            struct Song* tmp = (struct Song*)malloc((howMany + 1) * sizeof(struct Song));

            //Change the array by increasing the nr of slots
            for (int i = 0; i < howMany; i++) {
                tmp[i] = aSong[i];
            }

            //Redirect the pointers so it points to the correct array
            free(aSong);
            aSong = tmp;
            tmp = NULL;

            printf("Enter song name: \n");
            fgets(aSong[howMany].titel, SIZE, stdin);
            getchar();
            printf("Enter band/artist name: \n");
            fgets(aSong[howMany].artist, SIZE, stdin);
            getchar();
            printf("Enter the year when the song was released:\n");
            //scanf(" %d", &aSong[howMany].releaseD);
            fgets(aSong[howMany].artist, SIZE, stdin);
            getchar();
            printf("Song added!");
            printf("-------------------");

            howMany++;
            free(aSong);
            menu(&songs);
            break;

        case 5:
            printf("Exit.");
            menu(&songs);
            break;
        }
    case 2:
        return 0;
        break;
    }
}

My main.c file only calls the menu(&songs) function.

I am using a menu system that allows the user to choose what they want to do. A basic use of the system goes like this:

 * You enter the "Add song" menu. 
 * You choose how many songs you would like to enter
 * The user adds the info of the song
 * User prints the stored songs with case 3
 * User wants to add another song to the array with case 4
 * User enters data again to add another song (YOU CAN'T ADD SONGS AGAIN WITH CASE 1, YOU HAVE TO USE CASE 4)
 * User wants to print the songs again with the print case 3
 * Program prints out trash values and the old songs that printed out nicely before are now trash also.

I cant seem to understand what I am doing wrong. Someone enlighten me please.

The lab3.h file with the struct:

#ifndef LAB3_H
#define LAB3_H
#define SIZE 80

struct Song
{
    char titel[SIZE];
    char artist[SIZE];
    int releaseD;
};
int menu(struct Song *songs);

#endif // !LAB3_H

EDIT

(In main.c I do have the _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); to find memory leaks.)

Lsm
  • 1
  • 1

1 Answers1

1

At the end of case 4 there is a call free(aSong), so the content of the array is lost in the recursive call of menu. Hope this helps.

coquille
  • 11
  • 3
  • By removing the free(aSong) in case 4 (at the bottom) I was able to print out the band/artist name. Still no title and the releaseD got a trash value. This is in the right direction I think! – Lsm Jan 29 '17 at 13:24