-5

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • What makes you think it does not break of the loop? Add the debugging print `cout << (listaCitita.end()==it ? "it=end" : "it!=end") << endl;` before the `if(it != listaCitita.end())`. Add `cout << "It breaks!" << endl;` before the `break;`. Add `cout << "Did not break!" << endl'` after the `}`. See what happens. – user31264 Jun 30 '16 at 09:48
  • Did it. It didn't break. – furthergarden Jun 30 '16 at 10:29
  • Please send the excerpt from the output. – user31264 Jun 30 '16 at 10:43
  • Enter words: apple pear apple apple pear ^Z it=end It breaks! it=end It breaks! it=end It breaks! it=end It breaks! it=end It breaks! – furthergarden Jun 30 '16 at 11:34
  • Could you please copypaste your new code in your message? – user31264 Jun 30 '16 at 12:05
  • cout << (listaCitita.end()==it ? "it=end" : "it!=end") << endl; if(it == listaCitita.end()) { cout << "It breaks!" << endl; continue; } cout << "Did not break!" << endl; – furthergarden Jun 30 '16 at 12:54
  • The piece of code you just sent behaves just fine. It doesn't break because you replaced `break` by `continue`. I don't see any problem at all. – user31264 Jun 30 '16 at 17:04

3 Answers3

2

The break statement ends execution of the nearest enclosing loop or conditional statement in which it appears. Control passes to the statement that follows the end of the statement, if any.

When you reach break, you will jump at the end of the for loop (return 0). What you want to use here is the continue keyword, which will skip the current word and continue with the next one.

Gwen
  • 1,436
  • 3
  • 23
  • 31
1

If your program reaches break it will definitely break out of it.

One other way to break out of a loop especially if it is a nested loop and you want to break out of an outer loop would be goto.

...
while(...) { while(...) { if(...) { goto end; }}}
end: //will jump here
...
Shiro
  • 2,610
  • 2
  • 20
  • 36
1

You can break out of nested loops using goto, throw or return (of course you need to separate your loops into a function or a lambda for this latter method to work).

In any case your particular task doesn't call for breaking out of a nested loop, or any other loop for that matter. You need to check if the word was already printed in the outer loop. If it was, continue, otherwise proceed to counting it, printing it, and pushing it to the vector of already-printed words.

Using containers and/or algorithms appropriate for the task would eliminate the need to use nested loops altogether. Consider std::unordered_map. Also read about std::count.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243