-1

the program should read from 2 files (author.dat and citation.dat) and save them into a map and set; first it reads the citationlist without problem, then it seems to properly read the authors and after it went through the whole list (author.dat) a floating point exception arises .. can't quite figure out why

seems to happen in author.cpp inside the constructor for authorlist

author.cpp:

#include <fstream>
#include <iostream>
#include "authors.h"

using namespace std;

AuthorList::AuthorList(char *fileName) {
    ifstream s (fileName);
    int idTemp;
    int nrTemp;
    string nameTemp;

    try {
        while (true){
            s >> idTemp >> nrTemp >> nameTemp;
            cout << idTemp << " " << nrTemp << " " << nameTemp << " test_string";
            authors.insert(std::make_pair(idTemp,Author(idTemp,nrTemp,nameTemp)));
            if (!s){
                cout << "IF-CLAUSE";
                throw EOFException();
            }
             cout << "WHILE-LOOP_END" << endl;
        }
    } catch (EOFException){}
}

author.h:

#ifndef CPP_AUTHORS_H
#define CPP_AUTHORS_H

#include <iostream>
#include <map>
#include <string>
#include "citations.h"

class Author {
public:
    Author (int id, int nr, std::string name) :
            articleID(id),
            authorNR(nr),
            authorName(name){}

    int getArticleID() const {
        return articleID;
    }

    std::string getAuthorName() const {
        return authorName;
    }

private:
    int articleID;
    int authorNR;
    std::string authorName;
};

class AuthorList {
public:
    AuthorList(char *fileName);

    std::pair<std::multimap<int,Author>::const_iterator, std::multimap<int,Author>::const_iterator> findAuthors(int articleID) {
        return authors.equal_range(articleID);
    }

private:
    std::multimap<int,Author> authors;
};

#endif //CPP_AUTHORS_H

programm.cpp:

#include <iostream>
#include <cstdlib>
#include "citations.h"
#include "authors.h"
#include "authorCitation.h"

using namespace std;

int main(int argc, char *argv[]){
    CitationList *cl;
    AuthorList *al;

    //check if argv array has its supposed length
    if (argc != 4){
        cerr << "usage: programm article.dat citation.dat author.dat";
        return EXIT_FAILURE;
    }

    //inserting citation.dat and author.dat in corresponding lists (article.dat not used)
    cl = new CitationList(argv[2]);
    al = new AuthorList(argv[3]);
    try {
        AuthorCitationList *acl;
        acl->createAuthorCitationList(al,cl);
        acl->printAuthorCitationList2File("authorcitation.dat");
    } catch (EOFException){
        cerr << "something went wrong while writing to file";
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

All files: https://drive.google.com/file/d/0B734gx5Q_mVAV0xWRG1KX0JuYW8/view?usp=sharing

frost
  • 13
  • 1

1 Answers1

0

I am willing to bet that the problem is caused by the following lines of code:

AuthorCitationList *acl;
acl->createAuthorCitationList(al,cl);

You are calling a member function using an uninitialized pointer. I suggest changing the first line to:

AuthorCitationList *acl = new AuthorCitationList; 

Add any necessary arguments to the constructor.

While you are at it, change the loop for reading the data also. You have:

while (true){
    s >> idTemp >> nrTemp >> nameTemp;
    cout << idTemp << " " << nrTemp << " " << nameTemp << " test_string";
    authors.insert(std::make_pair(idTemp,Author(idTemp,nrTemp,nameTemp)));
    if (!s){
        cout << "IF-CLAUSE";
        throw EOFException();
    }
    cout << "WHILE-LOOP_END" << endl;
}

When you do that, you end up adding data once after the end of line has been reached. Also, you seem to have the last line in the wrong place. It seems to me that it should be outside the while loop.

You can use:

while (true){
    s >> idTemp >> nrTemp >> nameTemp;

    // Break out of the loop when reading the
    // data is not successful.
    if (!s){
        cout << "IF-CLAUSE";
        throw EOFException();
    }
    cout << idTemp << " " << nrTemp << " " << nameTemp << " test_string";
    authors.insert(std::make_pair(idTemp,Author(idTemp,nrTemp,nameTemp)));
}
cout << "WHILE-LOOP_END" << endl;

You can simplify it further by using:

while (s >> idTemp >> nrTemp >> nameTemp){
   cout << idTemp << " " << nrTemp << " " << nameTemp << " test_string";
   authors.insert(std::make_pair(idTemp,Author(idTemp,nrTemp,nameTemp)));
}
cout << "WHILE-LOOP_END" << endl;
R Sahu
  • 204,454
  • 14
  • 159
  • 270