Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,824 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,606 people online right now. Registration is fast and FREE... Join Now!




trying to write a function that increments the number of words in a do

 
Closed TopicStart new topic

trying to write a function that increments the number of words in a do

Phantom_of_the_opera
11 Feb, 2007 - 08:19 PM
Post #1

New D.I.C Head
*

Joined: 13 Nov, 2006
Posts: 43


My Contributions
.

This post has been edited by Phantom_of_the_opera: 3 May, 2007 - 10:45 AM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Trying To Write A Function That Increments The Number Of Words In A Do
12 Feb, 2007 - 05:23 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,156



Thanked: 44 times
Dream Kudos: 125
My Contributions
You are taking data from a file character by character there, so you have to check for the letter which comes your way everytime you traverse a loop.

let's say if the charater is a word separator like ',' , ' ' , '.' , ':' , '?' , '!' etc. then you
can increment the word count.
On the '.' character you can increment the line count

Now how you decide your paragraph formatting will decide what your paragraph count logic will be.
If new paragraph is just one enter to go on new line then you can check for '\n' and increment paragraph count.

If you want it in a proper way then check 2 character sequence '\n' followed by '\t'. [I think first one is better].

to do these things you need some if or switches and the job is done.

Hope this will help you. smile.gif
User is offlineProfile CardPM
+Quote Post

Phantom_of_the_opera
RE: Trying To Write A Function That Increments The Number Of Words In A Do
12 Feb, 2007 - 05:42 PM
Post #3

New D.I.C Head
*

Joined: 13 Nov, 2006
Posts: 43


My Contributions
-

This post has been edited by Phantom_of_the_opera: 3 May, 2007 - 10:45 AM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Trying To Write A Function That Increments The Number Of Words In A Do
2 Mar, 2007 - 04:38 AM
Post #4

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,156



Thanked: 44 times
Dream Kudos: 125
My Contributions
Is it necessary to have different function for that?

you can have a "switch" case block where you can check what is coming your way...

this is not the code but it will look something like...

CODE

char ch;
int counted =0;  //to handle multiple time counting due to "hello, "
// here , is followed by space so can create problem in counting.
while(!infile.eof())
{
   infile.get(ch);
   cout<<ch;
   switch(ch)
  {
     case '\n' : paraCount++;
     case '.'   : lineCount++;
                    wordCount++;
                    counted=1;
  }
  if((ch==' ' ||ch==';' ||ch==',' ||ch==':' ||ch==')' ||ch=='?' ||ch=='!')&&(counted==0))
  {
     wordCount++;
     counted=1;
   }
   else if((counted!=0)&&(!(ch==' ' ||ch==';' ||ch==',' ||ch==':' ||ch==')' ||ch=='?' ||ch=='!')))
   {
     counted =0;
   }
}




forgive me for any syntactical errors but hope you have got the logic there. wink2.gif
User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 1/8/09 09:18AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month