cin.get()不工作? [英] cin.get() not working?

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

问题描述

我第一次使用C ++从PHP。我在玩一些代码。对我的理解 cin.get(); 是假设停止窗口关闭,直到我按下一个键,但是似乎没有工作,因为代码之前它,我不知道是什么问题。这是我的代码:

  #include< iostream& 
#include< cstdlib>

using namespace std;

int multiply(int x,int y);

int main()
{
int x;
int y;

cout<< 请输入两个整数:;
cin>> x>> y;

int total = multiply(x,y);
cout<<总;

cin.get();
}

int multiply(int x,int y){
return x * y;
}


解决方案



  cin.ignore(numeric_limits< streamsize> :: max(),'\\\
')


x>> y; (或 cin.get())之前。



缓冲区 cin ,并删除仍然存在的挂起 \\\
,因为 cin 读取x和y,但也读取最后一个返回(y后)。当你调用 cin.get()时读取。如果你刷新缓冲区 cin.get()将看到一个空缓冲区,一切都很好。


I'm using C++ for the first time from PHP. I was playing around with some code. To my understanding cin.get(); was suppose to stop the window from closing until I press a key, however it doesn't seem to be working because of the code before it, I don't know what the problem is. Here is my code:

#include <iostream>
#include <cstdlib>

using namespace std;

int multiply (int x, int y);

int main ()
{
    int x;
    int y;

    cout << "Please enter two integers: ";
    cin >> x >> y;

    int total = multiply(x, y);
    cout << total;

    cin.get();
}

int multiply (int x, int y) {
    return x*y;
}

解决方案

Put a

cin.ignore(numeric_limits<streamsize>::max(),'\n')

after >> x >> y; (or before cin.get()).

This flushes the buffer of cin and deletes the pending \n which is still there, because you cin reads x and y but also reads the last return (after y). This gets read in when you call cin.get(). If you flush the buffer cin.get() will see an empty buffer and everything is fine.

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

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