简体   繁体   English

为什么我的代码在 Visual Studio 中不起作用?

[英]Why doesn't my code work in visual studio?

this is my code这是我的代码

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

using namespace std;

int main() {
    bool a = false;
    char b='p';
    int c=0;

    while (a != true) {
        if (_kbhit()) {
            b = _getch();
        }
        if (b=='w') {
            c++;
            cout << c << " ";
       }
        else if (b == 'c') {
            cout << "hello";
        }
    }
    system("pause");
    return 0;
}

The problem is where when I press 'w' I want it to print out the value of c and it should be repeating until i press another input for _kbhit() right?问题是当我按下“w”时,我希望它打印出 c 的值,它应该重复直到我按下_kbhit()另一个输入,对吗? because now it add 1 to c then prints c and when i press w again samething.因为现在它在 c 上加 1 然后打印 c 并且当我再次按 w 时同样的事情。 What's wrong with my visual studio I'm using community 2017 I've tried to uninstall it and install it again but same problem occurs.我的 Visual Studio 有什么问题我正在使用社区 2017 我试图卸载它并重新安装它,但发生了同样的问题。

The problem you're running into seems to be a result of a recently added bug in _getch() / _kbhit .您遇到的问题似乎是最近在_getch() / _kbhit添加的错误_kbhit

For an extended key (eg, a cursor key) it's documented that _getch() returns either a 0x0 or 0xe0 followed by the scan code for the key that was actually pressed.对于扩展键(例如,光标键),据记载_getch()返回 0x0 或 0xe0 后跟实际按下的键的扫描码。 What's not documented is that if the user presses a non-extended key, _kbhit will still return true twice in succession, and calls to _getch() will return the key code the first time, and 0x0 the second time.没有记录的是,如果用户按下非扩展键, _kbhit仍会连续两次返回true ,调用_getch()将第一次返回键码,第二次返回 0x0。

In your code, when the user presses 'w' or 'c', _kbhit will return true not just once (as you'd expect) but twice .在您的代码中,当用户按下 'w' 或 'c' 时, _kbhit会返回true (如您所料),还会返回两次 The first time you call it, it'll return the scan code of the key, and the second it'll return a 0 byte.第一次调用它时,它会返回密钥的扫描码,第二次它会返回一个 0 字节。

What's happening in your code is that you're reading the scan code, printing something appropriately, then _kbhit is returning true again, so you read the '\\0' byte, set b to '\\0', and then (since you don't have any code to do anything when b is 0) you (repeatedly) do nothing until the next time the user presses a key.您的代码中发生的事情是您正在阅读扫描代码,适当地打印一些东西,然后_kbhit再次返回true ,因此您读取了 '\\0' 字节,将b设置为 '\\0',然后(因为您不当b为 0 时,没有任何代码可以执行任何操作)您(反复)在用户下次按下某个键之前什么都不做。

Reference参考

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2017 https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=vs-2017

It seems like the program behaves the way you want it to.该程序似乎按照您希望的方式运行。 If I press 'w', it goes into an infinite loop of increasing the value of c and printing it.如果我按“w”,它会进入一个无限循环,增加c的值并打印它。 Pressing any other key stops printing and pressing 'c' goes on the same infinite loop and prints hello .按任何其他键停止打印,按 'c' 继续无限循环并打印hello Doesn't seem to be any problems as far as the posted code is concerned.就发布的代码而言,似乎没有任何问题。 Also, I would like to say the same as @Someprogrammerdude, if it compiles, but doesn't behave the way you want it to, it's an issue with the code, not the IDE and/or compiler.另外,我想和@Someprogrammerdude 说同样的话,如果它编译了,但没有按照你想要的方式运行,这是代码的问题,而不是 IDE 和/或编译器的问题。

Hypothetical answer: Your computer might always be thinking that a key is pressed, thus kbhit() always returns true.假设答案:您的计算机可能总是认为某个键被按下,因此kbhit()总是返回 true。 This maybe caused by a bad mouse/keyboard/controller driver and/or configuration.这可能是由错误的鼠标/键盘/控制器驱动程序和/或配置引起的。 The code is fine, your PC is not.代码没问题,你的电脑不行。

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

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