猜数字程序问题
我给电脑输入一个答案, 然后让玩家才我输入的数字, 如果他猜高了我会提示他, 猜低了也提示, 但是如果猜超过8次的话, 程序就自动显出答案, 如果按0的话就退出。我现在的程序其他的都没问题,除了玩家猜了8次后,程序还是不显答案, 还让玩家继续无限猜下去, 不知道问题出在哪里了, 大家帮忙看一下,谢谢啦
#include <iostream>
using namespace std;
int main ()
{
const int num=56;
int guess, i, flag=0, count=1;
cout<<"Secret number between 1-100 generated..."<<endl;
do
{
cout<<"Please enter your guess?";
cin>>guess;
if (guess<num && guess!=0)
{
if(num-guess<=5)
cout<<guess<<" is lower than the answer but close..."<<endl;
else
cout<<guess<<" is lower than the answer..."<<endl;
}
else if (guess>num)
{
if(guess-num<=5)
cout<<guess<<" is greater than the answer but close..."<<endl;
else
cout<<guess<<" is greater than the answer..."<<endl;
}
else if (guess==0 || count>8)
flag=1;
else
{
cout<<"Correct in "<<count<<" guesses."<<endl;
flag=1;
}
count++;
}while (flag != 1);
if(count>8)
cout<<"8 guesses. The secret number is "<<num<<"."<<endl;
else if (guess==0)
cout<<count<<" guesses. The secret number is "<<num<<"."<<endl;
return 0;
}