cin 输入问题 后续输入不可得,即使clear也不行啊。
#include "stdafx.h "
#include <iostream>
#include <conio.h>
using namespace std;
int nHeight, nWidth;
int nH, nW;
bool Initialization()
{
nHeight = 0, nWidth = 0, nH = 0, nW = 0;
//if( cin.good() )
//cin.clear();
cout < < "输入原料的大小:长 宽 " < < endl;
cin > > nHeight > > nWidth;
if(nHeight > nWidth)
{
int n = nHeight; nHeight = nWidth; nWidth = n;
}
//if( cin.good() )
//cin.clear();
cout < < "输入产品的大小:长 宽 " < < endl;
cin > > nH > > nW;
if(nH > nW)
{
int n = nH; nH = nW; nW = n;
}
cout < < "获得的输入: " < < endl;
cout < < "原料的大小: " < < nHeight < < " * " < < nWidth < < endl;
cout < < "产品的大小: " < < nH < < " * " < < nW < < endl;
if(!nHeight || !nWidth || !nH || !nW)
{
cout < < endl < < "输入数据不合法 " < < endl;
return false;
}
else
return true;
}
int OutMaxCount()
{
int nMax = nHeight*nWidth/(nH*nW);
cout < < "最大获得产品数量: " < < nMax < < endl;
return 0;
}
int OutHorizontalCount()
{
int xCount = nWidth / nW;
int yCount = nHeight / nH;
int nMax = xCount * yCount;
cout < < "普通平行方向最大获得产品数量: " < < nMax < < endl;
return 0;
}
int OutVerticalCount()
{
int xCount = nWidth / nH;
int yCount = nHeight / nW;
int nMax = xCount * yCount;
cout < < "普通交叉方向最大获得产品数量: " < < nMax < < endl;
return 0;
}
int main()
{
while(true)
{
if(Initialization())
{
OutMaxCount();
OutHorizontalCount();
OutVerticalCount();
}
cout < < "********************************* " < < endl;
char c;
cout < < "继续(Y/y)或退出(N/n): " ;
do
{
c = getch();
}while(c != 'Y ' && c != 'y ' && c != 'N ' && c != 'n ');
cout < < c < < endl;
if(c != 'Y ' && c != 'y ')
break;
}
return 0;
}
//本来是做着玩的,谁知道出问题了,
//Initialization里,如果输入不是数字的话,那么就会结束,
//也许是我刚查的IO错误吧,不过我还是没弄明白。因为我也处理了一下,没有效
//果,又注掉了。再以后就都不能再接收输入了。
[解决办法]
cin.clear();
cin.sync();
同时使用这2句。