Header file:
#include <string>
using namespace std;
struct word {
int count;
std::string w;
};
class WordAnalysis{
private:
int timesDoubled;
word *words;
int wordCount;
int index;
void doubleArrayAndAdd(std::string);
bool checkIfCommonWord(std::string);
void sortData();
public:
bool readDataFile(char*); //returns an error if file not opened
int getWordCount();
int getUniqueWordCount();
int getArrayDoubling();
void printCommonWords(int);
WordAnalysis(int);
~WordAnalysis();
};
Source file:
#include <iostream>
#include <fstream>
#include <sstream>
#include "WordAnalysis.h"
using namespace std;
void WordAnalysis:: doubleArrayAndAdd(std::string newWord){//double array length and add word
bool checkCommon = checkIfCommonWord(newWord);
int arrchecker=0;
if(wordCount-1==index){//doubler
timesDoubled++;
wordCount = wordCount*2;
word *list2 = new word [wordCount];
for(int k=0;k<wordCount-1;k++){
list2[k].count=0;
list2[k].w="0";
}
for(int i=0;i<wordCount/2;i++){ //allocate old array to newly created f2 which was formerly pointing torwards the 4 array
list2[i] = words[i];
}
delete []words;
words = list2;
}
for(int j=0;j<wordCount;j++){//wordadder
if(newWord==words[j].w){
words[j].count++;
j=wordCount;
arrchecker=1;
}
}
if(arrchecker==1){
for(int i=0;i<wordCount-1;i++){
if(words[i].w=="0"){
words[i].w=newWord;
words[i].count++;
}
}
index++;
}
}
bool WordAnalysis:: checkIfCommonWord(std::string word){
bool check = false;
string commonWords[]={"the", "you", "one", "be", "do", "all", "to", "at", "would", "of", "this", "there", "and", "but", "their", "a", "his", "what", "in", "by", "so", "that", "from", "up", "have", "they", "out", "i", "we", "if", "it", "say", "about", "for", "her", "who", "not", "she", "get", "on", "or", "which", "with", "an", "go", "he", "will", "me", "as", "my"};
for(int i=0;i<51;i++){
if(word==commonWords[i]){
return true;
}
}
if(check == false){
return false;
}
}
void WordAnalysis:: sortData(){
word temp [1];
for(int i=0;i<index-1;i++){
for(int j=0;j<=index-i;j++){
if(words[j].count > words[j+1].count){
temp[0].count = words[j].count;
temp[0].w = words[j].w;
words[j].w=words[j+1].w;
words[j].count=words[j+1].count;
words[j+1].w=temp[0].w;
words[j+1].count=temp[0].count;
}
}
}
}
bool WordAnalysis:: readDataFile(char* filename){ //this opens and reads in file. returns false if error, argument is file name
ifstream data(filename);
if (data.is_open()){
}
else{
return false;
}
string newWord;
while(getline(data, newWord, '\n')){
stringstream ss;
ss<<newWord;
while(getline(ss,newWord,' ')){
doubleArrayAndAdd(newWord);
}
}
}
int WordAnalysis:: getWordCount(){//total non common words by summing counts from array words
int sum=0;
for(int i=0;i<index;i++){
sum+=words[i].count;
}
cout<<"Total non-common words: "<<sum<<endl;
}
int WordAnalysis:: getUniqueWordCount(){
cout<<"Unique non-common words: "<<index<<endl;
}
int WordAnalysis:: getArrayDoubling(){
cout<<"Array doubled: "<<timesDoubled<<endl;
}
void WordAnalysis:: printCommonWords(int amount){
for(int i=index+1;i>index-amount+1;i--){
cout<<words[i].count<<" - "<<words[i].w<<endl;
}
}
WordAnalysis:: WordAnalysis(int){
for(int k=0;k<wordCount-1;k++){
words[k].count=0;
words[k].w="0";
}
}
WordAnalysis:: ~WordAnalysis(){
}
The error is probably somewhere in source file with WordAnalysis:: WordAnalysis or something. I've looked through this website and while this is a common error, the errors are different from what I am having it seems.
We are not allowed to change the header file at all. But can change anything in source.
A few points (edited). The integer in WordAnalysis(int) is a number for the size of the array of structs declared in the header file. I didn't know where to create it but I thought that would be the best place with the int there.
It is also probably a quick error.