首页 > 解决方案 > 用户输入/如果语句保持返回而不是继续

问题描述

我正在尝试运行此代码,当我输入“nfc123”的用户输入时,它会打印 Door Unlocked 虽然它返回到开始并希望我再次解锁门。我希望它继续并询问我是否想锁门。

#include <string>
#include <iostream>

int main( int, char ** )
{
    std::string input;

    while( true )
    {
        std::cout << "Unlock Door Code: " << std::endl;
        std::cin >> input;
        if( input == "nfc123" )
        {
            std::cout << "Door Unlocked" << std::endl;
            continue;

        }
        else if( input == "end" )
        {
            break;
        }
        else
        {
            std::cout << "Wrong Code: " << input << std::endl;
        }
    }

    std::string input2;

    while( true )
    {

        std::cout << "Lock Door Code: " << std::endl;
        std::cin >> input2;
        if( input2 == "nfc123" )
        {
            std::cout << "Door Locked" << std::endl;

        }
        else if( input2 == "end" )
        {
            break;
        }
        else
        {
            std::cout << "Wrong Code: " << input2 << std::endl;
        }


    }

    return 0;
}

标签: c++if-statementuser-input

解决方案


推荐阅读