青衫故人 2018-01-11 07:35 采纳率: 100%
浏览 3619
已采纳

为什么相同的程序在vs2017 和dev c++中跑出的结果不一样

#include

using std::cout;
using std::cin;
using std::endl;

int main()
{
cout << endl;
int n = 0;
double prev1 = 1.0;
double prev2 = 1.0;
double current = 0.0;
cout << "how many fib.nums to generate?";
while (!(cin>>n))
{
cin.clear();
cin.sync();
cout << "Please re-enter."< }
cout.precision(15);
while (n > 0)
{
current = prev1 + prev2;
prev2 = prev1;
prev1 = current;
--n;
}
cout << current << "\t";
cout << "ratio=" << prev1 / prev2 << endl;
cout << endl;
cout << "Press enter to exit.";
cin.ignore();
cin.ignore();
return 0;
}

 vs中输入字母会陷入死循环 另一个正常运行
  • 写回答

6条回答 默认 最新

  • zzgoucx 2018-01-11 09:43
    关注

    要不改为
    while (n)
    {
    if(cin.fail())
    {
    cin.clear();
    cin.sync();
    }
    cout << "Please re-enter."
    }
    再试一试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?