猜数小游戏(C++)

#include<ctime>
#include <iostream>
using namespace std;
//#include <stdlib.h>
class Guess
{
    private:
      int Value;
      int CompareTimes;

    public:
      Guess();
      int Compare(int TnputValue);
      int GetCompareTimes();
};
Guess::Guess()
{
    CompareTimes=0;
    srand((unsigned)time(NULL));
    Value=rand()%1000;
}
int Guess::Compare(int InputValue)
{
    CompareTimes++;
    return InputValue-Value;
}
int Guess::GetCompareTimes()
{
    return CompareTimes;
}
int main()
{
    int InputValue;
    char Select;
    cout<<"\n********欢迎使用*********\n";
    for(;;)
    {
        Guess guessobj;
        cout<<"我已经想好数字(0~999),请猜猜\n";
        for(;;)
        {
            int CompareResult;
            cout<<"\n我想的是:";
               cin>>InputValue;
            CompareResult=guessobj.Compare(InputValue);
            if(CompareResult==0)
            {
                int GuessTime=guessobj.GetCompareTimes();
                cout<<"\n恭喜,猜对了"<<endl<<"你一共猜了"<<"次"<<endl;
                break;
            }
            else if(CompareResult>0)
            {
                cout<<"\n对不起,你猜的数大了!\n";
            }
            else
             {
                 cout<<"\n对不起,你猜的小了!\n";
             }
        }
        cout<<"\你还想玩吗('n'=No,Others=Yes)\n";
        cin>>Select;
        cout<<'\n';
        if(Select=='n'||Select=='N')
         {
             break;
         }
    }
    cout<<"**********感谢你的使用!***********\n";
    return 0;
}

你可能感兴趣的:(猜数小游戏(C++))