发生的情况是在性别搜索功能期间,当它到达输入我希望看到的性别的时间时,它会跳过输入,然后在没有给出原因的情况下崩溃。据我所知,其他功能工作正常(ip ut 在那里,以防错误可能是因为其中一个)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void getinfo (char* nam[],int ag[], char gender[], int count){
int y;
for(y = 0; y < count; y++){
nam[y] = malloc(50);
printf ("What is the student's name?\t");
scanf ("%s", nam[y]);
printf ("\nWhat is the students age?\t");
scanf ("%d", &ag[y]);
printf ("\nwhat is the students gender, Male/Female\t");
scanf (" %c", &gender[y]);
}
}
void findeldest (char* nam[],int ag[], char gender[], int count){
int largest = 0, y, eldest =0 ;
for(y = 0; y < count; y++){
if (ag[y] > eldest){
largest = ag[y];
eldest = y;
}
}
printf ("The eldest student is:\t %s", nam[eldest]);
printf ("\nGender:\t %c", gender[eldest]);
printf ("\nWith an age of:\t %d \n", ag[eldest]);
}
void gendersearch (char* nam[],int ag[], char gender[], int count){
int y;
char choice;
printf ("What gender would you like to see(male-m/female-f)?\n");
scanf ("%c", choice);
switch (choice){
case 'm' : printf ("The male students are as follows:\n");
case 'f' : printf ("The female students are as follows:\n");
default : printf ("please enter m for male or f for female!\n");
}
if (choice == 'm'){
for (y = 0 ; y < count; y++){
if (gender[y] == choice){
printf (nam[y]);
}
}
}else{
for (y = 0 ; y < count; y++){
if (gender[y] == choice){
printf (nam[y]);
}
}
}
}
int main (){
int amount, y;
printf("How many students are you admitting?\t");
scanf ("%d", &amount);
char *name[50];
int age[50];
char gender[50];
if (amount > 50){
printf("Too many students!");
}else{
getinfo(name, age, gender, amount);
findeldest(name, age, gender, amount);
}
gendersearch (name, age, gender, amount);
system("pause");}