Hello ladies and gents,

I had to do the following exercise:
Write a (1)function that reads words from an input stream and stores them in a vector. Use that function both to write programs that (2)count the number of words in the input, and to (3)count how many times each word occured.

My solution for this was the following:

Write the header file for the function:

#ifndef GUARD_WordLibrary_h
#define GUARD_WordLibrary_h

//WordLibrary.h

#include <string>
#include <vector>

void readWords(std::istream&, std::vector<std::string>&);

#endif

Write the .cpp file for the function:

//(1)WordLibrary.cpp file

#include "WordLibrary.h"

using std::istream;				using std::vector;
using std::string;

void readWords(istream& input, vector<string>& aWord)
{
	if (input)
		aWord.clear();

	string str;
	while (input >> str)
		aWord.push_back(str);
}

Write the main.cpp file in wich I call the function and then present the amount of words and the word count.

//Main file

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include "WordLibrary.h"

using std::cin;				using std::sort;
using std::cout;			using std::string;
using std::endl;			using std::vector;

int main()
{
	vector<string> word;
	int amount = 1;
	char ch = 'n';

	while (ch != 'q' && ch != 'Q')
	{
		cout << "Read several words into a vector threw use of a function." << endl;
		readWords(cin, word);
		
		//(2)Show the amount of words in the vector
		cout << "The amount of words in the vector are " word.size() << " ." << endl;

		sort(word.begin(), word.end());
		
		//(3)Show how manny times each word appears in the vector
		for (int i = 0; i < word.size()-1; ++i)
		{
			if (word[i] == word[i+1])
			{
				amount++;
			}
			else
			{
				cout << word[i] << " appears " << amount << " times." << endl;
				amount = 1;
			}
		}
		cout << word.back() << " appears " << amount << " times." << endl;

		cin.clear();

		cout << "Press q to exit the program." << endl;
		cin >> ch;
		ch = toupper(ch);
	}
	return 0;
}

Question is, is this what I was supposed to do?
I searched this forum for the solution, I did a google search to find the solution just so that I would be sure that what I did was correct. Unfortunnatly, it didn't give any result.

Find the book to be really great and have learned alot, but, if there's one comment I could give on it, it's the fact that the solutions weren't given, this way, I could have checked each time I made an exercise to see wether this way was the way it was ment to be made.

I know that for every exercise, they're probably are different solutions, but it would have been easy to be able to check.

Anyway, thanks for the help.

Member Avatar for iamthwee

It looks very similiar to the last problem you posted. Seeing as you turned out a solution for the last one, I see no reason why your solution for this one would be incorrect.

*Remember in programming, many solutions exist, if it does what you expect it to then in most cases it is likely to be right.*

It looks very similiar to the last problem you posted. Seeing as you turned out a solution for the last one, I see no reason why your solution for this one would be incorrect.

Hi iamthewee,

Well, the thing is, it's not about the solution of finding the exact number or how manny of each words there are, like you said, that was from a previous exercise. It's about wether I interpreted the exercise correctly in that I had to write ONE function and that with this function I had to search for the two other required items.

Thing is, those remaining two, should they have been written in seperate functions or as I did, included in the main function?

That is actually the question.

I know that it does what it was supposed to do, the doubt I have is wether it was this way it was ment to be done.

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.