Beginner with C++ here.
I am learning data structures and can't figure out how to point in a function inside a loop. e.g.
struct book
{
string author;
string title;
int publicationYear;
};
void setBook(book&);
int main()
{
book bookInfo[3];
setBook(bookInfo);
return 0;
}
void setBook(book &bookToSet)
{
for(int i = 0; i < 3; i++)
{
cout << "Who is the author of the book: ";
cin >> bookToSet[i].author;
cout << "What is the title of the book: ";
cin >> bookToSet[i].title;
cout << "In what year was the book published: ";
cin >> bookToSet[i].publicationYear;
}
}
This doesn't work and I am not sure why.
Inside the loop in the function I also tried to write (bookToSet + 1).author
(as to refer to adress), but it also didn't work.
I struggle with pointers and addresses quite a lot.
EDIT:
I tried to create function (similar to setBooks) to print the title, author and publicationYear, but it won't compile. Where's the error?
void printBooks(book bookToPrint, int cout)
{
for(int i = 0; i < cout; i++)
{
cout << bookToPrint[i].title << " " << bookToPrint[i].author << " " << bookToPrint[i].publicationYear << endl;
}
}