简体   繁体   English

从cin读取int编号,直到按ESC C ++

[英]Read int numbers from cin until ESC is pressed C++

I am interested to find a solution for the following problem: 我有兴趣为以下问题找到解决方案:

I have to read int numbers form CIN until the user presses ESC and compute the average of the positive and even integers. 我必须从CIN读取整型数,直到用户按ESC并计算正整数和偶数整数的平均值。

My problem is i am not quite sure how correctly write the stopping condition. 我的问题是我不太确定如何正确编写停止条件。

Here's what i've tried so far(not working properly) 到目前为止,这是我尝试过的(无法正常工作)

#include<iostream>
#include<math.h>

using namespace std;

void main()
{
    int c, sum=0, i=0;
cout<<"Insert characters: ";
while(cin>>c)
{
    i++;
    if((int)c>=0&&(int)c%2==0){
        sum+=(int)c;
    }
}
cout<<"Average: "<<(float)sum/i<<endl;
system("pause");
}

You can try this 你可以试试这个

while((ch = cin.get()) != 27)
{
  i++;
  if((int)ch>=0&&(int)ch%2==0
  {
    sum+=(int)ch;
  }
}

This should loop until the esc key is pressed 这应该循环直到按下esc

The escape character is neither a digit nor a space. 转义字符既不是数字也不是空格。 Thus, if the program gets passed the character in the stream, you can just read integers and the stream will automatically fail when an espace characters is entered (just concentrating on the input aspect of the question): 因此,如果程序在流中传递了字符,则可以读取整数,并且当输入espace字符时(仅关注问题的输入方面),流将自动失败:

#include<iostream>

int main()
{
    int c = 0;
    while (std::cin >> c) {
        std::cout << "c=" << c << '\n';
    }
}

Whether the escape characters is forwarded to the stream by the operating system is, however, a different question. 但是,是否由操作系统将转义字符转发到流是另一个问题。 For example, on UNIXes the escape character will be forwarded but only when the input is given to the program which, by default, is when a line is entered. 例如,在UNIX上,将转义转义字符,但仅在将输入提供给程序(默认情况下是输入行)时才转义。 If the program shouldn't wait for the return key, the operating system needs to get told to not wait for the return key. 如果程序不应该等待返回键,则需要告知操作系统不要等待返回键。

The question seems to be for Windows but I don't know enough about Windows to determine how to change the terminal settings there (see below for something which may still work, though). 这个问题似乎是针对Windows的,但我对Windows的了解还不足以决定如何在此处更改终端设置(不过,请参阅下文了解仍然可以使用的某些功能)。 On UNIXes, you'd clear the ICANON flag in the terminal settings using tcgetattr() and tcsetattr() . 在UNIX上,您可以使用tcgetattr()tcsetattr()在终端设置中清除ICANON标志。 For example, you could use this: 例如,您可以使用以下代码:

#include <iostream>
#include <termios.h>

int main()
{
    termios values;
    tcgetattr(0, &values);
    values.c_lflag &= ~ICANON;
    tcsetattr(0, TCSANOW, &values);

    int c = 0;
    while (std::cin >> c) {
        std::cout << "c=" << c << '\n';
    }
}

I think there is getch() on Windows which get you one character without waiting. 我认为Windows上有getch() ,您无需等待即可获得一个字符。 This, hopefully, also includes the escape character. 希望这也包括转义字符。 If that's the case you could just create a simple stream buffer around getch() and use that instead: 如果是这种情况,您可以在getch()周围创建一个简单的流缓冲区,并改为使用它:

#include <iostream>
#include <streambuf>
#include <whatever-declares-getch>

struct getchbuf
    : std::streambuf {
    static int const end = <getch()-error-value-goes-here>;
    char buffer;
    int underflow() {
        int result(traits_type::eof());
        if (this->gptr() == this->egptr() && (result = getch()) != end) {
            buffer = traits_type::to_char_type(result);
            this->setg(&buffer, &buffer, &buffer + 1);
        }
        return result;
    }
};

int main()
{
    getchbuf sbuf;
    std::istream in(&sbuf);

    int c = 0;
    while (in >> c) {
        std::cout << "c=" << c << '\n';
    }
}

You need to patch up the above code to include whatever declares getch() (I think it is <conio.h> but I don't know) and to specify getch() 's error indicator. 您需要修补以上代码,以包括声明getch() (我认为这是<conio.h>但我不知道),并指定getch()的错误指示符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM