help inputting a text file into an array

I have been struggling with this for a while now. My assignment requires me to take information from a text file and input it into an array. The text file given to me is called "books.txt". The format of the text file is ID Title Author Category NumberofCopies. It looks something like this


9 Mega_Math Bob_Smith Math 6




I also have to make a class which in this case is called Book and separate it into a header and .cpp file.


this is my header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef BOOK_H
#define BOOK_H
#include <iostream>
#include <string>

using namespace std;


class Book
{
    public:
    void Books(int, string, string, string, int);

    private:
};





This is my .cpp file for my book class

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "Book.h"
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;




void Book::Books(int ID, string title, string author, string category, int copies)
{

        ifstream booksIn;
        booksIn.open("books.txt");         // Opening the books text file

            if(!booksIn)
            {
                cout << "Error" << endl;  // Displays error message if theres a problem
                exit(1);
            }


int i = 0;


            while (!booksIn.eof())
            {


            int ID;
            string title;
            string author;              
            string category;
            int copies;


            booksIn >> ID;
            booksIn >> title;
            booksIn >> author;          //Here I am inputting each token into the appropriate variable that I made.
            booksIn >> category;
            booksIn >> copies;





            Book bk (ID, title, author, category, copies)   // This is the part where i'm having a hard time implementing. 
//I have an idea of what I want to do but I just dont know how to put it into code. 
//I want to make it so that "bk" holds a line of information from the text file and is added into a array
            
            bookArray[i++] = bk;


            }


}


Sorry for the long code. Any help will be greatly appreciated.
Where are your class member variables?

You're trying to do way too much in the constructor. The constructor should never, IMO, try to do things like opening and reading a file. Why are you passing values into the constructor that you never use?

Start simpler. IMO you need to restudy your class notes and text book on how to create and use a class. This link may also be helpful.

http://www.cplusplus.com/doc/tutorial/classes/
Yeah everything is being done in the constructor which is not supposed to be so! Use member variables.

Aceix.
I hope you can learn something from this, and you find the code reusable for other projects.

Book.h

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
#ifndef BOOK_H
#define BOOK_H

#include <iostream>
#include <string>

class Book 	// Create a Book object
{
private: 	// Variables the book has
	int ID, copies;
	std::string title, author, category;
public:
	Book()  {} // Constructor
	~Book() {} // Deconstructor

	// These functions are very handy
	// Allows you to write code such as std::cout << Book; and std::cin >> Book;
	// Rather than that tedious Book.GetId() ... Book.N();
	// Though we use std::ifstream to read it from the file

	friend std::istream& operator>>(std::istream& is, Book& b);
	friend std::ostream& operator<<(std::ostream& os, Book& b);

	// The way it works is just like any normal functions with parameters
	// The arguments are a stream such as std::cin or std::cout
	// And an object you want the information to go to, such as our Book
};

#endif 


Book.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Book.h"

std::istream& operator>>(std::istream& is, Book& b)
{
	// Now just like normal we extract the contents of the stream into our variables
	// Pretty handy huh?
	is >> b.ID >> b.title >> b.author >> b.category >> b.copies;

	// Remember we must return the stream once we are done with it to be updated
	// Such as internal variables like the failbit
	return is;
}
std::ostream& operator<<(std::ostream& os, Book& b)
{
	os << "ID: " << b.ID << "\tTitle: " << b.title << "\tAuthor: " << b.author << "\tCategory: " << b.category << "\tCopies: " << b.copies;
	return os;
}


main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <fstream>

#include "Book.h"

int main()
{
	// Now the file can read the contents directly into our object, the book
	std::ifstream ifs("books.txt");
	Book b;
	ifs >> b;
	std::cout << b << "\n";
	return 0;
}


EDIT: Typo EDIT 2: Didn't include Book.h
Last edited on
Topic archived. No new replies allowed.