#include <iostream>
#include <string>

using namespace std;
int main()

{
    int i;
    int words = 1;
    string userString;

    cout << "Enter the text you want this fancy Word counter to count: " << endl << endl;
    getline(cin,userString);

    for(i = 0; i < userString.length(); i++)
    {
        if(userString.at(i) == ' ')
            words++;
    }
            if((isalnum(userString) == 0))
            words--;

    cout << "\nThere are " << words << " words in your text." << endl << endl;

    return 0;
}

so thats what i got. as you can see it works with the minor exception of the 'isalnum' if statement. what i want to do is keep this little program from counting spaced punctuation marks like ". . . ! ! !" right now all of those are counted as words. not a real application you know :)
any direction would be most appreciated.

also i keep seeing threads that say you shou NOT use 'void main()' if there a link to an explanation to this. my instructor says it doesnt really matter... what gives?


thank you!

Recommended Answers

All 4 Replies

>> my instructor says it doesnt really matter... what gives?
tell your instructor to RTFM! The c and c++ iso standards dictate that main() must always return an integer. I think you will find a lot of references to it here at DaniWeb. And here are quite a few references to it.

As for you program:
1. when a space is found you need to increment the i counter past all remaining spaces without counting them as words. For example if I enter "Hello World" your program will count a lot more then two words .

2. Don't know the purpose of lines 20 and 21. You should just delete them.

3. Inside that for loop check if the current character is a punctuation character -- see ispunct(). If punctuation then just ignore it and continue with the next charcter in the string.

thanks for the link. i will pass it on... just a little more "PC" .

anyway any love on solving my little silly program? i know there are a ton WC scenarios out there and here, but the ones i have foud are either way above my level or just dont even address it. c'mon i know you guys have a clue!!

PLEASE

please re-read my post -- I was probably adding the comments before you had a chance to read it.

thnks ancient dragon!
i removed lines 20 and 21 (i see why the numbers on the code) which i knew were wrong. i replaced line 17 with if(isspace( i )) and works much better.
the ispunct() is what i was looking for thank you. you suggested to put it in my for loop... i cant figure that one out! could you give a hint?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.