0

I have a piece of code I'm running in Cygwin in C++ I'm compiling using

g++ -o program program.cpp

And it is returning an error that reads 'Aborted (core dumped)'. It is intended to take a file name as input through a command line argument, count all the unique words and total words in the file, and prompts the user to input a word and counts how many times the word that they input occurs. It's only intended to use C++ streams for input/output.

    #include <fstream>
    #include <iostream>
    #include <string>
    #include <cctype>
    using namespace std; 
    int main(int argc, char *argv[])
    {
        string filename;
        for( int i = 1; i < argc; i++){
            filename+=argv[i];
        }
        ifstream file;
        file.open(filename.c_str());
        if (!file)
        {
            std::cerr << "Error: Cannot open file" << filename << std::endl;
            return 1;
        }
        string* words;
        int* wordCount;
        int wordLength = 0;
        string curWord = "";
        bool isWord;
        int total = 0;
        char curChar;
        string input;
        while(!file.eof())
        {         
            file.get(curChar);
            if (isalnum(curChar)) {
                curWord+=tolower(curChar);
            }
            else if (!curWord.empty() && curChar==' ')
            {
                isWord = false;
                for (int i = 0; i < wordLength; i++) {
                    if (words[i]==curWord) {
                        wordCount[i]++;
                        isWord = true;
                        total++;
                    }
                }
                if (!isWord) {
                    words[wordLength]=curWord;
                    wordLength++;
                    total++;
                }
                curWord="";
            }
        }
        file.close();
        // end
        cout << "The number of words found in the file was " << total << endl;
        cout << "The number of unique words found in the file was " << wordLength << endl;
        cout << "Please enter a word: " << endl;
        cin >> input;
        while (input!="C^") {
            for (int i = 0; i < wordLength; i++) {
                if (words[i]==input) { 
                    cout << wordCount[i];
                }
            }
        }
    }
  • 2
    When you used the debugger, which statement was the last one executed before the abort? You did use a debugger before posting, didn't you? – Thomas Matthews Sep 05 '15 at 00:07
  • 1
    Maybe unrelated, but see http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – Barmar Sep 05 '15 at 00:07
  • I'm not sure what you mean; is there a debugger for C++ built into cygwin? Before I posted, I went over the code line-by-line to check for errors. – Zayne Excalibur Sep 05 '15 at 00:09
  • 1
    Stop using pointers, `words` and `wordCount` are uninitialized pointers and every place you read/write through them is an error. If you want a dynamic array use `std::vector` or another standard library container. – Blastfurnace Sep 05 '15 at 00:13
  • @ZayneExcalibur I don't use cygwin, but I'll bet `gdb` is available, or easy to install. – Barmar Sep 05 '15 at 00:16
  • It's available: https://cygwin.com/cygwin-ug-net/gdb.html – Barmar Sep 05 '15 at 00:17
  • And even if a debugger can't be used for some reason (including that one just doesn't know how to use them), some debug prints can go a long way to at least narrowing down where the problem is. – Michael Burr Sep 05 '15 at 00:38

1 Answers1

3

You never allocated any space for words and wordCount to point to. It should be:

#define MAXWORDS 1000
string *words = new string[MAXWORDS];
int *wordCount = new int[MAXWORDS];

and then at the end of the program you should do:

delete[] wordCount;
delete[] words;

or you can allocate a local array:

string words[MAXWORDS];
int wordCount[MAXWORDS];

But you could do it more simply by using std::map to map the string to a count. This will automatically grow as needed.

Barmar
  • 741,623
  • 53
  • 500
  • 612