Counting words

Hi, I have questions regarding to this topic to count the words from the input file. for example, if the words inside the file "Give me everything"
It will be counted as 3.

This is what I got so far


#include <iostream>
#include <fstream>
#include <string>
#include <list>

using namespace std;


void fillList (list <char> &myList, const string &strings);
void printList (const list<char> &myList);
int countChar (const list <char> &myList);
int countWord (list <char> &myList);

int main (int argc, char *argv[])
{
string strings;
list <char> myList;
ifstream file (argv[1]);
if (argc != 2) {
cout << "usage: " << argv[0] << " <filename>" <<endl;
return 0;}
else
{
if (!file.is_open())
{
cout << "Could not open file " << argv[1] << endl;
return 1;
}
else
{
file.eof();
getline(file, strings);
}
}
fillList(myList, strings);
printList(myList);
countChar(myList);
countWord(myList);
}

void fillList (list <char> &myList, const string &strings) //This is to fill the list with the strings inside the file
{
for (int i = 0; i < strings.length(); i++)
myList.push_back (strings[i]);
}

void printList (const list<char> &myList) //This method is to print anything in the list
{
list<char>::const_iterator itr;

for (itr = myList.begin(); itr != myList.end(); itr++ ) {
cout << *itr;
}
cout << '\n';
}

int countChar (const list <char> &myList)
{
list<char>::const_iterator itr;
int count = 0;

for (itr = myList.begin(); itr != myList.end(); itr++ ) {
count ++;
}
cout << "Number of graphical characters are " << count << "\n";
return count;
}

int countWord (list <char> &myList)
{
list<char>::iterator itr;
int count = 0;
char current = *itr;
for (itr = myList.begin(); itr != myList.end(); itr++)
{
if (current == ' ') // I think here is the part where it gives error
count ++;[/b]
}
cout << "Number of words are " << count << "\n";
return count;
}


When I tried run the code it says segmentation fault (core dumped).

The purpose of this program is to count how many words in the text file.
I tried with reading characters, and now I am trying to do with counting words.
So the logic is whenever the iterator returns the character and equal to space, comma, or any special characters, it starts counting.

Can someone point out what is my mistake?

Thanks
Last edited on
Please use code tags: [code]Your code[/code]

The problem is this:

char current = *itr; // itr is pointing to an invalid char

remove that line;


Replace this if (current == ' ') with if ((*itr) == ' ') since current never changes
Thanks for the help!! It works now! :)

By the way, do you know any syntax that divides the words into lines?

I know there is strtok, but I don't know how I can use it in this program.

The output would be
"Give me everything"
Give = 1
me = 1
everything = 1

Thanks :)
if u use the same example above.. after each ' ' just insert a newline and continue.
Oh that's right! Thanks for pointing that out! I am still beginner and new to programming.
Hi guys, I have question. I tried with 2 different txt files

What do you eat? what meat (1)
number of words are 6
What do you eat? what meat? (2)
number of words are 7

1
2
3
4
5
6
7
8
9
10
11
void countWord (list <char> &myList)
   {
   list<char>::iterator itr;
   int count = 0;
   for (itr = myList.begin(); itr != myList.end(); itr++)
   {
   if ((*itr) == ' ' || (*itr) == ',' || (*itr) == '.' || (*itr) == '!' || (*itr) == '?' || (*itr) == '-')
   count ++;
   }
   cout << "Number of words are " << count << "\n";
   }


so, what happened with my code?
What happens is that you count the number of gaps in between the words. Not the words themself.

Introduce a bool (outside the loop). Its the indicator for a gap

On line 8: if that bool is false count otherwise set that bool true.
On line 7: You need an else for that if where you set that bool to false.
Hi coder777, I dont really get what you mean.

Can you expand more than that?

Thanks
Well, this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void countWord (list <char> &myList)
   {
   list<char>::iterator itr;
   int count = 0;
   bool wspace = false;
   for (itr = myList.begin(); itr != myList.end(); itr++)
   {
   if ((*itr) == ' ' || (*itr) == ',' || (*itr) == '.' || (*itr) == '!' || (*itr) == '?' || (*itr) == '-')
   {
      if(! wspace)
      {
         wspace = true;
         count ++;
      }
   }
   else
      wspace= false;
   cout << "Number of words are " << count << "\n";
   }
Topic archived. No new replies allowed.