Doing another big project (Arrays and Structures)

In summary: Names; 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 Bakervilles"; bookList.at(0).authorName = "Arthur Conan Doyle"; bookList.at(0).yearPublished = 1902; bookList.at(1).bookName = "True
  • #1
WarGhSt
15
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: 67
Technology news on Phys.org
  • #2
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:
  • #3
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:
  • #4
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.
 
  • #5
A suggestion: write separate functions for each menu item.
 
  • #6
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;
        }
 

Related to Doing another big project (Arrays and Structures)

1. What are arrays and structures?

Arrays and structures are data structures used in computer programming to store and organize data. Arrays are a collection of elements of the same type, while structures are a collection of different data types grouped together.

2. What are the benefits of using arrays and structures?

Arrays and structures allow for efficient storage and retrieval of data. They also make it easier to manipulate and organize large amounts of data, and can improve the overall performance of a program.

3. How do you declare and initialize an array or structure?

To declare an array, you must specify the data type and the number of elements in the array. For example, int myArray[5] would declare an array of 5 integers. To initialize the array, you can assign values to each element using brackets. Structures are declared similarly, but you must also define the data types and names of each member within the structure.

4. Can arrays and structures be used together?

Yes, arrays and structures can be used together to create more complex data structures. For example, you can have an array of structures, where each element in the array is a structure with multiple members.

5. How do you access and modify elements in an array or structure?

To access an element in an array, you can use its index number, which starts at 0. For example, myArray[2] would access the third element in the array. To modify an element, you can assign a new value to it using the assignment operator (=). To access and modify elements in a structure, you use the dot operator (.) followed by the member name. For example, myStruct.member = 10 would assign a value of 10 to the member "member" in the structure "myStruct".

Back
Top