Array of files problem

i am currently studying files i c++. Can someone tell me where is the problem in this simple code.
#include<iostream>
using namespace std;
struct library
{
char title[100];
char autor[100];
}book[100];
int main()
{
cin.get(book[].title,100);
int number_of_books=0;
while(book[number_of_books].title!='\0')number_of_books++;
cout<<number_of_books;
return 0;
}

Last edited on
Take a tutorial on arrays. The one on this site is decent
http://www.cplusplus.com/doc/tutorial/arrays/
1. What does this code have to do with files?

2. Please edit you post. Highlight your code and click the "<>" format button. This will add code tags making it easier to read and comment on your code.

3. cin.get(book[].title,100); You did not give an index to the book array, so the compiler does not know which book's title to populate.

4. while(book[number_of_books].title!='\0')number_of_books++; This is where the tutorial on arrays would be helpful. The .title field is an array of characters, so it cannot be equal to '\0', which is a single character. You probably meant while(book[number_of_books].title[0]!='\0'). However, because you never initialized anything in your book array, the title fields can have any content to start. You might not find any book[x].title[0] that is '\0'. This would cause you to continue incrementing number_of_books, and in turn accessing memory beyond the end of your array This is a bad thing.

There are probably other problems with the code, but you need to get the basics straightened out before looking deeper.
Topic archived. No new replies allowed.