编译怪谈
代码如下:
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void main()
{
srand(time(0));
int counter=0;
int magic,guess;
magic=1+rand()%1000;
cout<<"I have a number between 1 and 1000.\n"
<<"Can you guess my number?\n"
<<"Please type your first guess."<<endl;
cin>>guess;
counter++;
while(guess!=magic)
{
if(guess>=magic)
cout<<"Too high,try again.\n";
if(guess<=magic)
cout<<"Too low,try again.\n";
cin>>guess;
counter++;
}
if(counter==10)
cout<<"Ahah! You know the secret.\n";
if(counter>10)
cout<<"You should be able to do better.\n";
if(counter<10)
cout<<"Either you know the secret or you got lucky!"
<<endl;
for(;;);
}
最后一个for循环是用来延时的,可以去掉,
但如果不去掉,编译后执行时,当应该要显示语句Ahah! You know the secret和You should be able to do better.时,它就不会显示出来。这是怎么回事?
另一个问题的代码:
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
int produce_rand_number(void)
{
return 1+rand()%10;
}
void main()
{
int result;
srand(time(0));
for(;;)
{
int number1=produce_rand_number();
int number2=produce_rand_number();
cout<<"how much is "<<number1<<" "
<<"times "<<number2<<"?---(0 to end)"<<endl;
cin>>result;
if(result==0)exit(0);
while(result!=number1*number2)
{
cout<<"No,please try again.\n";
cin>>result;//为什么我输入0点几,如:0.1程序就会进入无限循环
}
cout<<"Very good!"<<endl;
}
}
最后一个BC问题:
用VC在DOS下编程,往往执行后就会立即退出,怎样才能使之延时退出