0

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;
  }
}
  • " how to point in a function inside a loop" this sentence seems incomplete. Also "This doesn't work" is a bit too little information. How does it not work? Compiler error? Runtime error? Unexpected results? – 463035818_is_not_an_ai May 15 '17 at 09:24
  • 1
    btw I dont see any pointers/addresses in your code and I also dont see any need to use pointers – 463035818_is_not_an_ai May 15 '17 at 09:25
  • On your edit, you're confusing `count` and `std::cout` and you're missing the pointer in the first arguments type. Could you look up a tutorial on passing arrays to functions? – Lanting May 15 '17 at 10:39

2 Answers2

1

You function setBook accepts a reference to a book, however you are passing it a pointer. This is because you create an array of 3 books.

Your function setbook makes it seem like you're using it for setting a single book (otherwise you should have called it setBooks. It should then look like this:

struct book  
{  
  string author;  
  string title;  
  int publicationYear;  
};  
void setBook(book&);  
int main()  
{  
  book bookInfo[3];
  for(int i = 0; i < 3; i++)
  {
    setBook(bookInfo[i]);  
  }
  return 0;  
}

void setBook(book &bookToSet) 
{ 
  cout << "Who is the author of the book: ";  
  cin >> bookToSet.author;  
  cout << "What is the title of the book: ";  
  cin >> bookToSet.title;  
  cout << "In what year was the book published: ";  
  cin >> bookToSet.publicationYear;
}    

or alternatively

struct book  
{  
  string author;  
  string title;  
  int publicationYear;  
};  
void setBooks(book *, int);
int main()  
{  
  book bookInfo[3];
  setBooks(bookInfo ,3);  
  return 0;  
}

void setBooks(book *booksToSet, int count) 
{ 
  for (int i = 0; i != count; ++i)
  {
    cout << "Who is the author of the book: ";  
    cin >> booksToSet[i].author;  
    cout << "What is the title of the book: ";  
    cin >> booksToSet[i].title;  
    cout << "In what year was the book published: ";  
    cin >> booksToSet[i].publicationYear;
  }
}    
Lanting
  • 3,060
  • 12
  • 28
0

You're getting pointers and references mixed up. They're two different things, and while there are many problems that can be solved with either, they work in quite different ways. I won't go into the detail of explaining the difference and which to use when, as there's plenty of documentation already out there on this subject (for instance, take a look at this SO question).

To get you started with the particular problem you've posted though, @Lanting's answer is a good one.

Community
  • 1
  • 1
user31601
  • 2,482
  • 1
  • 12
  • 22
  • This should have been a comment, IMHO – Federico klez Culloca May 15 '17 at 09:35
  • The first way to do it, I've figured out before, but I am interested in the second way(to have the loop inside the function). His second code, which I am interested in, won't compile. I tried it the same way. – Atjov Šoraj May 15 '17 at 09:36
  • @Federico It was going to be a full answer, but as I was writing I noticed Lanting got in there before me, and was saying exactly what I was going to say. You're right - I could have downgraded to a comment. – user31601 May 15 '17 at 09:38