I have a list of books that I need to access from 3 or 4 different functions of my program. It's 250 books with unique names. Would it be wise to use simple array to store the list?

Or is there any easy way for a newbie to manually create the list and then access it? I'm new to this, making a transition from JavaScript where I would use JSON.

So basically is this right way to do it:

string books[250] = {"MyBook One", "My second book", "Third book", "Book nr 250"};

and the call it like this:

void displayRandomBook(booknr)
{
  ... some code ....
  sdlTTFdisplay(books[booknr]);
}

The right way depends on the situation. But I would suggest to do something like this:

#include <vector>

struct Book{
 string _name;
 string _author;
 float  _price;
 //...more info
 Book(const string& name,const string& author, float price)
   : _name(name), _author(author), _price(price){}
};

int main(){
 std::vector<Book> listOfBooks;
 listOfBooks.push_back( Book("America","Obama Hussian", 59.95);
 //add more books
 func1(listOfBooks); //do stuff
 func2(listOfBooks); //do more stuff
 //and so on...
}

Hi,

To store the books thats a valid approach but manually coding all the book names is a bad idea i think. i suggest scanning a directory and geting the file names if they are in one directory see link -> LINK for a few possible ways to do this.

then to add the files to the list i would reccomend using a stl (standard template libary) class vector. vector is like an array but it has various advantages. so i think you would want something like the following in pseudo c

#include<string> //libary for strings
#include<vector> //libary for vectors

using namespace std;
vector<string> myList; //variables called my list that is an array of strings

myList.resize(250); //size the vector to have 250 elements (used as you know you have 250 titles

//use a function or loop to get the file names maybe like 
//bool getTitles(char *directory)
if(!getTitles("someDir"))
{
   cout<<"some error occoured either directory name is invalid or something else bad happened"<<endl;
   return(-1);//abort the application
}

//print items 
for(int x = 0; x < myList.size(); x++)
{
   cout<<"Book number "<<x+1<<" is called: "<<myList[x]<<endl;
  /* prints a string to the console such as say a book called Little Mermaid 
     saved as the 3rd element (index 2) would print as below 
     Book number 3 is called: Little mermaid
  */
  
}

This is how i would approach this problem, hope this is useful to you and good luck :)

Also if vectors bug you a simple google search will show some good examples as thats how i worked them out

edit: beat to the punch again,.. basically if you combine my idea and firstPersons for using the struct to store more info about the books you could have a very comprehensive list from which your functions can work.

It depends on what you're planning to do with your book container:

-Do you want to find something inside lots of time? Consider using a set (O(log n) search time).
-Do you want to keep indexes or track of information inside your container. If yes, an array would be useful.

Are you planning of hardcoding your book array inside your code? I suggest that you put all the book names in a file and make your program read the file and fill the container. If you forgot a book, there's no need to recompile the program. If you don't know how to do file input in C++, Google (or any search engine) is your friend!

@GDICommander: No. No search, no nothing. Just very very simple way to hardcode it into my code and display random title(s) - that's all. I won't need to add, remove or change anything.

So, if there will be no add or remove of books, I suggest to use an array that you can allocate automatically, like this:

string myArray[300];

So, the construction of the array will be done at compile-time and there will be no construction cost at run-time.

If you're using std::vector, consider resizing the vector at the beginning to avoid memory reallocation costs when resizing is needed.

So, if there will be no add or remove of books, I suggest to use an array that you can allocate automatically, like this:

string myArray[300];

So, the construction of the array will be done at compile-time and there will be no construction cost at run-time.

If you're using std::vector, consider resizing the vector at the beginning to avoid memory reallocation costs when resizing is needed.

Using raw arrays is hardly ever the correct solution. Even if there is no need for the
extra operation, using vectors is always better than using raw arrays, especially at
such an early stage. And worrying about pre-mature optimization, is the root of all evil.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.