首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

新手!小程序出错

2012-11-07 
新手求助!小程序出错数字小游戏#includestdio.h/*For input and output*/#includectype.h/*For touppe

新手求助!小程序出错
数字小游戏

#include<stdio.h> /* For input and output */
#include<ctype.h> /* For toupper() function */
#include<stdlib.h> /* For rand() and srand() functions */
#include<time.h> /* For timr() function */  
#define TURE 1 /* Define the symbol TURE */
#define FALSE 0 /* Define the symbol FALSE */  
void main()
{  
/* Records if anther game is to be played*/
char another_game ='Y';

/* TURE if corrrct sequence entered,FALSE otherwise*/
  int correct = FALSE;
/* Number of sequence entered successfully*/

  int counter = 0;

int sequence_length=0; /* Number of digits in a sequence */
int i=0; /* Loop conter */ 
long seed=0; /* Seed value for random number sequence */
int number=0; /* Stores an input digit */
long now =0; /* Stores current time -seed for random values */
long time_taken = 0; /* Time taken for game in seconds */



/* Decribe how the game is played */
printf("Welcome to this simple simon game!\n");
printf("Now look at the screen !\nThere is a string of number"
"and try to remember them\n");
printf("The number will disppear 1 second later\n");
printf("Now the game began\n");
printf("\ngood luck!\nPress Enter to play\n");
scanf("%c",&another_game);

/* One outer loop iteration is one game*/
do
{
correct = TURE; /* By default indicates correct sequence entered */
counter = 0; /* Initialize count of number of successful tries */
sequence_length=2; /* Initialize length of a digit sequence */
  time_taken=clock(); /* Record current time at strart of game */
  /* Other code to initialize the game* /

  /* Inner loop continues as long as sequences are entered correctly */
  while(correct)
{
/* On every third successful try, increase the sequence length */
sequence_length += counter++%3 ==0; /*??????????????*/

/* Set seed to be the number of seconds since Jan 1,1970 */
seed=time(NULL);

now=clock(); /* record start time for sequence */

/* Generate a sequence of numbers and display the number */
srand((int)seed); /* Initialize the random digit */
for(i=1;i<=sequence_length;i++)
printf("%d",rand()%10); /* Output a random digit */

/* Wait one second */
for( ;clock()-now<CLOCKS_PER_SEC; );

/* Now overwrite the digit sequence */
  printf("\r"); /* Go to beganning of the line */
  for(i=1;i<=sequence_length;i++)
printf(" "); /* Output two spaces */

if(counter==1) /* Only ouput message for the first try */ 
  printf("\nNow you enter the sequence - don't fotget the spaces\n");
else
printf("\r"); /* Back to the beginning of the line */ 

/* Prompt for the input sequence */

/* Check the input sequence of digits against the original */
srand((int)seed); /* Restart the random sequence */
for(i=1;i<=sequence_length;i++)
{
scanf("%d",number);
if(number!=rand()%10)

correct = FALSE;
break;
}
}
printf("%s\n",correct?"Correct!":"Wrong!");
}

/* Calculate total to play the game in second */
time_taken=(clock()-time_taken)/CLOCKS_PER_SEC;


/*Output the game score */
printf("\n\n Your score is %d",--counter*100/time_taken);

fflush(stdin);

/*Check if a new game is requied*/
printf("Do you want to play again(y/n)?\n");
  scanf("%c",&another_game);
}while(toupper(another_game) =='Y');
}
数字间不出现空格,且只能给出一次随机数 按回车后出现提示 unhandled expection ....0XC00000005字样


[解决办法]
别忘了加上&符号,改为scanf("%d",&number);这样:

C/C++ code
#include<stdio.h> /* For input and output */#include<ctype.h> /* For toupper() function */#include<stdlib.h> /* For rand() and srand() functions */#include<time.h> /* For timr() function */  #define TURE 1 /* Define the symbol TURE */#define FALSE 0 /* Define the symbol FALSE */  void main(){  /* Records if anther game is to be played*/char another_game ='Y';/* TURE if corrrct sequence entered,FALSE otherwise*/  int correct = FALSE;/* Number of sequence entered successfully*/  int counter = 0;int sequence_length=0; /* Number of digits in a sequence */int i=0; /* Loop conter */ long seed=0; /* Seed value for random number sequence */int number=0; /* Stores an input digit */long now =0; /* Stores current time -seed for random values */long time_taken = 0; /* Time taken for game in seconds *//* Decribe how the game is played */printf("Welcome to this simple simon game!\n");printf("Now look at the screen !\nThere is a string of number""and try to remember them\n");printf("The number will disppear 1 second later\n");printf("Now the game began\n");printf("\ngood luck!\nPress Enter to play\n");scanf("%c",&another_game);/* One outer loop iteration is one game*/do{correct = TURE; /* By default indicates correct sequence entered */counter = 0; /* Initialize count of number of successful tries */sequence_length=2; /* Initialize length of a digit sequence */  time_taken=clock(); /* Record current time at strart of game */  /* Other code to initialize the game* /  /* Inner loop continues as long as sequences are entered correctly */  while(correct){/* On every third successful try, increase the sequence length */sequence_length += counter++%3 ==0; /*??????????????*//* Set seed to be the number of seconds since Jan 1,1970 */seed=time(NULL);now=clock(); /* record start time for sequence *//* Generate a sequence of numbers and display the number */srand((int)seed); /* Initialize the random digit */for(i=1;i<=sequence_length;i++)printf("%d",rand()%10); /* Output a random digit *//* Wait one second */for( ;clock()-now<CLOCKS_PER_SEC; );/* Now overwrite the digit sequence */  printf("\r"); /* Go to beganning of the line */  for(i=1;i<=sequence_length;i++)printf(" "); /* Output two spaces */if(counter==1) /* Only ouput message for the first try */   printf("\nNow you enter the sequence - don't fotget the spaces\n");elseprintf("\r"); /* Back to the beginning of the line */ /* Prompt for the input sequence *//* Check the input sequence of digits against the original */srand((int)seed); /* Restart the random sequence */for(i=1;i<=sequence_length;i++){scanf("%d",&number);if(number!=rand()%10){ correct = FALSE;break;}}printf("%s\n",correct?"Correct!":"Wrong!");}/* Calculate total to play the game in second */time_taken=(clock()-time_taken)/CLOCKS_PER_SEC;/*Output the game score */printf("\n\n Your score is %ld",--counter*100/time_taken);fflush(stdin);/*Check if a new game is requied*/printf("Do you want to play again(y/n)?\n");  scanf("%c",&another_game);}while(toupper(another_game) =='Y');} 

热点排行
Bad Request.