0

Hey everyone I've been having some trouble with this assignment. I need to:

Read each word of the input file. For each word:

  1. If the word already exists in your word inventory, then simply add 1 to the count. Do not add a duplicate word to your inventory.
  2. If the word does not exist in your word inventory, add it and set the count to 1.
  3. After reading all of the input file, close the input file.
  4. Sort the word inventory by ASCII word order
  5. Display the word inventory, i.e. list the words and their counts. The output should be sorted in ASCII word order.

My allowed library's are iostream, iomanip, string, cstring, cerrno, limits, sstream, fstream, cmath.

So far, I've been having difficulties with counting the words! My code counts the characters, not the words. My code so far is as follows:

#include "appl.h"
using namespace std;

/*
 Class definition file for Appl
*/
string getFileName(ios_base::open_mode parm);
struct wordBlock {
    string word;
    int count;
};

// Constructor
Appl::Appl()
{
    // id string is required for all CS 162 submissions.  *** DO NOT CHANGE ***
    _cs162_id_ = new string(__FILE__ + string(" compiled ")
                            + __DATE__ + string(" ") + __TIME__
                            + string(" using g++ ") + to_string(__GNUC__)
                            + string(".") + to_string(__GNUC_MINOR__) + string(".")
                            + to_string(__GNUC_PATCHLEVEL__));  
}

// Destructor
Appl::~Appl()
{
    delete _cs162_id_;
}

string inputFileName(ios_base::open_mode parm){
    fstream iFile;
    string inFileName = "";
    int count = 1;

    if(parm == ios::in){
        while(count != 0){
            cout << "Enter an input file name that exists: ";
            getline(cin,inFileName);
            iFile.open(inFileName.c_str() , ios::in);
            if(iFile.good() != true){
                cout << "?Invalid file name : file does not exist" <<                                   
                        count++;
                iFile.clear();
            }else{
                count = 0;
                iFile.close();
                return inFileName;
            }
        }
    }
}

// Main Routine
int Appl::main(int argc, char ** argv)
{
    fstream inFile;
    string inFileNames;

    inFileNames = inputFileName(ios::in);    
    inFile.open(inFileNames.c_str(), ios::in);

    wordBlock inventory[1000];

    if(inFile.is_open()){
        for(auto idx = 0; idx < 1000; idx ++){
            inventory[idx].word = idx;
            inventory[idx].count = 0;
        }

        while(inFile.peek() != EOF) {
            inventory[inFile.get()].count++;
        }
        for(auto idx = 0; idx < 1000; idx++){
            inventory[idx].word = idx;
            inventory[idx].count = 0;
        }

        while(inFile.peek() != EOF) {
            inventory[inFile.get()].count++;
        }
        for(auto idx = 0; idx < 1000; idx++){
            if(inventory[idx].count == 0) continue;
            cout << "Word " << inventory[idx].word << " occurs " << inventory[idx].count << " times" << endl;
        }
        inFile.clear();
        inFile.close();
    }
    return 0;
}
CJCombrink
  • 3,346
  • 18
  • 35
  • 1
    First of all you need to read [this `istream::get` reference](http://en.cppreference.com/w/cpp/io/basic_istream/get). Then you should also read ["Why is iostream::eof inside a loop condition considered wrong?"](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Then you need to rethink your design, I recommend you write it down somewhere first, including requirements, goals and purpose of the program, and think a little more on how to achieve the purpose of the program. – Some programmer dude Apr 15 '16 at 05:43
  • Related: I see nothing in your description about case-insensitivity regarding determination of "unique". you may want to reread the assignment and clarify that little detail, as it could make this a bit more complicated. Ex: the word "all" vs "All" are not "equivalent" as ASCII encodings, but are none-the-less dictionary equivalent. – WhozCraig Apr 15 '16 at 06:03
  • Are there any limitations on what you may or may not use? [std::map](http://en.cppreference.com/w/cpp/container/map) can do everything except get the input. – CJCombrink Apr 15 '16 at 06:06
  • Yeah there are limitations. These are my allowed library's: #include #include #include #include #include #include #include #include #include – Ben Portis Apr 15 '16 at 06:07
  • Thanks for all the help so far. Joachim Pileborg I see your point! I think my inFile.get() is incorrect for this context. – Ben Portis Apr 15 '16 at 06:12

2 Answers2

0

you might wanna use set from stl declare a set of string type

set <string> mySet;

bool myfunc(string word){
    pair<set<string>::iterator,bool>unique; 
/* use #include<utility>
    when a set insertion returns a pair of values, the second one is boolean type. and as set's element is always sorted and unique, it makes our life easy*/
unique = mySet.insert(word);

return unique.second;
}
shb
  • 4,694
  • 2
  • 13
  • 25
  • Thanks for your suggestion! Unfortunately I can't use any more library's than the one's provided. :'( – Ben Portis Apr 15 '16 at 06:34
  • If `` were allowed for the assignment, that would probably be considered, though `` would be a stronger choice. *Neither* are allowed. – WhozCraig Apr 15 '16 at 06:34
0

In pseudo code you need to do the following to get the counts:

  1. Create a counter to keep track of the number of words in your inventory, must be 0 at the start (lets call it wordCount).
  2. Read a single word from the file.
  3. run through all of your added words and see if the word is in the list
    • from 0 to wordCount
  4. Compare the newWord with the word at inventory[index]
    • If they match, increment the count at inventory[index] and stop loop
    • If they do not match, go to next one.
  5. If at end of wordCount and did not find it, set inventory[wordCount]to the word with 0 count and increment wordCount.
    • Since you have 0-based indexing, the word in your inventory at the current count should be empty, thus add it as the new word and increment the count.
  6. repeat until all words are read.

To sort the words, read up on a sorting algorithm, something like bubble sort should be easy enough to implement:

  • the operator< can be used: if(str1 < str2) {}
CJCombrink
  • 3,346
  • 18
  • 35