user3916398
user3916398

Reputation: 47

Printing an array of structs in C

I'm trying to print an array of structs that contain two strings. However my print function does not print more than two indices of the array. I am not sure why because it seems to me that the logic is correct.

This is the main function

      const int MAX_LENGTH = 1024;

      typedef struct song
      {
          char songName[MAX_LENGTH];
          char artist[MAX_LENGTH];
      } Song;


      void getStringFromUserInput(char s[], int maxStrLength);
      void printMusicLibrary(Song library[], int librarySize);


      void printMusicLibraryTitle(void);
      void printMusicLibrary (Song library[], int librarySize);
      void printMusicLibraryEmpty(void);


      int main(void) {

      // Announce the start of the program
      printf("%s", "Personal Music Library.\n\n");
      printf("%s", "Commands are I (insert), S (sort by artist),\n"
       "P (print), Q (quit).\n");

      char response;
      char input[MAX_LENGTH + 1];
      int index = 0;

      do {
          printf("\nCommand?: ");
          getStringFromUserInput(input, MAX_LENGTH);

          // Response is the first character entered by user.
          // Convert to uppercase to simplify later comparisons.
          response = toupper(input[0]);

          const int MAX_LIBRARY_SIZE = 100;
          Song Library[MAX_LIBRARY_SIZE];

          if (response == 'I') {

               printf("Song name: ");
               getStringFromUserInput(Library[index].songName, MAX_LENGTH);


                printf("Artist: ");
                getStringFromUserInput(Library[index].artist, MAX_LENGTH);

                index++;


          }

          else if (response == 'P') {
               // Print the music library.
              int firstIndex = 0;

              if (Library[firstIndex].songName[firstIndex] == '\0') {
              printMusicLibraryEmpty();
              } else {
                  printMusicLibraryTitle();
                  printMusicLibrary(Library, MAX_LIBRARY_SIZE);
              }

This is my printing the library function

    // This function will print the music library
void printMusicLibrary (Song library[], int librarySize) {

    printf("\n");

    bool empty = true;

    for (int i = 0; (i < librarySize) && (!empty); i ++) {

        empty = false;

        if (library[i].songName[i] != '\0') {
            printf("%s\n", library[i].songName);
            printf("%s\n", library[i].artist);
            printf("\n");
        } else {
            empty = true;
        }

    }

}

Upvotes: 1

Views: 218

Answers (3)

OznOg
OznOg

Reputation: 4722

you gave the definition as:

  typedef struct song
  {
      char songName[MAX_LENGTH];
      char artist[MAX_LENGTH];
  }Song;

the later, you write if (library[i].songName[i] != '\0') which really seems strange: why would you index the songname string with the same index that the lib? so I would naturally expect your print function to be:

    // This function will print the music library
void printMusicLibrary (Song library[], int librarySize) {

   for (int i = 0; i < librarySize; i ++) {

       printf("%s\n%s\n\n", library[i].songName,
                            library[i].artist);

   }
}

note that you may skip empty song names by testing library[i].songName[0] != '\0' (pay attention to the 0), but I think it would be better not to add them in the list (does an empty song name make sens?)

(If you decide to fix that, note that you have an other fishy place: if (Library[firstIndex].songName[firstIndex] == '\0') with the same pattern)

Upvotes: 1

Pukki
Pukki

Reputation: 596

I think the problem is caused due to setting : empty = true outside the for loop and then checking (!empty) which will evaluate to false. What I am surprised by is how is it printing even two indices. You should set empty = false as you are already checking for the first index before the function call.

Upvotes: 2

wallyk
wallyk

Reputation: 57774

The logic has two ways to terminate the listing: 1) if the number of entries is reached, or 2) if any entry is empty.

I expect the second condition is stopping the listing before you expect. Probably the array wasn't built as expected (I didn't look at that part), or something is overwriting an early or middle entry.

Upvotes: 1

Related Questions