编程高手进~~~用C++编程解决此题

来源:百度知道 编辑:UC知道 时间:2024/06/24 14:34:27
编程先由计算机“想”一个1到100之间的数请人猜,如果人猜对了,则计算机给出提示:“Right!”, 否则提示:“Wrong!”,并告诉人所猜的数是大(Too high)还是小(Too low),最多可以猜10次。如果猜了10次仍未猜中的话,则停止本次猜数,然后继续猜下一个数。每次运行程序可以反复猜多个数,直到操作者想停止时才结束。

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{//srand(time(0));
int num=rand()%100+1;
int tries=0,guess;
cout<<"Welcome to Guess my number!"<<endl;
do
{
cout<<"Enter a guess!"<<endl;
cin>>guess;
tries++;
if(tries!=10)
{
if(guess>num)
{
cout<<"It's high !"<<endl;
}
if(guess<num)
{
cout<<"It's low !"<<endl;
}
}
else
{
cout<<"Error! The number is:"<<num<<endl;
cout<<"Number has been initialed !"<<endl;
num=rand()%100+1;
tries=0;
}
}
while(guess!=num);
cout<<"That's it! You get it in "<<tries<<" g