少年缪 2016-01-04 01:57 采纳率: 0%
浏览 1463

C++新手问题(死亡循环体)

case 3:
//题三
{
while (x == 1)
{
cout << "编程实现:从键盘输入任意多个整数(以输入字符a结束输入),输出第三大的整数\n";
Formatted1(str);
cout << "演示功能;" << endl;
cout << "Please enter your nums : \n";
ThirdNum();
Formatted1(str);
cout << "是否再展示一次?\n请输入1(再展示一次) OR 0(退出): ";
cin >> x;
}
break;
}

```void Formatted1(string s)
{
cout << s;
cout << endl;
cout << s;
cout << endl;
}
//找第三大的数字
void ThirdNum()
{
int XX = 0;
fir = numeric_limits::min(), sec = numeric_limits::min(), thi = numeric_limits::min();
cin >> XX;
while (cin.good())
{
if (XX > fir)
{
thi = sec;
sec = fir;
fir = XX;
}
else if (XX > sec && XX < fir)
{
thi = sec;
sec = XX;
}
else if (XX > thi && XX < sec)
{
thi = XX;
}
cin >> XX;
}
Formatted1(str);
if (fir == numeric_limits::min())
c1 = 3;
else if (sec == numeric_limits::min())
c1 = 2;
else if (thi == numeric_limits::min())
c1 = 1;
if (c1 != 0)
cout << "you have " << c1 << " numbers not to input .\n";
else
cout << "Your thrid number is :" << thi << endl;
XX = numeric_limits::min();
return;
}
全局变量
int x = 1; //用户输入量
int fir = numeric_limits::min(), sec = numeric_limits::min(), thi = numeric_limits::min();
string str(50, '=');


我的CASE 3是一个死循环, 请各位和我交流一下
  • 写回答

1条回答 默认 最新

  • fuyun11 2016-01-04 04:23
    关注

    在case 3 中
    cout << "是否再展示一次?\n请输入1(再展示一次) OR 0(退出): "
    语句后加上
    cin.sync();
    cin.clear();
    进行清空缓冲区中的数据,然后再进行输入数据;
    否则的话cin >> x会一直输出缓冲区的数据,而无法自己输入

    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R