如何在C++中从cin读取直到按下ESC按钮

问题描述 投票:0回答:6

我正在编写一个直接从用户输入读取数据的程序,并且想知道如何读取所有数据,直到按下键盘上的 ESC 按钮。我只找到这样的东西:

std::string line;
while (std::getline(std::cin, line))
{
    std::cout << line << std::endl;
}

但需要添加一种便携式方式(Linux/Windows)来捕获按下的 ESC 按钮,然后中断 while 循环。如何做到这一点?

编辑:

我写了这个,但即使我按下键盘上的 ESC 按钮仍然可以工作:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    const int ESC=27;
    std::string line;
    bool moveOn = true;

    while (std::getline(std::cin, line) && moveOn)
    {
        std::cout << line << "\n";
        for(unsigned int i = 0; i < line.length(); i++)
        {
            if(line.at(i) == ESC)
            { 
                moveOn = false;
                break;

            }
        }
    }
    return 0;
}

编辑2:

伙计们,这个解决方案也不起作用,它吃掉了我的行中的第一个字符!

#include <iostream>
#include <string>
using namespace std;

int main()
{
    const int ESC=27;
    char c;
    std::string line;
    bool moveOn = true;

    while (std::getline(std::cin, line) && moveOn)
    {
        std::cout << line << "\n";
        c = cin.get();
        if(c == ESC)
            break;

    }
    return 0;
}
c++ escaping getline
6个回答
9
投票
int main() {
  string str = "";
  char ch;
  while ((ch = std::cin.get()) != 27) {
    str += ch;
  }

 cout << str;

return 0;
}

这会将输入输入到字符串中,直到遇到转义字符


1
投票

读完该行后,检查刚刚读到的所有字符并查找转义 ASCII 值(十进制 27)。


这就是我的意思:

while (std::getline(std::cin, line) && moveOn)
{
    std::cout << line << "\n";

    // Do whatever processing you need

    // Check for ESC
    bool got_esc = false;
    for (const auto c : line)
    {
        if (c == 27)
        {
            got_esc = true;
            break;
        }
    }

    if (got_esc)
        break;
}

1
投票

我发现这适用于获取转义键的输入,您还可以在 while 函数中定义和列出其他值。

#include "stdafx.h"
#include <iostream>
#include <conio.h> 

#define ESCAPE 27

int main()
{
    while (1)
    {
        int c = 0;

        switch ((c = _getch()))
        {
        case ESCAPE:
            //insert action you what
            break;
        }
    }
    return 0;
}

0
投票
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int number;
    char ch;

    bool loop=false;
    while(loop==false)
    {  cin>>number;
       cout<<number;
       cout<<"press enter to continue, escape to end"<<endl;
       ch=getch();
       if(ch==27)
       loop=true;
    }
    cout<<"loop terminated"<<endl;
    return 0;
}

0
投票

我建议不仅对于 C++ 中的 ESC 字符,而且对于任何语言中键盘的任何其他字符,读取输入到整数变量中的字符,然后将它们打印为整数。

要么在线搜索 ASCII 字符列表。

这将为您提供密钥的 ASCII 值,然后就很简单了

if(foo==ASCIIval)
   break;

对于 ESC 字符,ASCII 值为 27。


0
投票

以下代码使用用户输入,即“P 或 p”表示付费汽车,“N 或 n”表示非付费汽车,无需每次按 Enter 进行输入。同样,它使用 ESC 键的 ASCII 代码(即 27)来终止程序,并在按 ESC 键时显示平衡。

#include <iostream>
#include <conio.h>  

class tolltb
{
private:
    unsigned int TotalCars;
    double TotalCash;

public:
    tolltb()
    {
        TotalCars = 0;
        TotalCash = 0;
    }

    void payingCar()
    {
        TotalCars++;
        TotalCash += 0.50;
    }

    void nopayCar()
    {
        TotalCars++;
    }

    void display()
    {
        cout << "Total cars: " << TotalCars << endl;
        cout << "Total cash: $" << TotalCash << endl;
    }
};

int main()
{
    tolltb tb;
    char input;

    do {
        cout << "Enter p for paying car, n for non-paying car, or Esc to exit: ";
        input = _getch();   //included in conio.h headerfile
                            //we don't have to press enter key every time we want to input

        if (input == 'p' || 'P')
        {
            tb.payingCar();
            cout << input << endl;
        }
        else if (input == 'n' || 'N')
        {
            tb.nopayCar();
            cout << input << endl;
        }
        else if (input == 27) // ASCII code for Esc key
        {
            break;
        }
        else
        {
            cout << "Invalid input. Please try again." << endl;
        }
    } while (input != 27); // Esc input ASCII code

    cout << "T                  Tollbooth Summary " << endl;
    tb.display();

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.