簡體   English   中英

第一次讀取后程序執行不會在 scanf() 處停止

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

我希望在 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;
}

所以,因為程序沒有在 scanf() 處停止,它正在打印所有這些金錢符號,但在達到字符串空白的數量之前不讓我與程序交互......

獲得的輸出圖像:

我知道這可能有些愚蠢,但無法弄清楚,希望你們能幫助我。謝謝:v

我在這里發布了您代碼的正確 C++ 實現。 我知道不推薦僅代碼答案。 但在這里,我沒有太多要告訴你的。 你需要自己檢查你缺乏知識的事情。 最好在此站點上搜索內容。

#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