C ++ если оператор, использующий лямбда-выражение, возвращает true, но вывод ложный, почему - PullRequest
0 голосов
/ 19 сентября 2019

Я использую C ++ 17 в ОС Windows 10 с использованием компилятора Visual Studio 2017.

Я пытаюсь настроить систему меню с циклами do-while и пользовательским вводом (всегда огромная боль в годпотом).Когда я делаю это, я помещаю предупреждение в выражение «else», если ввод был неправильного типа.

Я запустил этот код, думая, что он либо умрет до его запуска, либо будет работать отлично, но удивился, когдаЯ ввел номер 2 для ввода в функции itemMenu () и сразу получил удар в остальном статусе этой функции.Я сделал некоторую отладку и подтвердил, что все операторы if возвращали истину.Так как же мне получить вместо этого оператор else?

Я использую лямбда-выражение для критического оператора if и проверил, что значение f, а на самом деле оператор if является истинным.Я не использую их регулярно, и во время отладки я получаю странные уведомления об этом:

Menu.cpp
<...>\menu.cpp(94): warning C4805: '==': unsafe mix of type 'int' and type 'bool' in operation
<...>\menu.cpp(97): warning C4805: '==': unsafe mix of type 'int' and type 'bool' in operation
<...>\menu.cpp(86): warning C4715: '<lambda_6de01b1fefb73a14272db9ac7503c22b>::operator()': not all control paths return a value
<...>\Desktop\startingOver\Debug\startingOver.exe

Может показаться, что моё лямбда-выражение является проблемой, но, скорее всего, это флаги cin, которые нужно очистить.Я тоже пытался (возможно, неправильно), и это не решило проблему.Что может быть не так?Вот мой код:

//Menu.h
#pragma once
#include <iostream>
#include <string>
#include "Player.h"
#include "Item.h"
#include "MoveCommand.h"
using namespace std;

class Menu
{
public:
    Menu();
    ~Menu();

    static void mainMenu(Player * player);
    static void itemMenu(Player * player);
    static void hud(Player * player);
};

и

//Menu.cpp
#include "Menu.h"

Menu::Menu()
{
}

Menu::~Menu()
{
}

void Menu::mainMenu(Player * player)
{
    char input;
    // do and keep doing while input is bad
    do {
        cout << "What to do? (W: go north; S: go south; A: go west: D: go east; I: inventory  ";
        cin >> input;

        if (toupper(input) == 'W')
        {
            MoveCommand * cmd = new MoveCommand(0, -1);
            break;
        }
        else if (toupper(input) == 'S')
        {
            MoveCommand * cmd = new MoveCommand(0, 1);
            break;
        }
        else if (toupper(input) == 'A')
        {
            MoveCommand * cmd = new MoveCommand(-1, 0);
            break;
        }
        else if (toupper(input) == 'D')
        {
            MoveCommand * cmd = new MoveCommand(1, 0);
            break;
        }
        else if (toupper(input) == 'I')
        {
            itemMenu(player);
            break;
        }
        else {
            cout << "Not an option, enter another input...  " << endl;
            system("pause");
        }

        system("CLS");

    } while (toupper(input) != 'W' && 
        toupper(input) != 'S' && 
        toupper(input) != 'A' && 
        toupper(input) != 'D' && 
        toupper(input) != 'I');

    system("cls");
}

void Menu::itemMenu(Player * player)
{
    //All this first part does is draw a figure on the screen:
    //----------------------------------------------
    cout << "INVENTORY: " << endl;

    for (int i = 0; i < 10; i++)
    {
        // if inventory location is null:
        if (player->inventory[i], NULL) {
            cout << "[   ]";
        }
        else
            cout << "[ i ]";
    }
    cout << endl;

    for (int i = 1; i <= 10; i++) cout << "  " << i << "  ";
    cout << endl;
    //------------------------------------------------
    char input;

    // I made this lambda function to facilitate the test the user input is a digit from 1 to 10.
    // it is possible that this is a problem, even though debug says this value returns true (see note/test below)
    auto f = [](char in) {for (int i = 1; i <= 10; i++) {
        if (in == i) return true;
        else
            return false;
    }};

    // do and keep doing until choice is to quit
    do {
        cout << "Pick a slot: (Enter a number 1:10, or 'Q' to exit.  ";
        cin >> input;

        if (toupper(input) == 'Q') break;
        //bool both = (isdigit(input) == true && f(input) == true); // testing if value, which is true in debug tests...
        // ... and yet we never see anything inside this block--I even did a pause, which works everywhere else, but
        // the program is jumping to the else (wrong input) statment
        if (isdigit(input) == true && f(input) == true) {
            cout << "succesfully accessed " << input << "th inventory item" << endl;
            system("pause");
        }

        else
            cout << "(Inventory Else) Not an option, enter another input..." << endl; // I appended (Inventory Else) to confirm we go here
        system("pause");
        system("cls");

    } while (toupper(input) != 'Q' && f(input) == false);
    system("cls");
}

void Menu::hud(Player * player)
{
}

и

//Main.cpp
#pragma once
#include <iostream>
#include <string>
#include "Player.h"
#include "Command.h"
#include "MoveCommand.h"
#include "Event.h"
#include "Item.h"
#include "Tile.h"
#include "Menu.h"

int main() {

    Player player;

    //game loop
    while (1) {
        Menu::mainMenu(&player);
    }

    return 0;
}

РЕДАКТИРОВАТЬ: , и я забыл эту важную частьзагадка:

//Player.h

class Player
{
private:
    int _x;
    int _y; 
public:
    Item *inventory[10];

    Player();
    //~Player();

    //methods
    int getX();
    int getY();
    void move(int x, int y);
};

Обратите внимание, что, если я должен знать лучший способ сделать что-либо (например, регулярное выражение или попытка / выбросить / поймать что-нибудь еще), я бы предпочел, если бы я был уверен, что я знаю, как.

1 Ответ

3 голосов
/ 19 сентября 2019

Похоже, что вы смешиваете 0 и '0'.То есть значение 0 и символ 0. Первый - int, второй - char.C ++ выполнит некоторые преобразования, поэтому '0'+2 == '2', но важно '0'+'2' != '2'.

Следовательно, цикл в вашей лямбде должен работать от char i = '0' до i <= '9'.Не то чтобы это действительно имеет значение здесь.Вы пропустили std::isdigit(c).

Другая ваша проблема - isdigit(input) == true.Это анти-паттерн.В C ++ вы не сравниваете с логическими значениями.И он может неожиданно потерпеть неудачу для isdigit(c), который возвращает ненулевое число , когда c является цифрой.Вполне возможно, что isdigit('5') вернет '5', а isdigit('0') вернет '0'.Да, я сказал ненулевое число, но '0' это ненулевое число.isdigit('C') всегда будет возвращать 0 (число, а не символ).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...