Classes program

This is a discussion on Classes program within the C++ Programming forums, part of the General Programming Boards category; Hi, I am doing a assignment in which I have to create classes to store information about my books. However ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    17

    Question 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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    17
    Thanks I fixed the first part by using getline() but how would I convert the strings read to ints for year and pagenums?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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>>.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need the code of few programs in c++.plzzzzz help...plzzz
    By NAVINKR20 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2009, 09:13 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21