C 查找最年长年龄时代码崩溃(编辑)

C 查找最年长年龄时代码崩溃(编辑),c,crash,C,Crash,好的,所以我把它搞定了,然后我做了一些建议,我得到了同样的错误(顺便说一句,我使用codebloks),错误是它只是在没有给出原因的情况下崩溃了。我又犯了一个错误。在getinfo函数中,输入年龄后,它打印语句以获取性别,然后打印语句以获取其他人的姓名,而不让我输入(似乎只是跳过了这一部分) #include <stdio.h> #include <string.h> #include <stdlib.h> void getinfo (char* nam[]

好的,所以我把它搞定了,然后我做了一些建议,我得到了同样的错误(顺便说一句,我使用codebloks),错误是它只是在没有给出原因的情况下崩溃了。我又犯了一个错误。在getinfo函数中,输入年龄后,它打印语句以获取性别,然后打印语句以获取其他人的姓名,而不让我输入(似乎只是跳过了这一部分)

#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(30);
 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, M/F:\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", ag[eldest]);

}


int main (){
  int amount, y;
  printf("How many students are you admitting?\t");
  scanf ("%d", &amount);
  if (amount > 50){
  printf("Too many students!");
   }else{
   char *name[50];
   int age[50];
   char gender[50];
   getinfo(name, age, gender, amount);
   findeldest(name, age, gender, amount);
   system("pause");
  }
}
#包括
#包括
#包括
void getinfo(char*nam[],int-ag[],char-gender[],int-count){
int-y;
对于(y=0;y最年长者){
最大值=ag[y];
最年长的=y;
}
}
printf(“最年长的学生是:\t%s”,nam[eldest]);
printf(“\n姓名:\t%c”,性别[最年长]);
printf(“\n年龄为:\t%d”,ag[最年长]);
}
int main(){
整数金额,y;
printf(“你录取了多少学生?\t”);
scanf(“%d”和金额);
如果(金额>50){
printf(“学生太多了!”);
}否则{
字符*名称[50];
国际年龄[50];
性别[50];
getinfo(姓名、年龄、性别、金额);
findeldest(姓名、年龄、性别、金额);
系统(“暂停”);
}
}

提供的代码不可编译,因此我们无法正确诊断问题。请使用可编译的测试用例更新您的帖子,该测试用例仅包含重现问题所需的基本要素

if (ag[y] > eldest){
    largest = ag[y];
    eldest = y;
    } /* <--- Note the inconsistent indentation.
       *      Do you want us to read your code and help you?
       *      If so, please fix your indentation.
       */
printf(“\nGender:\t%c”,gender[eldest]);
如果eldest存储年龄,而不是索引,那么当
eldest>=50
时,这不会导致程序崩溃吗?我想,当您找到更好的标识符时,这句话会更有意义。此外,
gender[eldest]
是一个
字符*
,其中
%c
告诉printf需要一个带有字符值的
int


编辑:如果在使用scanf之前阅读过,您就不会遇到第二个错误。事实上,在阅读其文档之前使用scanf是您的错误,请停止

如果将
printf(“为性别输入的字符有一个整数值%d\n”,(无符号字符)性别[y]);
放在
scanf(“%c”,&gender[y]);
之后,您会得到什么值?“\n”是有效的性别吗

…因此,您已经修复了当用户在整数读取和字符读取之间按enter键时出现的问题。太好了!在未阅读手册的情况下,您遇到了第三个问题。假设用户按'M'或'F',然后按enter键。这将导致通过
scanf(%c',&gender[y])从stdin中读取'M'或'F';
,但“\n”将保留在stdin上。这有什么影响?当循环重复,并且您告诉scanf读取到但不包括下一个空格字符(即“%s”所指示的字符)时,您将得到一个空名称!oops

假设您希望用户在输入性别前后按enter键,我建议将
scanf(“%c”,&gender[y]);
更改为:

int c;
for (c = getchar(); c >= 0 && c != '\n'; c = getchar());  /* Discard the remainder of the line,
                                                           * so that it doesn't interfere with
                                                           * the gender input.
                                                           */
gender[y] = c;
for (c = getchar(); c >= 0 && c != '\n'; c = getchar());  /* Discard the remainder of the line,
                                                           * so that it doesn't interfere with
                                                           * the next scanf call.
                                                           */

…别忘了仔细阅读scanf手册!

提供的代码不可编译,因此我们无法正确诊断问题。请使用可编译的测试用例更新您的帖子,该测试用例仅包含重现问题所需的基本要素

if (ag[y] > eldest){
    largest = ag[y];
    eldest = y;
    } /* <--- Note the inconsistent indentation.
       *      Do you want us to read your code and help you?
       *      If so, please fix your indentation.
       */
printf(“\nGender:\t%c”,gender[eldest]);
如果eldest存储年龄,而不是索引,那么当
eldest>=50
时,这不会导致程序崩溃吗?我想,当您找到更好的标识符时,这句话会更有意义。此外,
gender[eldest]
是一个
字符*
,其中
%c
告诉printf需要一个带有字符值的
int


编辑:如果在使用scanf之前阅读过,您就不会遇到第二个错误。事实上,在阅读其文档之前使用scanf是您的错误,请停止

如果将
printf(“为性别输入的字符有一个整数值%d\n”,(无符号字符)性别[y]);
放在
scanf(“%c”,&gender[y]);
之后,您会得到什么值?“\n”是有效的性别吗

…因此,您已经修复了当用户在整数读取和字符读取之间按enter键时出现的问题。太好了!在未阅读手册的情况下,您遇到了第三个问题。假设用户按'M'或'F',然后按enter键。这将导致通过
scanf(%c',&gender[y])从stdin中读取'M'或'F';
,但“\n”将保留在stdin上。这有什么影响?当循环重复,并且您告诉scanf读取到但不包括下一个空格字符(即“%s”所指示的字符)时,您将得到一个空名称!oops

假设您希望用户在输入性别前后按enter键,我建议将
scanf(“%c”,&gender[y]);
更改为:

int c;
for (c = getchar(); c >= 0 && c != '\n'; c = getchar());  /* Discard the remainder of the line,
                                                           * so that it doesn't interfere with
                                                           * the gender input.
                                                           */
gender[y] = c;
for (c = getchar(); c >= 0 && c != '\n'; c = getchar());  /* Discard the remainder of the line,
                                                           * so that it doesn't interfere with
                                                           * the next scanf call.
                                                           */

…别忘了仔细阅读scanf手册!

您没有发布getinfo函数的源代码,所以我们在这里能做的最好的事情就是猜测。我首先要看的是getinfo,当它填充'nam'和'gender'参数时。您最有可能使用堆栈本地字符串。一旦'getinfo'函数但是,指向堆栈上这些位置的指针仍然存在,并且每当findeldest试图在printf中使用这些指针时,它都会从堆栈中打印垃圾

如果您提供“getinfo”实现,这将更容易。这是我的实现

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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%s", gender[eldest]);
  printf ("\nWith an age of:\t%d", ag[eldest]);

}

void getinfo(char* nam[],int ag[], char* gender[], int count)
{
    static const char * const MALE = "male";
    static const char * const FEMALE = "female";

    static const char * const BRIAN = "brian";
    static const char * const SUE = "sue";
    static const char * const DAVE = "dave";
    static const char * const APRIL ="april";



    for(int i = 0 ; i < count ; ++i)
    {
        ag[i] = i;

        switch(i%4)
        {
            case 0:
                nam[i] = BRIAN;
                break;
            case 1:
                nam[i] = SUE;
                break;
            case 2:
                nam[i] = DAVE;
                break;
            case 3:
                nam[i] = APRIL; 
                break;

        }

        switch(i%2)
        {
            case 0:
                gender[i] = MALE;
                break;
            case 1:
                gender[i] = FEMALE;
                break;
        }

    }
    return;
}

int main (){
  int amount, y;
  printf("How many students are you admitting?\t");
  scanf ("%d", &amount);
  if (amount > 50){
    printf("Too many students!");
  } else {
    char *name[50];
    int age[50];
    char *gender[50];
    getinfo(name, age, gender, amount);
    findeldest(name, age, gender, amount);
    system("pause");
  }
}
#包括
#包括
#包括
void findeldest(char*nam[],int ag[],char*gender[],int count){
int最大值=0,y,最大值=0;
为了(