我遇到了麻烦。我的程序似乎在 DEV C++ 上运行良好,但在 Xcode 上,最后一个 For 循环不知道何时停止。有什么帮助吗?
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
void strip_newline(char *str,int size)
{
int i;
for(i=0;i<size;++i)
{
if(str[i]=='\n')
{
str[i]='\0';
return;
}
}
}
int main()
{
int randomnumber;
int max;
int tall;
char name[40][tall];
char seat[40][tall];
int count;
int currentcount;
int flag;
srand( time(NULL) );
printf("Enter total number of students: ");
scanf("%d",&max);
getchar();
tall=max+1;
randomnumber=rand()% max +1;
printf("This is your random number\n %d \n",randomnumber);
printf("Enter your students names and press enter after each name:\n ");
fgets(name[0],40,stdin);
strip_newline(name[0],40);
for(count=1; count < max; count++ )
{
printf("Please enter next name\n ");
fgets(name[count],40,stdin);
strip_newline(name[count],40);
}
count=-1;
do {
randomnumber=rand()% max;
flag=0;
for(currentcount=0; currentcount<max; currentcount++)
{
if(strcmp(name[randomnumber],seat[currentcount])==0)
{
flag=1;
}
else
{
}
}
if(flag==0)
{
strcpy(seat[count],name[randomnumber]);
count++;
}
else
{
}
}
while (count != max);
for(count=0; count < max; count++)
{
printf("%s sits in seat %d\n",seat[count],count+1);
}
getchar();
return 0;
}