簡體   English   中英

C++ if 語句使用 lambda 表達式返回 true 但 output 來自 false,為什么

[英]C++ if statement using lambda expression returns true but output is from false, why

我使用 Visual Studio 2017 編譯器在 Windows 10 操作系統上使用 C++ 17。

我正在嘗試建立一個帶有 do-while 循環和用戶輸入的菜單系統(一年后總是一個巨大的痛苦)。 當我這樣做時,如果輸入的類型不正確,我會在“else”語句中提出警告。

我運行了這段代碼,以為它會在它開始之前死掉或完美運行,但是當我在 itemMenu() function 中輸入數字 2 並立即被踢到該函數的 else 語句中時,我感到很驚訝。 我做了一些調試並確認 if 語句都返回 true。 那么我該如何使用 else 語句呢?

我對關鍵的 if 語句使用 lambda 表達式,並測試了 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

這聽起來可能是我的 lambda 表達式是問題所在,但更有可能是需要清除 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);
};

請注意,如果我應該知道更好的方法來做任何事情(如正則表達式或嘗試/拋出/捕獲或其他任何東西),我寧願如果我確信我知道如何做。

看來您正在混合0'0' 也就是說,值 0 和字符 0。第一個是int ,第二個是char C++ 會做一些轉換,所以'0'+2 == '2' ,但重要的是'0'+'2' != '2'

因此,lambda 中的循環應該從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 (數字,而不是字符)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM