getchar()不工作? [英] getchar() doesn't work well?

查看:275
本文介绍了getchar()不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中编写了这段代码,我使用 getchar() 来调试控制台,但我没有看到使用该函数的任何效果,这里是代码:

I wrote this code in C++, and I used getchar() to puase the console, but I did not see any effect of using that function, here is the code:

#include<iostream>
#include<stdio.h>//to pause console screen

using namespace std;
//function prototypes
int  getSmallest(int*);
int getOccurrence(int, int*);

int main(){

    int a[7], counter=0, smallest;
    cout<<"Please enter 7 integers"<<endl;
    while(counter!=7){
        cin>>a[counter];
        counter++;
    }
    smallest=getSmallest(a);
    cout<<"The smallest number is "<<smallest<<"; it apears "<<getOccurrence(smallest,a)<<" times"<<endl;
    getchar();//to pause console screen
    return 0;
}

int  getSmallest(int*x){
int count=0, smallest=*x;
//loop till last item in array
while(count!=7){

    if(*x<smallest)
        smallest=*x;
    count++;
    x++;
}
return smallest;
}


int getOccurrence(int smallest, int* address){

int count=0,occ=0;
//loop till last item in array
while(count!=7){

    if(*address==smallest)
    occ++;
    count++;
    address++;
}
return occ;

}


推荐答案

已经指出,问题是您的输入缓冲区有一个字符串的数字和一个换行符。 C ++ I / O跳过引导空格,当它读取类似一个数字,但它不会从缓冲区的尾部空白。它留给下一个读取处理。因此 getchar()正在获取尚未处理的换行符。

As has been pointed out, the issue is that your input buffer had a string for the number AND a newline. C++ I/O skips leading whitespace when it reads something like a number out, but it doesn't take the trailing whitespace out of the buffer. It leaves that for the next read to deal with. So getchar() is getting that newline that's still pending.

忽略试图告诉你的人的建议flush(),ignore()或清除 getchar()调用之前的缓冲区中的任何内容。这些函数没有非阻塞输入的概念。

Ignore advice from people who are trying to tell you to flush(), ignore(), or clear out "whatever was in the buffer before the getchar() call". These functions have no notions of "non-blocking" input.

换句话说:通常的C ++输入流函数没有现在没有什么的概念。那么你稍后再打电话,它会说哦,但现在有一些东西!有一个输入序列,你只能检测为停止当你敲EOF。

Said another way: the usual C++ input stream functions don't have a concept of "there's nothing there right now"...but then you call later and it says "oh, but now there's something!!!" There's an "input sequence" that you can only detect as stopping when you hit EOF.

例外情况是 readsome()< /a>,而不是在输入序列上操作在输入缓冲器上操作。找到这样的东西,我们可以尝试这样:

The exception to this would be readsome()...which rather than operating on an "input sequence" operates on the "input buffer". Finding such a thing, we might try this:

#include<iostream>
#include<cstdio>//to pause console screen

using namespace std;

int main(int argc, char* argv[]) {
    cout << "Enter a number\n";
    int num;
    cin >> num;

    char ch;
    while (cin.readsome(&ch, 1) != 0)
         ;

    cout << "Press any key to continue...\n";
    getchar();
    return 0;
}

但至少在我的机器上,它不会导致所需的效果。这意味着即使有一个换行符位于终端应用程序或操作系统管道中,但它尚未达到 cin 的内部流缓冲区对象。 Upshot是:有一个基于缓冲区的非阻塞输入函数,但在这种情况下,它显然不会帮助。

But at least on my machine, it doesn't lead to the desired effect. Which means that even though there's a newline sitting in the terminal app or OS pipeline somewhere, it hasn't yet gotten as far as the internal stream buffer object for cin. Upshot is: there is a non-blocking input function based on the buffer, but in this kind of scenario it apparently isn't going to help.

真正的回答是Alf在评论中说的。最好的dev环境或设置将有一些方法配置它不让控制台自动关闭。如果没有,用你的启动方法攻击它。 heck,你甚至可以在返回0

The real answer is what Alf said in the comment. Most decent dev environments or setups will have some way to configure it to not let the console close automatically. If not, hack around it with your launch method. Heck, you can even put a breakpoint on the return 0 in main!

注意:

您应该注意C的兼容性库的正确C ++包含 #include< cfoo> 而不是 #includefoo.h。它可能不会在实践中的所有大的差异...但至少它分散了你的问题,当人们评论它(像我现在正在做):

You should be aware that "correct" C++ inclusions of compatibility libraries for C are done as #include <cfoo> instead of #include "foo.h". It may not make all that big a difference in practice...but at minimum it distracts from your question when people comment about it (like I'm doing right now):

使用C头文件而不是C ++中的C ++等价物(例如stdio.h而不是cstdio)是不好的做法吗?

演示了一个更小的样本!您可以用以下方法显示效果:

Also, you could have demonstrated this with a much smaller sample! You could have shown the effect simply with:

#include<iostream>
#include<cstdio>//to pause console screen

using namespace std;

int main(int argc, char* argv[]) {
    cout << "Enter a number\n";

    int num;
    cin >> num;

    cout << "Press any key to continue...\n";
    getchar();
    return 0;
}

因此,请尝试删除您的示例,以便将来真正解决问题! (随意编辑您的问题,这是简洁,以使此线程更有用的人搜索)。

So try and pare down your examples to really isolate the problem in the future! (Feel free to edit your question to be this succinct in order to make this thread more useful for people searching.)

这篇关于getchar()不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆