Counting words in a file

Hi guys. I'm on the verge of rage quitting and this is extremely frustring because I know EXACTLY what I need to do but I can't convey my thought into code.

I have a program that counts words from a file and it works just fine. But now I need to make it start counting from a particular word and stop counting when it reaches another particular word. Here is my loop.

1
2
3
4
5
6
7
8
while(wordfile>>nextword){
   if(nextword == "BEGIN"){
        count = true;
   }else if(nextword == "FINISH"){      
        count = false;
   }
}
cout<<"Total words from BEGIN to FINISH is: "<<count<<endl;


What the hell is wrong with my loop! I've been trying to 2 hours just to fix this and I've tried everything but using a boolean control seems like the most logical way.

Please just fix this for me it's making me so angry :@
If you want to count the words, you'll need to actually have a counter that increases each time you see a word that you want to count.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int counter=0;
count = false;
while(wordfile>>nextword){
   if(nextword == "BEGIN")
  {
        count = true;
   }
   else if(nextword == "FINISH")
  {      
        count = false;
   }
   if (count)
   {
     counter++;
   }
}
cout<<"Total words from BEGIN to FINISH is: "<<counter<<endl;
Last edited on
Thank you so much! I knew I was on the right lines. Thanks again you know I've been stuck on that for over 2 hours? Unbelievable.
Topic archived. No new replies allowed.