Help with some structures/for loops

I am working on a program to make a small library of the ten books I have chosen. With this program I should be able to

A) Print the full library inventory (Author, title, date of publish)
B) Allow my user to search a publishing year for a list of books from that date up to current day
C) Allow my user to search an author name for a list of books by said author.

The *issue* is that when I enter, say, "1925" for the publishing year, it is only returning to me the book Inheritance by C. Paolini, not the multiple other books that were published after 1925. And similarly, when I enter an author's name, I can't get any list at all in return.

Any pointers would be extremely helpful!!
The following is my current code.
_______________________________________________________________________
#include<iostream>
#include<string>
#include<vector>
using namespace std;

struct book
{
string title;
string author;
int year;
};


int main() {
book b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;


b1.year = 1998;
b1.title = "Harry Potter and the Chamber of Secrets";
b1.author = "J.K. Rowling";

b2.year = 2000;
b2.title = "Harry Potter and the Goblet of Fire";
b2.author = "J.K. Rowling";

b3.year = 2005;
b3.title = "The Penultimate Peril";
b3.author = "Daniel Handler";

b4.year = 1984;
b4.title = "Garfield Bigger Than Life";
b4.author = "Jim Davis";

b5.year = 1984;
b5.title = "Garfield Weighs In";
b5.author = "Jim Davis";

b6.year = 1925;
b6.title = "The Great Gatsby";
b6.author = "F. Scott Fitzgerald";

b7.year = 2006;
b7.title = "The Road";
b7.author = "Cormac McCarthy";

b8.year = 1898;
b8.title = "The War of the Worlds";
b8.author = "H.G. Wells";

b9.year = 2002;
b9.title = "Eragon";
b9.author = "Christopher Paolini";

b10.year = 2011;
b10.title = "Enheritance";
b10.author = "Christopher Paolini";

vector<book> books;
books.push_back(b1);
books.push_back(b2);
books.push_back(b3);
books.push_back(b4);
books.push_back(b5);
books.push_back(b6);
books.push_back(b7);
books.push_back(b8);
books.push_back(b9);
books.push_back(b10);

int x;
cout << "Welcome to Superduper Library." << endl;
cout << "Enter 1 for a list of our library inventory." << endl;
cout << endl;
cin >> x;

if (x == 1)
{
cout << b1.year << endl;
cout << b1.title << endl;
cout << b1.author << endl;
cout << endl;

cout << b2.year << endl;
cout << b2.title << endl;
cout << b2.author << endl;
cout << endl;

cout << b3.year << endl;
cout << b3.title << endl;
cout << b3.author << endl;
cout << endl;

cout << b4.year << endl;
cout << b4.title << endl;
cout << b4.author << endl;
cout << endl;

cout << b5.year << endl;
cout << b5.title << endl;
cout << b5.author << endl;
cout << endl;

cout << b6.year << endl;
cout << b6.title << endl;
cout << b6.author << endl;
cout << endl;

cout << b7.year << endl;
cout << b7.title << endl;
cout << b7.author << endl;
cout << endl;

cout << b8.year << endl;
cout << b8.title << endl;
cout << b8.author << endl;
cout << endl;

cout << b9.year << endl;
cout << b9.title << endl;
cout << b9.author << endl;
cout << endl;

cout << b10.year << endl;
cout << b10.title << endl;
cout << b10.author << endl;
cout << endl;
}


int year;
cout << "Enter a year of publication for a list of other year-to-date releases." << endl;
cin >> year;

int i;
for (i = 0; i < books.size()-1; i++);
{
cout << books.at(i).year << endl;
cout << books.at(i).title << endl;
cout << books.at(i).author << endl;
cout << endl;
}

string name;
cout << "Enter an author name for a list of books published by that author." << endl;
cin >> name;
for (i = 0; i < books.size() - 1; ++i); { // Find author's index
if (books.at(i).author == name) {
name = true;
cout << "books by " << name << endl;
cout << "watch " << books.at(i).author << endl;
}
}
system("pause");
return 0;
}
Last edited on
> int i;
> for (i = 0; i < books.size()-1; i++);
...
> for (i = 0; i < books.size() - 1; ++i); { // Find author's index

Those trailing ; make your loops redundant.

Had you done this however, you would have gotten an error, because the subscript you thought you had would have ended it's scope at the logical end of the for loop.
1
2
3
for ( size_t i = 0 ; i < books.size(); i++); {
  // do something with i
}

1
2
string name;
cin >> name;

That is formatted input that skips prefix whitespace and stops at the next whitespace.
In other words, if you do write
F. Scott Fitzgerald

The read operation extracts only the "F." and leaves
 Scott Fitzgerald

to the stream. (If the next read expects an integer, then the stream goes into failed state for "S" in no int.)

In order to read multiple "words" as one string, one has to use something else, like std::getline.
The getline reads up to "separator character" (which is a newline by default).
Mixing getline and formatted input is tricky too.

How about a partial match?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <vector>

struct Book
{
  std::string title;
  std::string author;
  int year;
};

std::ostream& operator<< ( std::ostream& out, const Book & book )
{
  out << book.year << ' ' << book.author << ' ' << book.title;
  return out;
}

int main()
{
  std::vector<Book> books {
   {"Harry Potter and the Chamber of Secrets", "J.K. Rowling", 1998},
   {"The Penultimate Peril", "Daniel Handler", 2005} };
  
  for ( auto book : books ) {
      std::cout << book << '\n';
  }
  std::cout << "\n\n";

  for ( auto book : books ) {
      if ( 2001 <= book.year ) std::cout << book << '\n';
  }
  std::cout << "\n\n";

  std::string name = "Rowl";
  for ( auto book : books ) {
      // if 'author' contains substring 'name'
      if ( std::string::npos != book.author.find( name ) ) std::cout << book << '\n';
  }
}
Topic archived. No new replies allowed.