c语言初学者关于结构体数据应用的一些问题

请问下面这段代码为什么在第一次输入后就自动结束了

#include<stdio.h>
struct passenger
{
    char name[20];
    long int idnum;
    char gender;
    float weight;
    float luggage;
    char meal;
};
int main()
{
    struct passenger member[300];
    struct passenger *p;
    p=member;
    int flag=0;
    printf("Please follow the prompts to enter passenger information.\n(Please end with the '#' key When you finish typing.)\n\n");
    printf("The notes written in the front:\n");
    printf("1.Rated weight shall not exceed 500kg (Rated weight :sum of passenger weight and baggage weight).\n");
    printf("2.The gender is replaced by the letters 'M' (male) and 'F' (female).\n");
    printf("3.The type of meal is 'A' and 'B'.\n");
    printf("4.The default unit of weight entered is kg.\n\n");
    printf("Please enter the inform ation in the order of name, ID number, gender, weight, baggage weight and type of meal.\n");
    for(;p<member+300;p++)
    {        
        scanf("%s %ld %c %f %f %c",p->name,p->idnum,p->gender,p->weight,p->luggage,p->meal);
        if(((p->weight)+(p->luggage))>500) printf("Warning: Your rated weight has exceeded the upper limit!\n");
        printf("Please enter the information of the next passenger.");
        flag+=1;
        if((p->name[0])='#') break;
    }
    printf("-------------------------------------------------------------------------");
    printf("|No.\tName                    Age\tSex\tWeight     Luggage     Meal\t|\n");
    int i=1,age;
    for(;p<member+flag;p++,i++)
    {
        printf("-------------------------------------------------------------------------");
        age=2020-(p->idnum/100000000)+(p->idnum/10000000)+(p->idnum/1000000)+(p->idnum/100000);
        printf("|%d %-20s% %d %c %-f     %-f     %c |\n",i,p->name,age,p->gender,p->weight,p->luggage,p->meal);
    }
    printf("-------------------------------------------------------------------------");
    return 0;
}

停止条件是 if((p->name[0])='#') break; 你的输入是否符合这个条件?

首先,你第一次输入了啥?这很重要。

输入格式不对?

谢谢大家 我已经找到原因了 1.没加取地址符(之前看书看岔了以为结构体变量不用加) 2.判断语句不小心写成了单等号

这里贴一下改进后的代码

#include#includestruct passenger{ char name[15]; char idnum[18]; char gender; float weight; float luggage; char meal;};int main(){ struct passenger member[300]; struct passenger *p; p=member; int flag=0; printf("Please follow the prompts to enter passenger information.\n(Please end with the '#' key When you finish typing.)\n\n"); printf("The notes written in the front:\n"); printf("1.Rated weight shall not exceed 500kg (Rated weight :sum of passenger weight and baggage weight).\n"); printf("2.The gender is replaced by the letters 'M' (male) and 'F' (female).\n"); printf("3.The type of meal is 'A' and 'B'.\n"); printf("4.The default unit of weight entered is kg.\n\n"); printf("Please enter the inform ation in the order of name, ID number, gender, weight, baggage weight and type of meal.\n\n"); char temp; for(;pname,&p->idnum,&p->gender,&p->weight,&p->luggage,&p->meal); if(((p->weight)+(p->luggage))>500) printf("Warning: Your rated weight has exceeded the upper limit!\n"); flag+=1; system("cls"); printf("Please enter the information of the next passenger.\n\n"); printf("(The input sample: name_example 111111000000001111 M 100 100 A)\n"); printf("(If you want to input the next data please enter after hitting 'ENTER')\n"); printf("(If you want to directly finish typing, type '#')\n"); getchar(); temp=getchar(); if(temp=='#') { system("cls"); break; } } printf("-----------------------------------------------------------------------------\n"); printf("| No.\tName Age\tSex\tWeight Luggage Meal |\n"); int i=1,j,k=0; int age[300]; char temp_age[4]; for(p=member;pidnum[j+6]; } age[k]=2020-(temp_age[0]-'0')*1000-(temp_age[1]-'0')*100-(temp_age[2]-'0')*10-(temp_age[3]-'0'); printf("| %-6d%-24s%-8d%-8c%-11.2f%-12.2f%c |\n",i,p->name,age[k],p->gender,p->weight,p->luggage,p->meal); k+=1; } printf("-----------------------------------------------------------------------------\n\n"); system("pause"); system("cls"); printf("Please select the sort you want to print:\n"); printf("1.Rank passengers by age from youngest to oldest.\n"); printf("2.Rank passengers by rated weight from smallest to largest.\n\n"); int a; scanf("%d",&a); system("cls"); switch(a) { case 1: { struct passenger *p1; struct passenger temp_1; int temp_2; i=0; j=1; for(p=member;page[j]) { temp_1=*p1; *p1=*p; *p=temp_1; temp_2=age[j]; age[j]=age[i]; age[i]=temp_2; } } k=0; i=1; printf("-----------------------------------------------------------------------------\n"); for(p=member;pname,age[k],p->gender,p->weight,p->luggage,p->meal); printf("-----------------------------------------------------------------------------\n"); } }break; case 2: { struct passenger *p1; struct passenger temp_1; int temp_2; for(p=member;pweight)+(p->luggage))>((p1->weight)+(p1->luggage))) { temp_1=*p1; *p1=*p; *p=temp_1; temp_2=age[j]; age[j]=age[i]; age[i]=temp_2; } } k=0; i=1; printf("-----------------------------------------------------------------------------\n"); for(p=member;pname,age[k],p->gender,p->weight,p->luggage,p->meal); printf("-----------------------------------------------------------------------------\n"); } } } system("pause"); system("cls"); char b; int flag_m=0,flag_f=0; while(1) { printf("Please enter the service you need.\n"); printf("'1':Count the number of male and female passengers separately.\n"); printf("'2':Count the number of minors.\n"); printf("'3':Enter 'A' or 'B' to count the number of people who have chosen meal A or B after you entered '3'.\n"); printf("'#':No extra statistics required,exit this program directly.\n\n"); getchar(); b=getchar(); system("cls"); switch(b) { case '1': { printf("-----------------------------------------------------------------------------\n\n"); for(p=member;pgender)=='M') flag_m+=1; if((p->gender)=='f') flag_f+=1; } printf("The number of male passengers is %d\nThe number of female passengers is %d.\n\n",flag_m,flag_f); printf("-----------------------------------------------------------------------------\n"); }break; case '2': { int flag_minor=0; printf("-----------------------------------------------------------------------------\n\n"); for(p=member,j=0;pmeal)=='A') { flag_a+=1; } } printf("The total number of passengers choosing meal A is %d.\n\n",flag_a); printf("-----------------------------------------------------------------------------\n"); }break; case 'B': { int flag_b=0; printf("-----------------------------------------------------------------------------\n\n"); for(p=member;pmeal)=='B') { flag_b+=1; } } printf("The total number of passengers choosing meal B is %d.\n\n",flag_b); printf("-----------------------------------------------------------------------------\n"); } } }break; case '#': { exit(0); } default: { printf("Warning, please enter the correct service code!\n\nPlease re-select the service you need.\n\n"); system("pause"); system("cls"); } } } return 0;}//ceshi_1 510402200101112211 M 90 100 A//ceshi_2 30311120000312231x F 60 200 B//ceshi_3 301234200303114351 M 70 450 B

#include<stdio.h>
#include<windows.h>
struct passenger
{
	char name[15];
	char idnum[18];
	char gender;
	float weight;
	float luggage;
	char meal;
};
int main()
{
	struct passenger member[300];
	struct passenger *p;
	p=member;
	int flag=0;
	printf("Please follow the prompts to enter passenger information.\n(Please end with the '#' key When you finish typing.)\n\n");
	printf("The notes written in the front:\n");
	printf("1.Rated weight shall not exceed 500kg (Rated weight :sum of passenger weight and baggage weight).\n");
	printf("2.The gender is replaced by the letters 'M' (male) and 'F' (female).\n");
	printf("3.The type of meal is 'A' and 'B'.\n");
	printf("4.The default unit of weight entered is kg.\n\n");
	printf("Please enter the inform ation in the order of name, ID number, gender, weight, baggage weight and type of meal.\n\n");
	char temp;
	for(;p<member+300;p++)
	{		
		scanf("%s %s %c %f %f %c",&p->name,&p->idnum,&p->gender,&p->weight,&p->luggage,&p->meal);
		if(((p->weight)+(p->luggage))>500) printf("Warning: Your rated weight has exceeded the upper limit!\n");
		flag+=1;
		system("cls");
		printf("Please enter the information of the next passenger.\n\n");
		printf("(The input sample: name_example 111111000000001111 M 100 100 A)\n");
		printf("(If you want to input the next data please enter after hitting 'ENTER')\n");
		printf("(If you want to directly finish typing, type '#')\n");
		getchar();
		temp=getchar(); 
		if(temp=='#') 
		{
			system("cls");
			break;
		}
	}
	printf("-----------------------------------------------------------------------------\n");
	printf("| No.\tName                    Age\tSex\tWeight     Luggage     Meal |\n");
	int i=1,j,k=0;
	int age[300];
	char temp_age[4];
	for(p=member;p<member+flag;p++,i++)
	{
		printf("-----------------------------------------------------------------------------\n");
		for(j=0;j<4;j++)
		{
			temp_age[j]=p->idnum[j+6];
		}
		age[k]=2020-(temp_age[0]-'0')*1000-(temp_age[1]-'0')*100-(temp_age[2]-'0')*10-(temp_age[3]-'0'); 			
		printf("| %-6d%-24s%-8d%-8c%-11.2f%-12.2f%c    |\n",i,p->name,age[k],p->gender,p->weight,p->luggage,p->meal);
		k+=1;
	}	
	printf("-----------------------------------------------------------------------------\n\n");
	system("pause");
	system("cls");
	printf("Please select the sort you want to print:\n");
	printf("1.Rank passengers by age from youngest to oldest.\n");
	printf("2.Rank passengers by rated weight from smallest to largest.\n\n");
	int a;
	scanf("%d",&a);
	system("cls");
	switch(a)
	{
		case 1:
		{
			struct passenger *p1;
			struct passenger temp_1;
			int temp_2;
			i=0;
			j=1;
			for(p=member;p<member+flag-1;p++,i++)
				for(p1=p+1;p1<member+flag;p1++,j++)
				{					
					if(age[i]>age[j])
					{
						temp_1=*p1;
						*p1=*p;
						*p=temp_1;
						temp_2=age[j];
						age[j]=age[i];
						age[i]=temp_2;
					}
				}
			k=0;
			i=1;
			printf("-----------------------------------------------------------------------------\n");
			for(p=member;p<member+flag;p++,k++,i++)
			{
				printf("| %-6d%-24s%-8d%-8c%-11.2f%-12.2f%c    |\n",i,p->name,age[k],p->gender,p->weight,p->luggage,p->meal);
				printf("-----------------------------------------------------------------------------\n");
			}
		}break;
		case 2:
		{
			struct passenger *p1;
			struct passenger temp_1;
			int temp_2;			
			for(p=member;p<member+flag-1;p++,i++)
				for(p1=p+1;p1<member+flag;p1++,j++)
				{
					if(((p->weight)+(p->luggage))>((p1->weight)+(p1->luggage)))
					{
						temp_1=*p1;
						*p1=*p;
						*p=temp_1;
						temp_2=age[j];
						age[j]=age[i];
						age[i]=temp_2;
					}
				}
			k=0;
			i=1;
			printf("-----------------------------------------------------------------------------\n");			
			for(p=member;p<member+flag;p++,k++,i++)
			{
				printf("| %-6d%-24s%-8d%-8c%-11.2f%-12.2f%c    |\n",i,p->name,age[k],p->gender,p->weight,p->luggage,p->meal);
				printf("-----------------------------------------------------------------------------\n");
			}
		}
	}
	system("pause");
	system("cls");
	char b;
	int flag_m=0,flag_f=0;	
	while(1)
	{
		printf("Please enter the service you need.\n");
		printf("'1':Count the number of male and female passengers separately.\n");
		printf("'2':Count the number of minors.\n");
		printf("'3':Enter 'A' or 'B' to count the number of people who have chosen meal A or B after you entered '3'.\n");
		printf("'#':No extra statistics required,exit this program directly.\n\n");
		getchar();
   		b=getchar();
   		system("cls");
		switch(b)
		{
			case '1':
			{
				printf("-----------------------------------------------------------------------------\n\n");
				for(p=member;p<member+flag;p++)
				{
					if((p->gender)=='M') flag_m+=1;
					if((p->gender)=='f') flag_f+=1;
				}
				printf("The number of male passengers is %d\nThe number of female passengers is %d.\n\n",flag_m,flag_f);
				printf("-----------------------------------------------------------------------------\n");
			}break;	
			case '2':
			{
				int flag_minor=0;
				printf("-----------------------------------------------------------------------------\n\n");
				for(p=member,j=0;p<member+flag;p++,j++)
				{
					if(age[j]<18)
					{
						flag_minor+=1;	
					}
				}
				printf("The total number of underage passengers is %d.\n\n",flag_minor);
				printf("-----------------------------------------------------------------------------\n");
			}break;
			case '3':
			{
				printf("Please enter the type of meal('A' or 'B') you want to count.\n\n");			
				char c;
				getchar(); 
				c=getchar();
				switch(c)
				{
					case 'A':
					{
						int flag_a=0;
						printf("-----------------------------------------------------------------------------\n\n");
						for(p=member;p<member+flag;p++)
						{
							if((p->meal)=='A')
							{
								flag_a+=1;	
							}
						}
						printf("The total number of passengers choosing meal A is %d.\n\n",flag_a);
						printf("-----------------------------------------------------------------------------\n");
					}break;
					case 'B':
					{
						int flag_b=0;
						printf("-----------------------------------------------------------------------------\n\n");
						for(p=member;p<member+flag;p++)
						{
							if((p->meal)=='B')
							{
								flag_b+=1;
							}
						}
						printf("The total number of passengers choosing meal B is %d.\n\n",flag_b);
						printf("-----------------------------------------------------------------------------\n");
					}	
				}
			}break;
			case '#':
			{
				exit(0);
			}
			default:
			{
				printf("Warning, please enter the correct service code!\n\nPlease re-select the service you need.\n\n");
				system("pause");
				system("cls");
			}
		}
	}
	return 0;
}
//ceshi_1 510402200101112211 M 90 100 A
//ceshi_2 30311120000312231x F 60 200 B
//ceshi_3 301234200303114351 M 70 450 B