Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My recursive display function is not going to the next value of the array

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading