我是第一次从PHP使用C++.我正在玩一些代码.据我所知cin.get();,假设要关闭窗口直到我按下一个键,但由于前面的代码似乎没有工作,我不知道问题是什么.这是我的代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
放一个
cin.ignore(numeric_limits<streamsize>::max(),'\n')
Run Code Online (Sandbox Code Playgroud)
之后>> x >> y;(或之前cin.get()).
这会刷新缓冲区cin并删除\n仍然存在的挂起,因为您cin读取x和y但也读取最后一个返回(在y之后).当你打电话时,这会被读入cin.get().如果刷新缓冲区cin.get()将看到一个空缓冲区,一切都很好.
| 归档时间: |
|
| 查看次数: |
4676 次 |
| 最近记录: |