-
Classes program
Hi, I am doing a assignment in which I have to create classes to store information about my books. However it is not working because I am getting crazy output and I don't know what's wrong. I keep on trying different things but I cannot get it to work. I'd appreciate it if someone could look at it. I think the problem may lie in the data file, but I'm not sure. Here is my program:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int SIZE = 15;
ifstream file_in;
ofstream file_out;
class cover {
public:
string title;
string first;
string last;
};
class inside {
public:
string genre;
string publisher;
int year;
int pagenums;
};
class book {
public:
cover coverinfo;
inside insideinfo;
};
void readdata (book [], int &);
void printdata (book [], int);
int main ()
{
int n;
book library [SIZE];
file_in.open ("program8in.dat");
file_out.open ("program8.out");
readdata (library, n);
printdata (library, n);
return 0;
}
void readdata (book library [], int &n)
{
file_in >> n;
for (int count = 0; count < n; count++) {
file_in >> library[count].coverinfo.title;
file_in >> library[count].coverinfo.first;
file_in >> library[count].coverinfo.last;
file_in >> library[count].insideinfo.genre;
file_in >> library[count].insideinfo.publisher;
file_in >> library[count].insideinfo.year;
file_in >> library[count].insideinfo.pagenums;
}
return;
}
void printdata (book library [], int n)
{
for (int count= 0; count < n; count++) {
file_out << "\t" << library[count].coverinfo.title;
file_out << "\t" << library[count].coverinfo.first;
file_out << "\t" << library[count].coverinfo.last;
file_out << "\t" << library[count].insideinfo.genre;
file_out << "\t" << library[count].insideinfo.publisher;
file_out << "\t" <<library[count].insideinfo.year;
file_out << "\t" << library[count].insideinfo.pagenums;
}
return;
}
This is my data file:
Code:
7
Harry Potter
JK
Rowling
SciFi
Scholastic
2003
870
And this is my output:
Code:
Harry Potter JK Rowling SciFi 205936 196984 196984 196984 27 512 152 -2141497075 -445879994 2358840 198376 2089878893 211408 211408
I'm sorry about the length, I know it's rather long but I'm stuck.
-
To be honest, all you are using are C-style structs, not OO classes.
What is happening is that you read in 7 and then loop 7 times to read in data. However, your data file only has one item which spans 7 lines. Not only that, but you are reading in using formatted input, i.e., you read in until the first whitespace. Rather, you want to read line by line, possibly by using std::getline(). Finally, year and pagenums are ints, so you need to convert the strings read to ints before storing them (or use formatted input to read directly to the ints, but you have to be careful to discard newlines when mixing formatted input with getline).
-
Thanks I fixed the first part by using getline() but how would I convert the strings read to ints for year and pagenums?
-
-
Another solution is to simply use getline for the lines that may contain a space, then use operator>> for the lines that have integers. The only caveat is that when mixing operator>> and getline, you have to add a file_in.ignore() after the calls to operator>> to ignore trailing newlines. The benefit is that you don't have to take the extra step of converting strings to integers, it is done automatically by operator>>.