Passing data to a class and storing it in a struct array

Hello. I have been working on this code for weeks and somehow i still can't understand how to operate a class. So what i am trying to do is get data on books store them in a struct array and write it to a file. i also want to read from the file store it in the strut array and display a nice table but that's for later when i slove my fist problem. the program is being compiled but i don't know if it's doing what i want it to do. Please help me.
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  #include <iostream>
#include <windows.h>
#include <iomanip>
#include <fstream>

using namespace std;

//constant definitions
const char FILE_NAME[] = "BooksLibrary.txt";
const int ARRAY_SIZE = 100;

class Books
{

    public:
     //default constructor
    Books();
    Books(string, string, string, string);

     //Mutators
    void setData(string, string, string, string);
    void writeFile();


    //Accessors
    void getData() const;
    void printData() const;


     void readfile();

private:
    struct BookData
    {
        string title;
        string authur;
        string publisher;
        string isbn;
    }data[ARRAY_SIZE];


};





int main()
{
    //variables definition
    char option = ' ';
    string title = "";
    string authur = "";
    string publisher = "";
    string isbn = "";

    //class object
    //Books books;

    do {
    system("cls");
    cout << "\n ==========================================================";
    cout << "\n                           Library                         ";
    cout << "\n ==========================================================";
    cout << "\n [1] New Entry ";
    cout << "\n [2] View Listing";
    cout << "\n [3] Exit";
    cout << "\n ==========================================================";
    cout << "\n Please input your selection (1,2,3): ";
    cin >> option;

    switch (option)
    {
        case '1':
            {

                system("cls");
                cout << "\n ==========================================================";
                cout << "\n                         New Entry                         ";
                cout << "\n ==========================================================" << endl;


                cout << "\n Please enter the title of the book: ";
                cin.get();
                getline(cin, title);

                cout << "\n Please enter the author of the book: ";
                getline(cin, authur);

                cout << "\n please enter the publisher of the book: ";
                getline(cin, publisher);

                cout << "\n Please enter the ISBN number of the book: ";
                getline(cin, isbn);

                //takes the data the use input and stores it in struct array
                Books libary(title, authur, publisher, isbn);

             system("cls");

               cout << "\n ===================================";
               cout << "\n              Thank You!            ";
               cout << "\n ===================================" << endl;
               cout << "\n Your Book has been added!" << endl;

               system("pause");
            }//end of case 1

            break;

        case '2':
            //books.setData();
            system("pause");
            break;

        case '3':
            break;

        default:
            cout << "\n\a ERROR INVALID ENTRY PLEASE TRY AGAIN!!!" << endl;
            system("pause");
            cout << endl;
            break;
    }//end of switch statement

    }while(option!= '3');//end of do-while


return 0;
}

//function definitions

Books::Books()
{
     for (int x = 0; x < ARRAY_SIZE ; x++)
    {
      Books::data[x].title = "";
      Books::data[x].authur = "";
      Books::data[x].publisher = "";
      Books::data[x].isbn = "";

    }//end of for loop
}//end of default constructor

Books::Books(string t, string a, string p, string i)
{
    setData(t, a, p, i);
}

void Books::setData(string t, string a, string p, string i)
{
    for (int x = 0; x < ARRAY_SIZE; x++)
    {
        if(data[x].title == " ")
        {
             data[x].title = t;
        }

        if(data[x].authur == " ")
        {
            data[x].authur = a;
        }

        if(data[x].publisher == " ")
        {
            data[x].publisher = p;
        }

        if(data[x].isbn == " ")
        {
            data[x].isbn = i;
            break;
        }
 }
}//end of set data


void Books::writeFile()
{
    fstream fileObject;//file stream variable

    fileObject.open(FILE_NAME, ios::app);//open file to write to it

    if(!fileObject)//check to see if the file is open.
    {
        cout << "\n\a Error File Cannot Open!"
             << "\n Terminating Program";
             exit(1);
    }
    else
    {
        for (int x = 0; x < ARRAY_SIZE; x++)
        {
            fileObject << left
             << data[x].title << " "
             << data[x].authur << " "
             << data[x].publisher << " "
             << data[x].isbn << " " << endl;
        }

    }//end of if else

    fileObject.close();
}//end of write file function 
Hello CrixStixs,

I will have to load the program and test it to see how it is working.

What did jump out at me is line 183. Because you defined the "fileObject as a "fstream" you will have to tell the open statement to open the stream as output, i.e., fileObject.open(FILE_NAME, std::ios::out | ios::app);.

Andy
oh okay but if i use the ios::out it overwrites the file and i don't want it do to that.
Hello CrixStixs,

if i use the ios::out it overwrites the file

I could be wrong, but I thought that "ios::trunc" is what clears the file.

It has been my understanding that you have to tell it to open the stream either input. output or both where as "ofstream" opens an output file and "ifstream" opens an input both by default.

This may be of some help: http://www.cplusplus.com/reference/fstream/fstream/open/

Just getting started, but this is what I am thinking of for now.

The struct should be defined outside the class and in the private section of the class I would put
BookData data[ARRAY_SIZE];. My approach would be to put the variables of the struct in the private section and in main define something like Books books[ARRAY_SIZE];. This way each element of the array would be a complete class of information about only one book and it would be easier to keep track of. If you know about vectors I would use a vector over an array. This way you only store what you need and have nothing extra. Just a thought.

For now I would concentrate on just collecting the data and storing it in the program. Writing to a file will become easier later. Also reading from a file.

For now I would keep the class simple until you have a better understanding of how classes work.

For now it is late and I am loosing focus. I will do better in the morning.

Hope that helps,

Andy
Oh okay i see what you are talking about putting the struct outside of the class however my teacher does not want it like that he wants everything inside of the class, which is such a pain.

I never used vectors before though so i will have to look into it.

I have another question, for where i had called the class in Int main, did i do it correctly? because some how no data is still being passed trough it. if i can solve that i believe i can get everything else complete in a flash.

I hope you are feeling better this morning. I really could use some more help.

Thank You.
closed account (E0p9LyTq)
ios:app is a flag telling the compiler what to do when writing to a file. Write the data at the end, adding it to the already existing data. Not needed to add ios:out when using ios::app for writing, but recommended.

http://en.cppreference.com/w/cpp/io/ios_base/openmode
ios::app seek to the end of stream before each write
Last edited on
oh okay, so the ios::app is best for what i am doing.
Hello CrixStixs,

Yes, but since you defined fileObject as an "fstream" you will also need "ios::out" as in: fileObject.open(FILE_NAME, iso:out | ios::app);. Otherwise when the file is opened it will not know if the stream is for input or output and I do not believe there is any default for "fstream" as there is with "ifstream" or "ofstream".

Hope that helps,

Andy
closed account (E0p9LyTq)
since you defined fileObject as an "fstream" you will also need "ios::out" as in: fileObject.open(FILE_NAME, iso:out | ios::app);

Not necessarily.

https://stackoverflow.com/questions/12217202/c-iosapp-doesnt-need-iosout-in-fstream

ios:app tells the compiler to append data on a write, implying the file is opened for output (ios:out).
http://en.cppreference.com/w/cpp/io/ios_base/openmode
closed account (E0p9LyTq)
Best way to make sure your stream is for output only, use std::ofstream.

Only for input, use std::ifstream.

Rarely, if ever, should a file need to be opened for simultaneous reading and writing. Don't use std::fstream.
oh okay thanks so much i believe i am getting it now
Topic archived. No new replies allowed.