Home > Software design >  My recursive display function is not going to the next value of the array
My recursive display function is not going to the next value of the array

Time:10-24

I have a recursive display function, meant to go through the values of array b and print the details. My function is successful in looping the correct amount of times, but only prints out the value at index 0. For example, if I have 3 books, it prints out the first book 3 times and is not going to the other values. I am a beginner programmer and I believe I am missing something very simple or obvious, but any help is appreciated.

void displayBooks(int n)
{
// Write the code below

    if (n >= currentCount) {
        return;
    }
    else {
    
        cout << "Name: " << b->getName() << "\n";
        cout << "Name of Book Author: " << b->getAuthor() << "\n";
        cout << "Publication Year: " << b->getYearOfPublication() << "\n";
        cout << "ID: " << b->getID() << "\n";

        displayBooks(n   1);
    }
}

This is the function itself, however I can't show the complete program since it is a lot of code with multiple files. When the function is first called as displayBooks(0) in a switch case.

CodePudding user response:

I believe that you are not printing out each index of the "b" variable you need to access the index of each one. You need to have b as an array of pointers then access the index of that variable like b[n]->someProperty();

You can create the array like this:

Obj* b[HOWMANY];
  • Related