Hi, I'm new to these boards as well as programming. I'm working my way through "Accelerated C++" by Koenig and Moo. I'm happy with this book except for the fact that it does not provide solutions to the execercises.
I'm currently working a word counting program that should display the number of distinct words in its input. I am probably way off track, but have been able to get it to work unless there are 3 or more of the same entries. I could really use advice on where to go from here. Thanks in advance.
Code:
#include<iostream>
#include<string>
#include<vector>
#include<stdlib.h>
using namespace std;
int main()
{
// get input
cout << "Please enter a list a words." << endl;
vector<string> words;
string x;
while(cin >> x)
{
words.push_back(x);
}
//count words
vector<string>::size_type size;
size = words.size();
int pass = 0;
int i = 0;
int j = 0;
int duplicate = 0;
while (i < size)
{
j = 0;
while (j < size)
{
if (i != j && j >= pass)
{
cout << "\nComparing " << words[i] << " with " //troubleshooting
<< words[j] << endl << endl; //troubleshooting
if(words[i] == words[j])
++duplicate;
}
++j;
}
++pass;
++i;
}
cout << endl << "You have entered " << size << " words.";
cout << endl << "You have entered " << size - duplicate << " distinct words.";
cout << "\n";
system("pause");
return 0;
}