文件无缘无故

时间:2013-03-09 21:14:17

标签: c crash

在gendersearch函数期间发生的事情是当它到达时间进入哪个性别我想看到它跳过输入然后只是崩溃而没有给出理由。其他功能从我能告诉的方面运行良好(如果错误可能是因为其中一个错误的话,请在那里使用它)

#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");}

1 个答案:

答案 0 :(得分:2)

您正在使用scanf而不使用&amp;在void gendersearch ()

 scanf ("%c", choice);


改为:

 scanf ("%c", &choice);

Scanf需要变量的地址而不是它的值。