c++ 获取输入

获取键盘输入

cpp 复制代码
#include <iostream>
#include <limits>

bool enter_state(int &pos)
{
    unsigned int userInput;
    std::cout << "[info] 请选择: 0.主驾, 1.副驾, 2.后排" << std::endl;
    std::cout << "[info] 请输入:";
    std::cin >> userInput;

    if (std::cin.fail())
    {
        std::cin.clear();                                                   // 清除错误状态
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // 忽略之前的输入缓冲
        std::cout << "Invalid input. Please enter an integer." << std::endl;
        return false;
    }

    if (userInput < 3)
    {
        pos = userInput;
        return true;
    }

    return false;
}

bool enter_feel(int &pos)
{
    unsigned int userInput;
    std::cout << "[info] 请输入驾驶id[" << pos << "]的热感受:";
    std::cin >> userInput;

    if (std::cin.fail())
    {
        std::cin.clear();                                                   // 清除错误状态
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // 忽略之前的输入缓冲
        std::cout << "Invalid input. Please enter an integer." << std::endl;
        return false;
    }
    std::cout << "驾驶id[" << pos << "]的热感受:"<<userInput << std::endl;
    return true;
}

int main()
{
    int position = 0;
    while (true)
    {
        if (enter_state(position))
        {
            while (!enter_feel(position))
            {
                /* code */
            }
            
        }
    }
    return 0;
}

红色方框内代码块必须紧跟随,中间插入其他代码,无效

相关推荐
KookeeyLena810 分钟前
如何限制任何爬虫爬取网站的图片
开发语言·c++·爬虫
m_Molly23 分钟前
vs2022配置opencv==4.9.0(C++)
c++·opencv
charon877840 分钟前
Unreal Engine 5 C++: 编辑器工具编写入门(中文解释)
c++·ue5·编辑器·游戏引擎·虚幻
Ddddddd_15843 分钟前
C++ | Leetcode C++题解之第421题数组中两个数的最大异或值
c++·leetcode·题解
末央&1 小时前
【C++】内存管理
java·开发语言·c++
YRr YRr2 小时前
在Windows上安装WSL2和Ubuntu 20.04以搭建C++开发环境的详细指南
c++·windows·ubuntu·wsl2
Farewell_me2 小时前
C++模拟实现list:list、list类的初始化和尾插、list的迭代器的基本实现、list的完整实现、测试、整个list类等的介绍
c++·list
hong1616882 小时前
VSCode中配置C/C++环境
c语言·c++·vscode
小羊在奋斗3 小时前
【C++】探秘二叉搜索树
c++·人工智能·神经网络·机器学习
白葵新3 小时前
PCL addLine可视化K近邻
c++·人工智能·算法·计算机视觉·3d