Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,055 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,619 people online right now. Registration is fast and FREE... Join Now!




Editing my Word Count Feature to Count Words in a Paragraph Rather tha

 
Reply to this topicStart new topic

Editing my Word Count Feature to Count Words in a Paragraph Rather tha

cJr.
4 Aug, 2008 - 12:05 PM
Post #1

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 30


My Contributions
I have this Word Count feature on my revision program I have created.

I developed it to be able to count a sentance up to one full stop and it works perfectly to do that. However, I have decided I would like it to count how many words a user types in a whole paragraph, allowing them to use full stops, question marks & other punctuation.

Here is my code so far:

CODE

void wordCounter(int& keyPressed, int& currentMonth, int& currentYear)
{//start of 'wordCounter' function to allow the user to count the number of words they have typed
    
int getKeyPress();

    char letter;
    int numberOfWords = 0;    
    
    
    Gotoxy(0, 22);
    SelectTextColour( clDarkGrey);
    cout << "To go back to the menu, press the 'M' key at any time.";
        
    Gotoxy(6, 6);
    SelectTextColour( clGrey);
    SelectBackColour( clBlack);
    cout << "Please type in a sentance & press the 'Enter' key:";
    Gotoxy(6, 9);
    SelectTextColour( clWhite);
    cin.get( letter);

            while ( letter != '.')
            {
                    if ( letter == ' ')
                        {
                            numberOfWords = numberOfWords + 1;
                        }
                    cin.get( letter);
            }
                

            if ( numberOfWords > 0)
                {
                    numberOfWords = numberOfWords + 1;
                    Gotoxy(6, 19);
                    SelectTextColour( clGrey);
                    SelectBackColour( clBlack);
                    cout << "Thank you. The number of words you have entered is ";
                    SelectTextColour( clDarkCyan);
                    cout << numberOfWords;
                }


    keyPressed = getKeyPress();
    if (keyPressed == 'M')
        {
            displayMenu(keyPressed, currentMonth, currentYear);
            displayHeaderFooter(currentMonth, currentYear);
        }
    
    
    while(!_kbhit());
}//end of 'wordCount' function


I know it may seem simple to others, but I am a real beginner lol & cannot seem to understand how I would get this to count the number of words past the full stop. I think it may have something to do with this line:
CODE
while ( letter != '.')
in the while statement, but I can't tell what?

Thank you in advance.
Calum.


----------------
Now playing on iTunes: All-4-One - I Swear
via FoxyTunes
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Editing My Word Count Feature To Count Words In A Paragraph Rather Tha
4 Aug, 2008 - 01:16 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Well you could see what happens when you chance '.' to '\n'
User is offlineProfile CardPM
+Quote Post

cJr.
RE: Editing My Word Count Feature To Count Words In A Paragraph Rather Tha
5 Aug, 2008 - 05:27 AM
Post #3

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 30


My Contributions
QUOTE(NickDMax @ 4 Aug, 2008 - 10:16 PM) *

Well you could see what happens when you chance '.' to '\n'



Unfortunately, if I do it like that it just doesn't let me type anything when I come to that function. It just exits the program as I try & type anything; however, if I put the '.' back in then it works just as it did before.

Is there anything else I can try?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 05:54PM

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