I'm doing an assignment where you have to create class structures. The problem I'm having is when you read in a string into the array. Here is my program so far:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int NUMCONS = 15;
class author_name {
public:
string last;
string first;
};
class other_info {
public:
string publisher;
int year;
};
class book_info {
public:
string title;
int pages;
string genre;
other_info other;
};
class mybooks {
public:
author_name name;
book_info book;
};
void readdata (mybooks [], int &);
void prettyprint (mybooks [], int);
int main ()
{
mybooks library[NUMCONS];
int num;
readdata (library, num);
prettyprint (library, num);
return 0;
}
void readdata (mybooks library [], int &num)
{
ifstream cfile("program8in.dat");
cfile >> num;
for (int count = 0; count < num; count++) {
cfile >> library[count].name.last;
cfile >> library[count].name.first;
cfile >> library[count].book.title;
cfile >> library[count].book.pages;
cfile >> library[count].book.genre;
cfile >> library[count].book.other.publisher;
cfile >> library[count].book.other.year;
}
cfile.close();
return;
}
void prettyprint (mybooks library [], int num)
{
ofstream dbfile("program8.out");
for (int count= 0; count < num; count++) {
dbfile << "\t" << library[count].name.last;
dbfile << "\t" << library[count].name.first;
dbfile <<"\t" << library[count].book.title;
dbfile <<"\t" << library[count].book.pages;
dbfile <<"\t" << library[count].book.genre;
dbfile <<"\t" << library[count].book.other.publisher;
dbfile <<"\t" << library[count].book.other.year;
dbfile << endl;
}
dbfile.close();
return;
}
Here is my data file:
Code:
2
Jk Rowling
Harry 167 Fantasy Scholastic 1999
JRR Tolkien
LOTR 999 Fantasy Penguin 1956
And this is my output:
Jk
Code:
Rowling Harry 167 Fantasy Scholastic 1999
JRR Tolkien LOTR 999 Fantasy Penguin 1956
But let's say I want the title to be "Harry Potter". The program will read in "Harry" into a different location in the array from "Potter".
I've tried using getline(), because that's the only thing I can think of to do that.
I tried this:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int NUMCONS = 15;
class author_name {
public:
string last;
string first;
};
class other_info {
public:
string publisher;
int year;
};
class book_info {
public:
string title;
int pages;
string genre;
other_info other;
};
class mybooks {
public:
author_name name;
book_info book;
};
void readdata (mybooks [], int &);
void prettyprint (mybooks [], int);
int main ()
{
mybooks library[NUMCONS];
int num;
readdata (library, num);
prettyprint (library, num);
return 0;
}
void readdata (mybooks library [], int &num)
{
ifstream cfile("program8in.dat");
cfile >> num;
for (int count = 0; count < num; count++) {
cfile >> library[count].name.last;
cfile >> library[count].name.first;
getline(cfile, library[count].book.title);
cfile >> library[count].book.pages;
getline(cfile, library[count].book.genre);
getline(cfile, library[count].book.other.publisher);
cfile >> library[count].book.other.year;
}
cfile.close();
return;
}
void prettyprint (mybooks library [], int num)
{
ofstream dbfile("program8.out");
for (int count= 0; count < num; count++) {
dbfile << "\t" << library[count].name.last;
dbfile << "\t" << library[count].name.first;
dbfile <<"\t" << library[count].book.title;
dbfile <<"\t" << library[count].book.pages;
dbfile <<"\t" << library[count].book.genre;
dbfile <<"\t" << library[count].book.other.publisher;
dbfile <<"\t" << library[count].book.other.year;
dbfile << endl;
}
dbfile.close();
return;
}
With this data file:
Code:
2
Jk Rowling
Harry Potter
167
Science Fiction/Fantasy
Scholastic
1999
JRR Tolkien
Lord of the Rings
999
Science Fiction
Penguin Classics
1956
But it gives me crazy output, like this:
Code:
Jk Rowling 211400 211400
205920 205936
So if someone can explain what I'm doing wrong, I'd be quite grateful.