1

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

    }

}
2
  • In your printMusicLibrary() function, the for loop body never executes because you've set empty to true (so (!empty) is false) right before it. Looks like setting it to false instead (before the loop) should work, since you already check for empty entries inside the loop. Commented Nov 28, 2015 at 6:27
  • Would be nice to have a code snipet that compiles, or at least that is complete: we do not have the complete do {} while() loop here Commented Nov 28, 2015 at 9:18

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.