I am trying to solve this problem:
Write a program to count how many times each distinct word appears in its input.
This is the code so far:
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
using std::cin;
using std::sort;
using std::cout;
using std::streamsize;
using std::endl;
using std::string;
using std::setprecision;
using std::vector;
int main()
{
cout << "Enter words: ";
vector<string> lista;
vector<string> listaCitita;
string x;
while (cin >> x)
{
lista.push_back(x);
}
int listaSize = lista.size();
for (int i = 0; i <= listaSize -1; i++)
{
int x = 0;
int counter = 0;
vector<string>::iterator it = find(listaCitita.begin(), listaCitita.end(), lista[i]);
vector<string>::iterator itu = find(lista.begin(), lista.end(), lista[i]);
if (it != listaCitita.end())
{
break;
}
while(x <= listaSize -1)
{
if(lista[i] == lista[x])
{
counter++;
x++;
if(itu != lista.end())
{
}
else
{
listaCitita.push_back(lista[i]);
}
}
else
{
x++;
}
}
cout << "The string: '" << lista[i] << "' appeared " << counter << " times" << endl;
}
return 0;
}
I'm trying to: If the it
variable has already been printed how many times the word showed, and it wouldn't be printed again how many times it showed.
That's why I made a second vector (listaCitita) where I add the elements that have already been iterated through. The problem is that it doesn't break out of the for loop when I do this.
if (it != listaCitita.end())
{
break;
}