Finish a vector of arrays with a while

2

Basically I want to fill a vector of strings (I thought of doing it with a for), the vector will receive the data through the keyboard (with cin). So far so good but I have no idea how to do to stop the cycle and stop filling the vector at the time I want. That is, it could make a maximum size not very large and when it gets there it would end. But I want it to be variable. I had thought then of conditioning the cycle with a while but I have no idea what it is that has to be in the while to work. I have done this:

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

/*
*/
int main(int argc, char** argv) {
    int tam = 1000;
    string collar[tam];
    cout << "Introduce el nombre de una mazmorra: " ;
    cout << "Cuando termines de introducir datos pulsa 0";

    do{
        for (int i = 0; i < tam; i++)
            cin >> collar[i];
    }while ();

    return 0;
}

I thought about conditioning 0, but anything helps me. Also, if someone thinks that all this can be done in a simpler way or whatever, I would also like to know:).

    
asked by JACK1ETO 12.05.2018 в 19:15
source

2 answers

2

From what I see, you are not only making mistakes in your analysis, but also in the syntax. Maybe this will be solved if you have a little more detailed information about what you want to get.

  • You can not enter a string via cin > > . You need to use the getline (inputStream, string) function. EXAMPLE: getline (cin, necklace [i])
  • To save characters, it is better to use an array of char . The difference between char and string is that the first type only stores 1 byte (that means that if you enter "RED" only the first character is saved) and the second type is a dynamic pointer of char (That means that if you enter "RED" it saves all the characters of the word).
  • I corrected the code a bit without making it unintelligible to your vision.

    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main(int argc, char** argv) {
        string collar[100];
        cout << "Introduce el nombre de una mazmorra: " ;
        cout << "Cuando termines de introducir datos pulsa 0";
    
        for (int i = 0; i < 100; i++){
            getline(cin, collar[i]);
            if(collar[i]=="0")
                break;
        }
        return 0;
    }
    

    The break placed at the end ends with the for before the condition of finishing all the elements is fulfilled.

        
    answered by 12.05.2018 / 20:57
    source
    0

    For the vector of strings to be of variable size you must create it dynamically. Here I send you a code similar to what you need, I added some comments, anyway if you do not understand something you can ask.

    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <string.h>
    
    using namespace std;
    
    int main()
    {
    int size = 3;
    int count = 0;
    string * collar = new string[size];
    string reciever;
    string * temporal;
    
    cout<< "Intoduce el nombre de una mazmorra: ";
    cout<< "Pulsa 0 para terminar de introducir datos.";
    
    while (true)
    {
        fflush(stdin); //Limpiar el buffer de entrada
        cin>>reciever;   //Almacenas el valor entrado en una variable temporal para verificar si es cero o un dato válido.
        if(reciever.compare("0")==0) //En caso de ser cero terminas el ciclo while.
            break;
        else   //Si no lo almacenas en tu vector.
        {
            if(count==size) // Aumentar el tamaño del vector ya que ha alcanzado su capacidad máxima.
            {
                temporal = new string[size+10];
                for (int j=0;j<size;j++)
                {
                    temporal[j]=collar[j];
                }
                delete[] collar;
                collar = temporal;
            }            
            collar[count]=reciever;   
            count++;
        }
    }
    return 0;
    }
    

    In this way you can enter as many strings as you need and use them at your convenience, taking into account that they are stored in a collar [...] each position contains a string.

        
    answered by 12.05.2018 в 21:54