Create an array that does not repeat words C ++ strings

1

I'm doing a program in C ++ that takes words from a .txt file and inserts each one into an array, the problem is that you should NOT repeat any word, and I'm having complications in that part. Currently I'm only working in that part, the menu only works for the moment with 1.

/ Project.cpp: //

#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int menu()    //
{
    cout << "Proyecto\n";
    cout << "0. Salida\n";
    cout << "1. Lectura de archivos\n";
    cout << "2. Busqueda \n";
    cout << "Opcion: ";
    int opcion;
    cin >> opcion;
    return opcion;
}


void Almacenar(string Arreglo[200], int tamanio = 200)
{
    string palabra;
    int valor, i=0;
    ifstream ficheroEntrada;
    ficheroEntrada.open("Entrada.txt");

    while (!ficheroEntrada.eof())                                   //eof para comprobar que no hemos llegado al final del archivo.
    {

            while (ficheroEntrada >> palabra)
            {

                bool comp = false;
                while (i < tamanio)
                {

                    int j = 0;

                    while (j <= i && comp == false)
                    {


                        if (palabra.compare(Arreglo[j])==0)
                        {


                            comp = true;
                        }
                        else
                        {

                            j++;
                        }
                    }
                    if (comp == false)
                    {
                        Arreglo[i] = palabra;

                        i++;

                    }

                }
            }
    }
    ficheroEntrada.close();
}
int main()
{
    int opcion, tamanio = 250;
    string Arreglo[200];
    do
    {
        opcion = menu();
        switch (opcion)
        {
        case 1: Almacenar(Arreglo, tamanio); break;

        }
    } while (opcion != 0);
    system("pause");
    return EXIT_SUCCESS;
}
    
asked by Eric Arzate 13.11.2018 в 05:52
source

2 answers

2

changes the two while nested by a loop for . You really only need to iterate once the collection to know if a word is present or not in the list, then with a single loop should be enough.

It would also eliminate the eof check. This flag is only activated after of a reading that reaches the end of the file and at that moment you will have already failed the while that reads the words ... so much redundancy is not necessary.

while (ficheroEntrada >> palabra)
{
  bool duplicada = false;

  for( size_t i=0; i<tamanio; i++ )
  {
    duplicada = palabra == Arreglo[i];
    if( duplicada ) break;
  }

  if( !duplicada )
  {
    Arreglo[tamanio] = palabra;
    tamanio++;
  }
}
    
answered by 13.11.2018 в 07:29
0

Do not reinvent the wheel, the STL offers containers that remove duplicates if you know how to use them , I present you std::map . Adapting the proposal of eferion :

std::map<std::string, int> palabras;

while (ficheroEntrada >> palabra)
{
    ++palabras[palabra];
}

The map index operator ( operator [] ) returns the element in the indicated index and if the index did not previously exist: it creates it.

Therefore, with the previous code, you will have the unique words of the text read, how many times they appeared and you will also receive them in alphabetical order:

for (const auto &aparicion : palabras)
    std::cout << "La palabra '" << aparicion.first << "' apareció " << aparicion.second << " veces en el texto\n";
    
answered by 13.11.2018 в 10:17