In my program I need to read in a file, search for 100 unique words, and count how many times a found word has been found all by using an array. This is what I have so far, but when the file is found it simply returns zero.
please any help would be great
[CODE=cpp]#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int LIST_SIZE = 100;
struct WordInfo
{
string theWord;
int multi;
};
bool openFile(ifstre am &inf)
{
string fileName;
cout << "Please enter the file: ";
cin >> fileName;
inf.open(fileNa me.c_str());
if (!inf.is_open() )
{
cerr << "File couldnt be found ";
cout << endl;
exit(-1);
return false;
}
else
return true;
};
bool readWord(ifstre am &inf, string &word)
{
if (inf >> word)
{
return true;
}
return false;
};
int lSearch(WordInf o list[], int items, string key)
{
int idx;
for (idx=0; idx < items && list[idx].theWord !=key; idx++);
if (idx==items)
return (-1);
return(idx);
};
bool readWords(WordI nfo wordList[], int &numWords)
{
ifstream inf;
if (!inf.is_open() ) // openFile returns false if file not opened
return(false);
numWords=0;
string tempWord;
while (numWords<LIST_ SIZE && readWord(inf,te mpWord))
{
// readWord returns true if a word is read, false if end of file
int idx=lSearch(wor dList, numWords, tempWord);
// check idx and either place tempWord into the array at the next
// open slot or use idx to increment the found word's count
// (don't forget to increment numWords, too).
// IF TEMPWORD=FOUND, ++ WORD COUNT, IF TEMPWORD=NEWWOR D, ADD TO ARRAY IN NEXT SPOT
}
return true;
};
void sortText(WordIn fo list[], int items)
{
int spot;
for(spot =items-1; spot>0;spot--)
{
int idxMax=spot;
for (int idx=0; idx<spot; spot++)
if(list[idx].theWord>list[idxMax].theWord)
idxMax = idx;
if (idxMax!=spot)
swap(list[idxMax],list[spot]);
}
};
int main()
{
WordInfo arr[LIST_SIZE];
ifstream inf;
int x = 0;
string word;
openFile(inf);
cout << readWord(inf, word) << endl;
lSearch(arr, x, word);
readWords(arr,x );
return 0;
}[/CODE]
please any help would be great
[CODE=cpp]#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int LIST_SIZE = 100;
struct WordInfo
{
string theWord;
int multi;
};
bool openFile(ifstre am &inf)
{
string fileName;
cout << "Please enter the file: ";
cin >> fileName;
inf.open(fileNa me.c_str());
if (!inf.is_open() )
{
cerr << "File couldnt be found ";
cout << endl;
exit(-1);
return false;
}
else
return true;
};
bool readWord(ifstre am &inf, string &word)
{
if (inf >> word)
{
return true;
}
return false;
};
int lSearch(WordInf o list[], int items, string key)
{
int idx;
for (idx=0; idx < items && list[idx].theWord !=key; idx++);
if (idx==items)
return (-1);
return(idx);
};
bool readWords(WordI nfo wordList[], int &numWords)
{
ifstream inf;
if (!inf.is_open() ) // openFile returns false if file not opened
return(false);
numWords=0;
string tempWord;
while (numWords<LIST_ SIZE && readWord(inf,te mpWord))
{
// readWord returns true if a word is read, false if end of file
int idx=lSearch(wor dList, numWords, tempWord);
// check idx and either place tempWord into the array at the next
// open slot or use idx to increment the found word's count
// (don't forget to increment numWords, too).
// IF TEMPWORD=FOUND, ++ WORD COUNT, IF TEMPWORD=NEWWOR D, ADD TO ARRAY IN NEXT SPOT
}
return true;
};
void sortText(WordIn fo list[], int items)
{
int spot;
for(spot =items-1; spot>0;spot--)
{
int idxMax=spot;
for (int idx=0; idx<spot; spot++)
if(list[idx].theWord>list[idxMax].theWord)
idxMax = idx;
if (idxMax!=spot)
swap(list[idxMax],list[spot]);
}
};
int main()
{
WordInfo arr[LIST_SIZE];
ifstream inf;
int x = 0;
string word;
openFile(inf);
cout << readWord(inf, word) << endl;
lSearch(arr, x, word);
readWords(arr,x );
return 0;
}[/CODE]
Comment