简体   繁体   English

第一次读取后程序执行不会在 scanf() 处停止

[英]Program execution not stopping at scanf() after first read

I want that at every iteration of the do-while() loop the program stops at the scanf() function, but the program loops the same amount of times as the number of white spaces that the read string contains before stoping again at the scanf()我希望在 do-while() 循环的每次迭代中,程序在 scanf() function 停止,但程序循环的次数与再次停止在 scanf 之前读取的字符串包含的空格数相同()

enum Comando
{
    AYUDA,
    LOGIN,
    LOGOUT,
    LISTAR_BUSES,
    LISTAR_RUTAS,
    LISTAR_VIAJES, 
    NULL_COMMAND 
};

//Funcion que registra el comando ingresado por el usuario para indicarle al programa que es lo que debe hacer
pair<Comando, queue<char*>> ProcesarComando(char comandoEntero[20]);
void MostrarAyuda();

int main()
{
    bool salir = false;
    char comando[20];
    Comando cmd;

    do
    {
        printf("\n\t$ ");
        scanf(" %s", comando);

        cmd = ProcesarComando(comando).first;

        switch(cmd)
        {
            case AYUDA:

            break;
            case LOGIN:

            break;
            case LOGOUT:

            break;
            case LISTAR_BUSES:

            break;
            case LISTAR_RUTAS:

            break;
            case LISTAR_VIAJES:

            break; 
            case NULL_COMMAND:

            break;           
        }
    }
    while(!salir);

    //system("pause");
    return 0;
}

pair<Comando, queue<char*>> ProcesarComando(char comandoEntero[20])
{
    int pos = 0;
    char *argumento, *comandoNombre;
    bool tieneParametros = false;
    pair<Comando, queue<char*>> retorno;

    argumento = new char[20];
    
    while(comandoEntero[pos] != '\0')
    {
        if(comandoEntero[pos] == '<'|| comandoEntero[pos] == '[')
        {
            tieneParametros = true;
        }
        else if(tieneParametros && comandoEntero[pos] != ' ' && comandoEntero[pos] != '<' && comandoEntero[pos] != '>' && comandoEntero[pos] != '[' && comandoEntero[pos] != ']')
        {
            strncat(argumento, &comandoEntero[pos], 1);
        }
        else if(tieneParametros && comandoEntero[pos] == ' ')
        {   
            cout<<"HOLAAAAAAA";
            retorno.second.push(argumento);
            memset(argumento, '\0', strlen(argumento));
            tieneParametros = false;
        }
        pos++;
    }

    comandoNombre = new char[20];
    comandoNombre = strtok(comandoEntero, " ");

    if(strcmp(comandoNombre, "ayuda") == 0)
    {
        retorno.first = AYUDA;
        return retorno;
    }
    else if(strcmp(comandoNombre, "login") == 0)
    {
        retorno.first = LOGIN;
        return retorno;
    }
    else if(strcmp(comandoNombre, "logout") == 0)
    {
        retorno.first = LOGOUT;
        return retorno;
    }
    else if(strcmp(comandoNombre, "listar_buses") == 0)
    {
        retorno.first = LISTAR_BUSES;
        return retorno;
    }
    else if(strcmp(comandoNombre, "listar_rutas") == 0)
    {
        retorno.first = LISTAR_RUTAS;
        return retorno;
    }
    else if(strcmp(comandoNombre, "listar_viajes") == 0)
    {
        retorno.first = LISTAR_VIAJES;
        return retorno;
    }

    // printf("\n%s", retorno.second.front());
    // retorno.second.pop();
    // printf("\n%s", retorno.second.front());

    retorno.first = NULL_COMMAND;
    return retorno;
}

So, because the program is not stoping at the scanf(), it is printing all those money signs but without letting me interact with the program until the amount of the string white spaces are reached...所以,因为程序没有在 scanf() 处停止,它正在打印所有这些金钱符号,但在达到字符串空白的数量之前不让我与程序交互......

获得的输出图像:

I know that it may be something dumb, but couldn't figure it out, hope that you guys could help me.THANKS:v我知道这可能有些愚蠢,但无法弄清楚,希望你们能帮助我。谢谢:v

Here I am posting a proper C++ implementation of your code.我在这里发布了您代码的正确 C++ 实现。 I know that code-only answers are not recommended.我知道不推荐仅代码答案。 But here, I don't have much to tell you.但在这里,我没有太多要告诉你的。 You need to yourself check about the things you lack knowledge about.你需要自己检查你缺乏知识的事情。 Better search things on this site .最好在此站点上搜索内容。

#include <algorithm>
#include <iostream>
#include <queue>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

enum Comando {
    AYUDA,
    LOGIN,
    LOGOUT,
    LISTAR_BUSES,
    LISTAR_RUTAS,
    LISTAR_VIAJES,
    NULL_COMMAND
};

auto ProcesarComando(std::string comandoEntero) {

    std::string argumento, comandoNombre;
    bool tieneParametros = false;
    std::queue<std::string> retorno;

    for (auto &&i : comandoEntero)
        if (i == '<' or i == '[')
            tieneParametros = true;
        else if (tieneParametros and std::string(" []<>").find(i) == size_t(-1))
            argumento.push_back(i);

        else if (tieneParametros and i == ' ') {
            std::cout << "HOLAAAAAAA\n";
            retorno.push(argumento);
            argumento.clear();
            tieneParametros = false;
        }

    std::stringstream(comandoEntero) >> comandoNombre;
    const std::vector<std::string> cmd = {"ayuda",        "login",
                                          "logout",       "listar_buses",
                                          "listar_rutas", "listar_viajes"};

    return std::make_pair(
        static_cast<Comando>(std::find(cmd.begin(), cmd.end(), comandoNombre) -
                             cmd.begin()),
        retorno);
}

int main() {
    do {
        std::cout << "\n$ ";
        std::string comando;
        std::getline(std::cin, comando);

        switch (ProcesarComando(comando).first) {
        case AYUDA:

            break;
        case LOGIN:

            break;
        case LOGOUT:

            break;
        case LISTAR_BUSES:

            break;
        case LISTAR_RUTAS:

            break;
        case LISTAR_VIAJES:

            break;
        default: // case NULL_COMMAND:

        }
    } while (true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM