Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 843507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:08:20+00:00 2026-05-15T06:08:20+00:00

I am writing a program for class that is asking us to create a

  • 0

I am writing a program for class that is asking us to create a class of “book”. We are then supposed to create new instantiations of that class upon demand from the user. I am new to C++ so I am attempting to code this out but am running into a problem.

The main problem is how do I instantiate a class with a variable if I don’t know how many I will have to do ahead of time. The user could ask to add 1 book or 1000. I am looking at this basic code:

This is the simple code I started with. I wanted to have an index int keep a number and have the book class I create be called by that int (0, 1, 2, etc…) So I attempted to convert the incoming index int into a string, but I’m kind of stuck from here.

void addBook(int index){
    string bookName;
    std::stringstream ss;
    ss << index;

    book bookName;

    cout << "Enter the Books Title: ";
    cin >> bookName.title; 
}

But obviously this doesn’t work as “bookName” is a string to the computer and not the class member I tried to create.

All of the tutorials I have seen online and in my text show the classes being instantiated with names in the code, but I just don’t know how to make it variable so I can create any amount of “books” that the user might want. Any insight on this would be appreciated. Thank you for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:08:21+00:00Added an answer on May 15, 2026 at 6:08 am

    Given your type book, if you want to create a list of books, try using a container like std::vector, std::list or std::deque.

    typedef std::vector<book> library_type;
    
    library_type library;
    
    book catch22("Catch 22")
    library.push_back(catch22);
    
    book haltingState("Halting State");
    library.push_back(haltingState);
    

    You can create books and add to the library from a loop, which sounds like what you want.

    Your choice of container type will depend on the access pattern you want. For example, a std::vector is good if you want to add books like this and you rarely want to remove them in an arbitrary order. It’s fairly straightforward to change the type later if you change your mind about this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 468k
  • Answers 468k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try to not using variables at table names: $liveposts =… May 16, 2026 at 2:27 am
  • Editorial Team
    Editorial Team added an answer bool aredifferent = "abcDEf" != "abcdef"; //OR string str1 =… May 16, 2026 at 2:27 am
  • Editorial Team
    Editorial Team added an answer I would recommend against having the control connect to the… May 16, 2026 at 2:27 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.