输入字母就刷屏!怎么回事呢?
今天编一个很简单的程序试了一下其他都好就是一输入字母就刷屏,不知道是什么原因,恳请各位大虾帮忙啊!
下面是程序:
#include <iostream>
void time(int hours,int minutes);
int main()
{
using namespace std;
int hours,minutes;
cout < < "Enter the number of hours: ";
cin> > hours;
while (hours <0 || hours> 12)
{
cout < < "Your hours is wrong! Please input it again! ";
cin> > hours;
}
cout < <endl;
cout < < "Enter the number of minutes: ";
cin> > minutes;
while (minutes <0 || minutes> 60)
{
cout < < "Your minutes is wrong! Please input it again! ";
cin> > minutes;
}
cout < <endl;
time(hours,minutes);
cin.get();
cin.get();
return 0;
}
void time(int hours,int minutes)
{
using namespace std;
cout < < "Time: " < <hours < < ": ";
if(minutes <10)
{
cout < < "0 " < <minutes < <endl;
}
else
cout < <minutes < <endl;
}
[解决办法]
在while循环中cin> > 的下面加入cin.clear(),cin.sync()两行代码,用以清除输入流缓冲区