-
December 3rd, 2008, 08:12 PM
#1
Determining the number of unique words in a .txt file
Hey I have to write a program that reads a text file that contains a list of words, 1 word per line. I have to store the unique words and count the occurrences of each unique word. When the file is completely read, I have to print the words and the number of occurrences to a text file. The output should be the words in alphabetical order along with the number of times they occur. Then print to the file some statistics:
I have to use character arrays instead of strings.
I must use the linear search (something that looks like this)
Code:
int search (int list [], int size, int key)
{
int pos = 0;
while (pos < size && list[pos] != key)
pos++;
if (pos == size)
pos = -1;
return pos;
}
to determine if a word is in the array. The array is an array of structures and that the key is a char array so the string comparison must be used. The search task should be a separate function.
The search must be a separate function that returns an integer values. I cant use a for loop and the function must have only one return statement.
I'm starting off by trying to read and store the words of the text file. My text book has code for reading line by line a text file and then displaying it. So I figured I should start with that. Heres the code-
Code:
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string>
using namespace std;
int main()
{
string filename = "text.dat"; // put the filename up front
string line;
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
// read and display the file's contents
while (getline(inFile,line))
cout << line << endl;
inFile.close();
cin.ignore(); // this line is optional
return 0;
}
Heres my code split into two functions, however it does not compile. I get an error at the while statement.-
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
void displayFile( char []);
void main ()
{
int const wordLength = 21;
int const Num = 101;
int const fileSize = 255;
char filename[fileSize];
cout << "Please enter the name of the file you wish to open: "<< endl;
cin.getline(filename,fileSize);
displayFile (filename);
cin.ignore();
}
void displayFile (char fileName[] )
{
ifstream inFile;
char line [101];
inFile.open(fileName);
while (getline(inFile, line))
cout << line << endl;
inFile.close();
return 0;
}#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
void displayFile( char []);
void main ()
{
int const wordLength = 21;
int const Num = 101;
int const fileSize = 255;
char filename[fileSize];
cout << "Please enter the name of the file you wish to open: "<< endl;
cin.getline(filename,fileSize);
displayFile (filename);
cin.ignore();
}
void displayFile (char fileName[] )
{
ifstream inFile;
char line [101];
inFile.open(fileName);
while (getline(inFile, line))
cout << line << endl;
inFile.close();
return 0;
}
My code is nearly identical except for the fact that I use character arrays instead of strings, so I'm not sure why its not compiling.
Also, is this a good way to start off the program? Or should I try something else.
-
December 3rd, 2008, 08:25 PM
#2
Re: Determining the number of unique words in a .txt file
getline(inFile, line) doesn't exist for character arrays----that's a special overload for std::strings. The one you want for a char array looks like inFile.getline(line,length). Reference here:
http://www.cplusplus.com/reference/i...m/getline.html
I have to use character arrays instead of strings.
I must use the linear search (something that looks like this)
Shame, without these restrictions you could write the program in about 10 lines using a std::map.
-
December 3rd, 2008, 08:52 PM
#3
Re: Determining the number of unique words in a .txt file
The simplest solution is to use a counter for each word.
The easiest way to implement this is by the use of an associate array (a map). You simply match,
word -> counter
So you walk through all words and increment the counter for each occurance of a word using a map.
-
December 3rd, 2008, 09:01 PM
#4
Re: Determining the number of unique words in a .txt file
Thanks, However, I cant use maps or vectors. I'm still having trouble storing the words I find in the text file. Heres my function-
Code:
void displayFile (char fileName[], words array[] )
{
int i = 0;
ifstream inFile;
char line [101];
inFile.open(fileName);
while (inFile.getline(line,101))
{
cout << line << endl;
array[i].word = line;
i++;
}
inFile.close();
}
Where array, is an array of structs made up of character array"word" and an integer "count". Any help on storing the words and the number of words in the array would be helpfull.
Last edited by matt_570; December 3rd, 2008 at 09:02 PM.
Reason: New post
-
December 3rd, 2008, 09:09 PM
#5
Re: Determining the number of unique words in a .txt file
Originally Posted by matt_570
Thanks, However, I cant use maps or vectors.
Yes you can. You may have to implement them yourself though.
-
December 3rd, 2008, 09:20 PM
#6
Re: Determining the number of unique words in a .txt file
I might be using the wrong words but, I cant use strings at all, I need to use character arrays instead. So i cant use std::map or any std::
I'm not sure what std::map does, I never heard of maps (its not in my text book at all). The first time I heard it was asking this question on another board.
-
December 3rd, 2008, 09:28 PM
#7
Re: Determining the number of unique words in a .txt file
Originally Posted by matt_570
I might be using the wrong words but, I cant use strings at all, I need to use character arrays instead. So i cant use std::map or any std::
I'm not sure what std::map does, I never heard of maps (its not in my text book at all). The first time I heard it was asking this question on another board.
Well, then listen to what I already told you.
You need to be able to associate each word with a counter.
It's this association,
word -> counter
Now implement it. Are you stupid or what?
-
December 3rd, 2008, 11:06 PM
#8
Re: Determining the number of unique words in a .txt file
Perhaps you'd better show us what you're passing it for the array parameter. As you have it you'll be limited by a maximum number of words----are you given such an allowance? Or is the teacher expecting you to use a dynamic-sized array here?
-
December 4th, 2008, 03:25 PM
#9
Re: Determining the number of unique words in a .txt file
Originally Posted by _uj
Well, then listen to what I already told you.
You need to be able to associate each word with a counter.
It's this association,
word -> counter
Now implement it. Are you stupid or what?
Thanks for the encouragement , I bet you never had to takes classes on this stuff, you just knew it.
I'm trying to make the line 1 associated with the array[0].count where count is an integer, but I'm running into the same problem with trying to store the line in a character array. I get a syntax error of ( "=": left operand must be a 1-vaule).
The maximun number of words is 100, with a maximun word length of 20.
Heres my whole code -
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;
struct words
{
char word[21];
int count;
};
void displayFile( char [], words []);
void main ()
{
int const wordLength = 21;
int const Num = 101;
int const fileSize = 255;
char filename[fileSize];
cout << "Please enter the name of the file you wish to open: "<< endl;
cin.getline(filename,fileSize);
words array[101];
displayFile (filename, array);
cin.ignore();
}
void displayFile (char fileName[], words array[] )
{
int i = 0;
ifstream inFile;
char line [101];
inFile.open(fileName);
while (inFile.getline(line,101))
{
cout << line << endl;
line = array[i].word;
i++;
}
inFile.close();
}
-
December 4th, 2008, 03:46 PM
#10
Re: Determining the number of unique words in a .txt file
Code:
line = array[i].word;
Two problems here: It's left-right swapped (assignment goes from right to left), and arrays are not assignable in this way. You should be using strncpy() here, with n = 20 and an explicit "array[i].word[20] = 0" statement *just* to make sure that the thing is zero-terminated. Your code will of course break (but not horribly) if any line contains more than 20 characters, or if there are more than 101 lines.
"Breaking" by dropping some of the input is far preferable to "breaking" by overwriting random memory, of course. So always code with that in mind. One of the many reasons why std::string is awesome is because it doesn't have such limitations.
-
December 4th, 2008, 03:47 PM
#11
Re: Determining the number of unique words in a .txt file
Originally Posted by _uj
Now implement it. Are you stupid or what?
That's not appropriate.
-
December 4th, 2008, 04:51 PM
#12
Re: Determining the number of unique words in a .txt file
Thank you that worked. I'm now trying to use strncmp to find out the number of unique words (or lines) in the text file. I tried a couple of things, but none seemed to work. I'm given these guideline-
You must use the linear search algorithm to determine if a word is in the array. Remember that the array is an array of structures and that the key is a string (char array) so the string comparison must be used. The search task should be a separate function.
The search must be a separate function that returns an integer values. Do not use a for loop and the function must have only one return statement.
Heres the instructors linear search-
Code:
int search (int list [], int size, int key)
{
int pos = 0;
while (pos < size && list[pos] != key)
pos++;
if (pos == size)
pos = -1;
return pos;
}
I'm really having trouble on this, any help would be appreciated.
-
December 4th, 2008, 05:09 PM
#13
Re: Determining the number of unique words in a .txt file
It seems like that function would work just fine, with the type of list altered and the != check replaced by the appropriate strncmp call.
-
December 4th, 2008, 05:49 PM
#14
Re: Determining the number of unique words in a .txt file
I'm running into the same problem as I did before. My array is an array of stuctures consisting of a charater array and integer.
Code:
int search (int list [], int size, int key)
{
int pos = 0;
while (pos < size && list[pos] != key)
pos++;
if (pos == size)
pos = -1;
return pos;
}
I assume the integer in the struct is size, he tells me the array is an array of structs, and that the key is the character array. So it seems like I'm supposing to be comparing an array of structs to an array of characters. I tried to code it-
Code:
int pos = 0;
while (pos < 8 && (strncmp(array[pos].words, array[pos]) != 0))
pos++;
if (pos == size)
pos = -1;
cout<< pos<< endl;
but I get a syntax. Like before, I'm running inton problems on the comparision.
-
December 4th, 2008, 06:21 PM
#15
Re: Determining the number of unique words in a .txt file
Those three parameters are:
list, which is your array of structs. (Type will be whatever your struct is called with []).
size, which is the length of that array. (Type will be int.)
key, which is what you're looking for. (Type will be char[]).
The teacher's code compares something in list against key. Yours does not.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|