MHB Doing another big project (Arrays and Structures)

AI Thread Summary
The discussion revolves around a coding project involving the use of structures and arrays to manage a library of books. The user expresses uncertainty about how to start, particularly regarding the implementation of structures for individual books versus a collective structure for all books. They have provided initial code that includes a structure for book details and a menu for user interaction, but they seek assistance with specific menu options and functionality, such as displaying books by genre and filtering by publication year. The user also contemplates using integers to represent genres for easier counting but struggles with the implementation. Overall, the thread highlights the challenges faced in coding and the need for guidance on structuring the program effectively.
WarGhSt
Messages
15
Reaction score
0
So, like the title says, I'm doing another project. The kicker this time is that I don't even know how to start. Last time I was able to post some code I had already messed around with (and messed up) but this one I'm not even sure how to begin.

View attachment 5442

I've already done some of the work with structures and arrays through the course but I'm flailing and failing here.
Here are the books I've decided to use. Not that that helps much.

"The Hound of the Baskervilles" Arthur Conan Doyle, published 1902 Genre: (M)ystery.
"True Confessions" Novel by John Gregory Dunne, published 1977 Genre: (M)ystery
"Where the Wild Things Are" by Maurice Sendak, published 1963 Genre: (C)hildren's
"Pat the Bunny" by Dorothy Kunhardt, published 1940 Genre: (C)hildren's
"Hatchet" by Gary Paulsen, published 1987 Genre: (C)hildren's
"The Hobbit" by J.R.R. Tolkien, published 1937 Genre: (F)antasy
"The Lord of the Rings" by J.R.R. Tolkien, published 1954 Genre: (F)antasy
"The Name of the Wind" by Patrick Rothfuss, published 2007 Genre: (F)antasy
"Into the Wild" by Jon Krakauer, published 1997 Genre: (B)iography
"Night" by Elie Wiesel, published 2006 Genre: (B)iography

The program states that the books shall be stored as structures. Does that mean I want 1 structure per book or 1 structure that includes all the books? I'm getting really stressed out here, lol.
 

Attachments

  • Assignment.png
    Assignment.png
    10.6 KB · Views: 103
Technology news on Phys.org
I've given up on this front of coding. Post 3 is where I currently am at. I think it is modeled better to see me to the end.

Okay, I've got some code to go along with it. But it's very basic and I don't think it will carry me to the end of this. Especially with the requirement for genres and searching by years thrown in. I'm also not sure how to do part 1 (Printing the entire list of books if the user asks to).

Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct LibList {
  string bookName;
  string authorName;
  int yearPublished;
};

int main() {

    const int NUM_BOOKS = 10;

    vector<LibList> bookList(NUM_BOOKS);
    string bookToFind;
    bool bookFound = false;
    int i = 0;

    bookList.at(0).bookName = "The Hound of the Bakservilles";
    bookList.at(0).authorName = "Arthur Conan Doyle";
    bookList.at(0).yearPublished = 1902;
    bookList.at(1).bookName = "True Confessions";
    bookList.at(1).authorName = "John Gregory Dunne";
    bookList.at(1).yearPublished = 1977;
    bookList.at(2).bookName = "Where the Wild Things Are";
    bookList.at(2).authorName = "Maurice Sendak";
    bookList.at(2).yearPublished = 1963;
    bookList.at(3).bookName = "Pat the Bunny";
    bookList.at(3).authorName = "Dorothy Kunhardt";
    bookList.at(3).yearPublished = 1940;
    bookList.at(4).bookName = "Hatchet";
    bookList.at(4).authorName = "Gary Paulsen";
    bookList.at(4).yearPublished = 1987;
    bookList.at(5).bookName = "The Hobbit";
    bookList.at(5).authorName = "J.R.R. Tolkien";
    bookList.at(5).yearPublished = 1937;
    bookList.at(6).bookName = "The Lord of the Rings";
    bookList.at(6).authorName = "J.R.R. Tolkien";
    bookList.at(6).yearPublished = 1954;
    bookList.at(7).bookName = "The Name of the Wind";
    bookList.at(7).authorName = "Patrick Rothfuss";
    bookList.at(7).yearPublished = 2007;
    bookList.at(8).bookName = "Into the Wild";
    bookList.at(8).authorName = "Jon Krakauer";
    bookList.at(8).yearPublished = 1997;
    bookList.at(9).bookName = "Night";
    bookList.at(9).authorName = "Elie Wiesel";
    bookList.at(9).yearPublished = 2006;
    cout << "Enter book name: ";
    getline(cin, bookToFind);

    for (i = 0; i < NUM_BOOKS; ++i) { // Find book's index
        if (bookList.at(i).bookName == bookToFind) {
            bookFound = true;
            cout << "The author of " << bookToFind << endl;
            cout << "is " << bookList.at(i).authorName;
            cout << " and it was published in " << bookList.at(i).yearPublished << endl;
        }
    }
    if (!bookFound) {
        cout << "Book not found, try again." << endl;
    }

    return 0;
}
 
Last edited:
Okay, a second set of code which gives me a menu. Hopefully this will draw some more attention so I can get some help. I'm putting in an effort, I truly am!

I could use some help with menu option 2, 3, and 4.

Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct LibList {
  string bookName;
  string authorName;
  int yearPublished;
  char bookGenre;
};

int main() {

    const int NUM_BOOKS = 10;
    int menu;

    vector<LibList> bookList(NUM_BOOKS);
    string bookToFind;
    bool bookFound = false;
    int i = 0;

    bookList.at(0).bookName = "The Hound of the Baskervilles";
    bookList.at(0).authorName = "Arthur Conan Doyle";
    bookList.at(0).yearPublished = 1902;
    bookList.at(0).bookGenre = 'M';
    bookList.at(1).bookName = "True Confessions";
    bookList.at(1).authorName = "John Gregory Dunne";
    bookList.at(1).yearPublished = 1977;
    bookList.at(1).bookGenre = 'M';
    bookList.at(2).bookName = "Where the Wild Things Are";
    bookList.at(2).authorName = "Maurice Sendak";
    bookList.at(2).yearPublished = 1963;
    bookList.at(2).bookGenre = 'C';
    bookList.at(3).bookName = "Pat the Bunny";
    bookList.at(3).authorName = "Dorothy Kunhardt";
    bookList.at(3).yearPublished = 1940;
    bookList.at(3).bookGenre = 'C';
    bookList.at(4).bookName = "Hatchet";
    bookList.at(4).authorName = "Gary Paulsen";
    bookList.at(4).yearPublished = 1987;
    bookList.at(4).bookGenre = 'C';
    bookList.at(5).bookName = "The Hobbit";
    bookList.at(5).authorName = "J.R.R. Tolkien";
    bookList.at(5).yearPublished = 1937;
    bookList.at(5).bookGenre = 'F';
    bookList.at(6).bookName = "The Lord of the Rings";
    bookList.at(6).authorName = "J.R.R. Tolkien";
    bookList.at(6).yearPublished = 1954;
    bookList.at(6).bookGenre = 'F';
    bookList.at(7).bookName = "The Name of the Wind";
    bookList.at(7).authorName = "Patrick Rothfuss";
    bookList.at(7).yearPublished = 2007;
    bookList.at(7).bookGenre = 'F';
    bookList.at(8).bookName = "Into the Wild";
    bookList.at(8).authorName = "Jon Krakauer";
    bookList.at(8).yearPublished = 1997;
    bookList.at(8).bookGenre = 'B';
    bookList.at(9).bookName = "Night";
    bookList.at(9).authorName = "Elie Wiesel";
    bookList.at(9).yearPublished = 2006;
    bookList.at(9).bookGenre = 'B';    do
    {
     //Displaying Options for the menu
     cout << "1) Display books in library: (Title, Author, Year Published, and Genre)" << endl;
     cout << "2) Display how many books per genre " << endl;
     cout << "3) Display all books whose year was published after the users input" << endl;
     cout << "4) Print title of books in a genre " << endl;
     cout << "5) Exit Program " << endl;
     //Prompting user to enter an option according to menu
     cout << "Please select an option : " << endl;
     cin >> menu;  // taking option value as input and saving in variable "option"

        if(menu == 1) //Checks if the user selected one and runs with it
        {
            //displays books in library
            cout << bookList.at(0).bookName << " by " << bookList.at(0).authorName << " published in " << bookList.at(0).yearPublished;
                cout << " Genre: " << bookList.at(0).bookGenre << "." << endl;
            cout << bookList.at(1).bookName << " by " << bookList.at(1).authorName << " published in " << bookList.at(1).yearPublished;
                cout << " Genre: " << bookList.at(1).bookGenre << "." << endl;
            cout << bookList.at(2).bookName << " by " << bookList.at(2).authorName << " published in " << bookList.at(2).yearPublished;
                cout << " Genre: " << bookList.at(2).bookGenre << "." << endl;
            cout << bookList.at(3).bookName << " by " << bookList.at(3).authorName << " published in " << bookList.at(3).yearPublished;
                cout << " Genre: " << bookList.at(3).bookGenre << "." << endl;
            cout << bookList.at(4).bookName << " by " << bookList.at(4).authorName << " published in " << bookList.at(4).yearPublished;
                cout << " Genre: " << bookList.at(4).bookGenre << "." << endl;
            cout << bookList.at(5).bookName << " by " << bookList.at(5).authorName << " published in " << bookList.at(5).yearPublished;
                cout << " Genre: " << bookList.at(5).bookGenre << "." << endl;
            cout << bookList.at(6).bookName << " by " << bookList.at(6).authorName << " published in " << bookList.at(6).yearPublished;
                cout << " Genre: " << bookList.at(6).bookGenre << "." << endl;
            cout << bookList.at(7).bookName << " by " << bookList.at(7).authorName << " published in " << bookList.at(7).yearPublished;
                cout << " Genre: " << bookList.at(7).bookGenre << "." << endl;
            cout << bookList.at(8).bookName << " by " << bookList.at(8).authorName << " published in " << bookList.at(8).yearPublished;
                cout << " Genre: " << bookList.at(8).bookGenre << "." << endl;
            cout << bookList.at(9).bookName << " by " << bookList.at(9).authorName << " published in " << bookList.at(9).yearPublished;
                cout << " Genre: " << bookList.at(9).bookGenre << "." << endl;
        }
        else if(menu == 2)
        {
            //displays how many books per genre
            cout << "FIX ME" << endl;
        }
        else if(menu == 3)
        {
            //displays all books whose year was published after input
            cout << "FIX ME YEARS" << endl;
        }
        else if(menu == 4)
        {
            //print title of books in a genre
            cout << "FIX ME TITLE-GENRE" << endl;
        }
        else if(menu == 5)
        {
            //exits program
            cout << "Goodbye." << endl;
        }
            else
            {
                cout << "Not valid, try again." << endl;
            }
        }
        while(menu != 5);

        return 0;

        }
 
Last edited:
For part 2, would defining the genres (B, C, F, and M) as integers work?

And then I could call the value from each vector position for bookGenre, if it were = to B, C, F, or M add +1 and then print the value of the int?

Is my logic sound?

I've tried running it this way, but I can't seem to pull it off.
 
A suggestion: write separate functions for each menu item.
 
Got part 2 working with this super long strip of code.
Code:
else if(menu == 2)
        {
    int genreB = 0;
    int genreC = 0;
    int genreF = 0;
    int genreM = 0;
            //displays how many books per genre
            if (bookList.at(0).bookGenre == 'B') {
            genreB = genreB + 1;
         }
            if (bookList.at(0).bookGenre == 'C') {
            genreC = genreC + 1;
         }
            if (bookList.at(0).bookGenre == 'F') {
            genreF = genreF + 1;
         }
            if (bookList.at(0).bookGenre == 'M') {
            genreM = genreM + 1;
         }
            if (bookList.at(1).bookGenre == 'B') {
            genreB = genreB + 1;
         }
            if (bookList.at(1).bookGenre == 'C') {
            genreC = genreC + 1;
         }
            if (bookList.at(1).bookGenre == 'F') {
            genreF = genreF + 1;
         }
            if (bookList.at(1).bookGenre == 'M') {
            genreM = genreM + 1;
         }
            if (bookList.at(2).bookGenre == 'B') {
            genreB = genreB + 1;
         }
            if (bookList.at(2).bookGenre == 'C') {
            genreC = genreC + 1;
         }
            if (bookList.at(2).bookGenre == 'F') {
            genreF = genreF + 1;
         }
            if (bookList.at(2).bookGenre == 'M') {
            genreM = genreM + 1;
         }
            if (bookList.at(3).bookGenre == 'B') {
            genreB = genreB + 1;
         }
            if (bookList.at(3).bookGenre == 'C') {
            genreC = genreC + 1;
         }
            if (bookList.at(3).bookGenre == 'F') {
            genreF = genreF + 1;
         }
            if (bookList.at(3).bookGenre == 'M') {
            genreM = genreM + 1;
         }
            if (bookList.at(4).bookGenre == 'B') {
            genreB = genreB + 1;
         }
            if (bookList.at(4).bookGenre == 'C') {
            genreC = genreC + 1;
         }
            if (bookList.at(4).bookGenre == 'F') {
            genreF = genreF + 1;
         }
            if (bookList.at(4).bookGenre == 'M') {
            genreM = genreM + 1;
         }
            if (bookList.at(5).bookGenre == 'B') {
            genreB = genreB + 1;
         }
            if (bookList.at(5).bookGenre == 'C') {
            genreC = genreC + 1;
         }
            if (bookList.at(5).bookGenre == 'F') {
            genreF = genreF + 1;
         }
            if (bookList.at(5).bookGenre == 'M') {
            genreM = genreM + 1;
         }
            if (bookList.at(6).bookGenre == 'B') {
            genreB = genreB + 1;
         }
            if (bookList.at(6).bookGenre == 'C') {
            genreC = genreC + 1;
         }
            if (bookList.at(6).bookGenre == 'F') {
            genreF = genreF + 1;
         }
            if (bookList.at(6).bookGenre == 'M') {
            genreM = genreM + 1;
         }
            if (bookList.at(7).bookGenre == 'B') {
            genreB = genreB + 1;
         }
            if (bookList.at(7).bookGenre == 'C') {
            genreC = genreC + 1;
         }
            if (bookList.at(7).bookGenre == 'F') {
            genreF = genreF + 1;
         }
            if (bookList.at(7).bookGenre == 'M') {
            genreM = genreM + 1;
         }
            if (bookList.at(8).bookGenre == 'B') {
            genreB = genreB + 1;
         }
            if (bookList.at(8).bookGenre == 'C') {
            genreC = genreC + 1;
         }
            if (bookList.at(8).bookGenre == 'F') {
            genreF = genreF + 1;
         }
            if (bookList.at(8).bookGenre == 'M') {
            genreM = genreM + 1;
         }
            if (bookList.at(9).bookGenre == 'B') {
            genreB = genreB + 1;
         }
            if (bookList.at(9).bookGenre == 'C') {
            genreC = genreC + 1;
         }
            if (bookList.at(9).bookGenre == 'F') {
            genreF = genreF + 1;
         }
            if (bookList.at(9).bookGenre == 'M') {
            genreM = genreM + 1;
         }
            cout << "Books in (B)iography: " << genreB << " Books in (C)hildrens: " << genreC << endl;
                cout << "Books in (F)antasy: " << genreF << " Books in (M)ystery: " << genreM << endl;
        }
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top