Click here to Skip to main content
15,889,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a very basic library system. I have a set called books and one called author. They are both filled with user input, I have no problem with that.

The user will select an option to retrieve all book names from a menu option. This is listed as case1 below. It will call the void getbooknames() function.

How can I get those set values to be passed to that function so they can be displayed on screen?

What I have tried:

C++
<pre>

//menu2 is called from int main()
//book names are input via a function called void addBook()

void menu2()
{
    char choice2;
    cout << "\nTell us what you want to do today: ";
    cout << "\n  1. Retrieve list of book names";
    cout << "\n  2. Retrieve list of author names";
    //other options not included.

 do
	{
    cout << "\nPlease key in your option number: ";
    cin >> choice2;

	switch (choice2)
		{
		case 1:
		    getbooknames(book?);
			break;
 		case 2:
			//getauthnames();
        case 3:
            menu3();
		case 4:
    		menu1();
		}
	} while (choice2 != 4);
}

void getbooknames(book?) //this is where i am confused. Do i pass by reference somehow?
{
   for (auto pos = book.begin(); pos != book.end(); ++pos) 
    {
      cout << ' ' << *pos << "\n";
    } 
}
Posted
Updated 2-Sep-21 20:17pm

Passing by reference does make sense:
C++
// ...
  switch (choice2)
    {
    case 1:
        getbooknames(book);
      break;
    case 2:
      //getauthnames();
        case 3:
            menu3();
    case 4:
        menu1();
    }
  } while (choice2 != 4);
}

void getbooknames(const set<Book> & book) 
{
   for (const auto & pos = book.begin(); pos != book.end(); ++pos)
    {
      cout << ' ' << *pos << "\n";
    }
}
 
Share this answer
 
v2
Comments
KarstenK 3-Sep-21 2:44am    
You are right: copying complex data is always bad for performance and energy!!!
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

So start by reading your task again, and thinking about what you are trying to do: why are you passing anything to the function? What will it do with it? Would it be easier and more consistent to return a value instead?

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900