I have to make a program that reads in words, deletes certain ones, stores ones that pass, and then count the total. He didn't exactly specify how the words would be coming it, but I'm assuming that if a space at the end of the word is bad, that it's probably a word on each line. So, getline sounds like the best way to get the information.

Of course, these words he is giving us to store could be a web address for all we know. So, what would be a good size for me to initialize the array to, or is there a way to store it in a string so I wouldn't have to worry about the size.

Recommended Answers

All 3 Replies

string myString;
getline(cin, myString);

Chris

It kind of depends on how the assignment defines "words". If you mean any sequence of displayable characters, bounded by whitespace (thus, a punctuation would be included with the word it precedes or follows), then cin >> would do the job of isolating individual words. If you have a more restrictive definition, such as just the sequence of alphabetic characters (no digits or punctuation) you will have to do a bit more more. You could still use the cin >>, then examine the beginning and ending character(s) of the word to determine more about its state. You might trim off punctuation.

cin >> has the benefit of not being concerned with number of words on an input line, it stops at each.

You could use a char array of any large size you want (256, 1024....) or you could use the string type.

Thanks. I forgot after that you can have it just output to a string...

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.