用C++编辑一个程序,内容是电脑随即从1-100里抽一个数,让用户来猜

如果用户猜过大了,系统会提示,如果用户猜过小了,也会提示.而最后当用户猜对后,会有时间显示用户猜数的整个时间用了多长.
我的邮箱是[email protected]
用txt发给我.谢谢了.
cin>>g_number;在cin下面有红线报错

发了
设定一个计数器每次循环加1,就可以了
while循环需要规定作用域,一个if分支也需要规定作用域,{}规定各自的作用域,while(1)是无条件循环,退出条件是你猜对了,如果错误会一直循环下去,continue,break的作用就是在此
程序如下:
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int number,g_number;
time_t start_time,end_time;
int i=0;
srand(time(NULL));//防止每次程序都产生同样的随机数
number=rand()%100+1;//产生1-100间的随机数
start_time=time(NULL);//记录开始时间
while(1)
{
i++;//记录次数
cout<<"input your number:";
cin>>g_number;
if(g_number<number)
{
cout<<"oh,you number is so little,try again!"<<endl;
continue;
}
else if(g_number>number)
{
cout<<"my lord,you number is so big,try again!"<<endl;
continue;
}
else
{
cout<<"lucky you!you got it!it's "<<number<<endl;
end_time=time(NULL);//记录结束时间
break;
}
}
cout<<"the whole time you use is:"<<end_time-start_time<<"s"<<endl;
cout<<"the times you usded is "<<i<<endl;
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-10-21
#include<iostream>
#include<windows.h>
#include<ctime>
using namespace std;
int main()
{
long t1,t2,t;
int number,gnumber;
srand(time(NULL));//防止每次程序都产生同样的随机数
number=rand()%100+1;//产生1-100间的随机数
t1=GetTickCount();//程序段开始前取得系统运行时间(ms)
while(1)
{
cout<<"input your number:";
cin>>gnumber;
if(gnumber<number)
{
cout<<"you number is so little,try again!"<<endl;
continue;
}
else if(gnumber>number)
{
cout<<"you number is so big,try again!"<<endl;
continue;
}
else
{
cout<<"Right "<<number<<endl;
t2=GetTickCount();//程序段结束取得系统运行时间(ms)
break;
}
}
t=(t2-t1)/1000;
cout<<"the whole time you use is:"<<t<<"s"<<endl;
system("pause");
return 0;
}
第2个回答  2010-10-21
std::cin
相似回答