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:
Here is my data file: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; }
And this is my output:Code:2 Jk Rowling Harry 167 Fantasy Scholastic 1999 JRR Tolkien LOTR 999 Fantasy Penguin 1956
JkBut 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".Code:Rowling Harry 167 Fantasy Scholastic 1999 JRR Tolkien LOTR 999 Fantasy Penguin 1956
I've tried using getline(), because that's the only thing I can think of to do that.
I tried this:
With this data file: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; }
But it gives me crazy output, like this:Code:2 Jk Rowling Harry Potter 167 Science Fiction/Fantasy Scholastic 1999 JRR Tolkien Lord of the Rings 999 Science Fiction Penguin Classics 1956
So if someone can explain what I'm doing wrong, I'd be quite grateful.Code:Jk Rowling 211400 211400 205920 205936



LinkBack URL
About LinkBacks



