Jump to content
  • Advertisement
Sign in to follow this  
xidis

Question about how to do an exercise in a book?

This topic is 5040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The exercise is as follows: "Write a program to count how many times each distinct word appears in its input." This exercise came out of Accelerated C++, chapter three. In this chapter, vectors are covered along with the while (cin >> x) loop. Im assuming that you have to use these two things, but i can't think of a way to do it. Im thinking that you store each string in the vector by using the .push_back(x) member function. But then i dont know what to do from then. I dont think the chapter covered how to do this. Any help on this problem is appreciated. Thanks.

Share this post


Link to post
Share on other sites
Advertisement

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
vector<string> word;
vector<string>::const_iterator cIter;
vector<string>::const_iterator pIter;

int count;

string input;

while (cin >> input)
{
word.push_back(input);
}

for (cIter = word.begin(); cIter != word.end(); ++cIter)
{
count = 0;

for (pIter = word.begin(); pIter != word.end(); ++pIter)
{
if (*cIter == *pIter)
++count;
}

cout << *cIter << " appears " << count << " time.\n";
}

return 0;
}



You can do this...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!