Read only the first word of cin

0

Good afternoon, I am writing an interactive program directed by commands. Some of them have arguments but others do not.

I leave the code of my function main to give you an idea.

int main(){
    char comando[10];
    ListaPersonas L = nuevaLista();

    mostrarOrdenes();
    cout << "orden>" << flush;
    cin >> comando;
    normalizar(comando);
    cout << endl;
    while(!sonIguales(comando,"fin")){
        ejecutarOpcion(comando, L);
        cout << endl;
        cout << "orden>" <<flush;
        cin >> comando;
        normalizar(comando);
        cout << endl;
    }
    return 0;
}

The question is that I need that in the case of being introduced a command that does not require parameters with some extra parameter, this is ignored and only the main command is executed; with my current code in case of writing a parameter of more this is interpreted as a new command entered, which I do not want.

I have been testing with cin.getline() with different delimiters but without success at the moment.

Greetings, thank you and happy year!

    
asked by pitazzo 31.12.2016 в 16:03
source

2 answers

1

I think your approach is wrong. You must read the user's entry in full and then process it according to your needs.

Proposal

I suggest you read the user's entry and separate this entry in Command and Parameters . If the Command does not require parameters (or are optional) you can decide on the fly. So, create an object that collects the Command and its Parameters :

struct Comando
{
    std::string Nombre{};
    std::vector<std::string> Parametros{};
};

You will need a function that transforms the user input into Comando , for example this:

Comando procesar(std::string &entrada)
{
    Comando resultado{};

    if (!entrada.empty())
    {
        normalizar(entrada); // Asumo que normalizar pasa a minúsculas la entrada
        const auto elementos = split(entrada);
        resultado.Nombre = *elementos.begin();
        std::copy(elementos.begin() + 1, elementos.end(), std::back_inserter(resultado.Parametros));
    }

    return resultado;
}

What the function procesar does is normalizar the entry and then separate each of the words entered by the user (for that use the function split proposed in this thread ), the first word will be the Command and the later ones would be its Parameters .

So, you could read the whole entry line and decide based on what you received:

std::string entrada{};
std::getline(std::cin, entrada);
auto comando = procesar(entrada);

if (sonIguales(comando.Nombre, "fin") && !comando.Parametros.emtpy()){
    std::cout << "vaya locura! fin va sin parametros!\n";
}

Additional notes.

Do not use an array of characters ( char comando[10]; ) for reading commands, better use a std::string

The std::endl order also implies a std::flush .

Once you have the object Comando you can work directly with it, so the function ejecutarOpcion could receive it as is:

void ejecutarOpcion(const Comando &comando) { ... }
    
answered by 02.01.2017 в 10:03
0

You could try this to take the first word of your string:

sscanf(tuStrinf, "%s%*[^\n]", word);

This way you would remove the first word of your string and ignore the rest. Here you have a tutorialspoint manual that could be useful to you.

Greetings and happy year!

    
answered by 31.12.2016 в 16:14