Filling Arrays from File

I have filled arrays before when i am dealing with numeric values but i have a book.txt document with book titles, authors, publishing, year, and price. I don't know how to fill the array with this information. If anyone could give me a hint or template that would really help.

book.txt:
Great Expectations
Charles Dickens
Simon & Brown
2011
10.95

A Tale of Two Cities
Charles Dickens
Simon & Brown
2011
11.94

A Christmas Carol
Charles Dickens
Tribeca Books
2010
6.66

Nicholas Nickleby
Charles Dickens
General Books LLC
2010
19.44

Oliver Twist
Charles Dickens
Collectors Library
2010
9.95

David Copperfield
Charles Dickens
Digireads.com
2008
14.39

The Ultimate Hitchhikers Guide to the Galaxy
Douglas Adams
Del Rey
2002
11.28

Dirk Gently's Holistic Detective Agency
Douglas Adams
Pocket Books
1991
7.99

The Restaurant at the End of the Universe
Douglas Adams
Del Rey
2005
11.20

The Hitchhiker's Guide to the Galaxy
Douglas Adams
Crown
2004
23.10

The Long Dark Tea-Time of the Soul
Douglas Adams
Pocket Books
1991
7.99

Life, the Universe and Everything
Douglas Adams
Del Rey
2005
11.76

So Long, and Thanks for All the Fish
Douglas Adams
Del Rey
2005
11.90
Last edited on
use a structure or a class, then add the above to an array of that type.
This is what i have so far but i don't think my main function is correct.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//______________Book Class______________
class Bookclass
{
private:
char title[50];
char author[50];
char publisher[50];
int year;
float price;

public:
//bool FillArraysFromFile(const string Filename);

char getTitle() const;
char getAuthor() const;
char getPublisher() const;
int getYear() const;
float getPrice() const;

void setTitle(string title);
void setAuthor(string author);
void setPublisher(string publisher);
void setYear(int year);
void setPrice(float price);
};




int main ()
{
ifstream file("book.txt");
if(file.is_open())
{
string myArray[4];
for(int i = 0; i < 4; ++i)
{
book >> myArray[i];
}
}


return 0;
}
string myArray[13][5];
what you have is a Multidimensional array.
http://www.cplusplus.com/doc/tutorial/arrays/

Using a 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>

using namespace std;
class bookData {
private:
    string title;
    string author;
    string publisher;
    int year;
    double price;
public:
    // default constructor
    bookData() { };

    // using constructor to set data;
    bookData(const string &title, const string &author, const string &publisher, int year, double price) : title (
            title), author (author), publisher (publisher), year (year), price (price) { }

    // or use setters 
    void set_title(const string &title) {
        bookData::title = title;
    }

    void set_author(const string &author) {
        bookData::author = author;
    }

    void set_publisher(const string &publisher) {
        bookData::publisher = publisher;
    }

    void set_year(int year) {
        bookData::year = year;
    }

    void set_price(double price) {
        bookData::price = price;
    }

    const string &get_title() const {
        return title;
    }

    const string &get_author() const {
        return author;
    }

    const string &get_publisher() const {
        return publisher;
    }

    int get_year() const {
        return year;
    }

    double get_price() const {
        return price;
    }
};

int main() {

    // making a book with constuctor 
    bookData aBook ("a title", "a author", "a publisher", 2001, 5.5);

    // making an array of books;
    bookData aBunchOfBooks[100];
    // setting the title of one book in the 0th place in array 
    aBunchOfBooks[0].set_author ("some author");
    return 0;
}



using a structure (pubic 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
#include <iostream>

struct bookData {
    string title;
    string author;
    string publisher;
    int year;
    float price;

};

using namespace std;

int main() {
    bookData aBunchOfBooks[100];
    aBunchOfBooks[0].title = "foo bar";
    aBunchOfBooks[0].author = "some author";
    aBunchOfBooks[0].publisher = "some publisher";
    aBunchOfBooks[0].year = 2000;
    aBunchOfBooks[0].price = 5.5;

    // ... etc

    return 0;
}



Does that help to get you started?

How do I get the information from my text file into the program? I don't want to define each one of my books in the main.
Topic archived. No new replies allowed.