本文介绍了发现最年长的年龄时,code崩溃(编辑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定这样iffixed了起来,我这presentable做了一些建议,我也得到了同样的错误(使用codebloks BTW IM),它只是崩溃没有给一个原因的错误。我得到了第二个错误与此有关。中输入岁以后是getinfo功能,其打印,以获得性再得到其他人的名字,没有让我输入的语句后面的语句(它似乎只是跳过这一部分。

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;无效程序getinfo(字符*南[],INT公司[],CHAR性别[],诠释计数){
 诠释Ÿ;
 对于(Y = 0; Y<计数; Y ++){
 南[Y] =的malloc(30);
 的printf(什么是学生的姓名\\ t?);
 scanf函数(%S,&安培;南[Y]);
 的printf(\\ nWhat是学生的年龄\\ t?);
 scanf函数(%d个,&安培;股份公司[Y]);
 的printf(\\ nwhat是学生性别,M / F:\\ t的);
 scanf函数(%C,&安培;性别[Y]);
  }
 }无效findeldest(字符*南[],INT公司[]的char *性别[],诠释计数){
 诠释最大= 0,Y,长子= 0;
 对于(Y = 0; Y<计数; Y ++){
    如果(AG [Y]>长子){
        最大= AG [Y];
        长子= Y;
        }
    }
    的printf(大小姐学生:\\ t%S,南安[大]);
    的printf(\\ nGender:\\ T%C,性别[大]);
    的printf(\\ n使用的年龄:\\ t%D,AG [大]);}
诠释主(){
  INT金额,Y;
  的printf(有多少学生被你承认\\ t?);
  scanf函数(%d个,&安培;金额);
  如果(量大于50){
  的printf(学生太多了!);
   }其他{
   字符*名称[50];
   INT年龄[50];
   焦炭性别[50];
   程序getinfo(姓名,年龄,性别,量);
   findeldest(姓名,年龄,性别,量);
   系统(暂停);
  }
}


解决方案

提供的code不是编译,因此,我们不能正确地诊断问题。请更新只具有重现您的问题所需要的最基本的一个编译测试用例您的文章。

 如果(AG [Y]>长子){
    最大= AG [Y];
    长子= Y;
    } / *< ---注意不一致的缩进。
       *难道要我们读你的code和帮助你?
       *如果是这样,请修复您的压痕。
       * /

有一个比较假想上的年龄,然后索引的分配到一个年龄变量?这确实大店?索引或年龄?任你选,并使其保持一致。也许这可能是一个更好的主意,多采用描述性标识,如eldest_index和eldest_age。这将提供的内部文件的eldest_index是最早的人与eldest_age数组的索引是年龄最老的人。因为它似乎年龄最大的卖场,也许你的code应该看起来更像是:

 如果(AG [Y]>最大){
    最大= AG [Y];
    长子= Y;
}

的printf(\\ nGender:\\ T%C,性别[大]); 如果大店的青睐,而不是索引,以后就不会这会导致程序崩溃时长子> = 50 ?我presume这句话会更有意义,当你拿出更好的标识符。此外,性别[大] 的char * ,其中%C 告诉printf的期望的 INT 用字符值。


修改的:你不会遇到了你的第二个错误,如果你读过的。事实上,使用scanf函数读它的文档的之前您的错误;请停下来。

如果您将的printf(性别的输入的字符有%d \\ n的整数值(无符号字符)性别[Y]); scanf函数(%C,&安培;性别[Y]); ,你得到了什么价值?是的'\\ n'一个有效的性别?

...所以你固定在用户presses之间输入时出现的问题的一个的整数的阅读和的字符的读取。大!如果没有阅读本手册,你有一个第三个问题。假设用户presses'M'或'F',依次输入。这将导致无论是'M'或'F'由从标准输入读取scanf函数(%C,&安培;性别[Y]); ,但'\\ N'将留在标准输入。是什么造成的效果?当循环重复,你告诉scanf函数读取的,但不包括下一个空格字符的(这是'%s'的指示),你结束了一个空白的名字!哎呀!

假设你指望用户preSS的输入的前后进入性别后,我建议改变 scanf函数(%C,&安培;性别[Y]) ; 来:

  INT℃;
为(C =的getchar(); C> = 0&放大器;&安培;!C ='\\ n'; C =的getchar()); / *丢弃该行的其余部分,
                                                           *,使其不干扰
                                                           *性别输入。
                                                           * /
性别[Y] = C;
为(C =的getchar(); C> = 0&放大器;&安培;!C ='\\ n'; C =的getchar()); / *丢弃该行的其余部分,
                                                           *,使其不干扰
                                                           *下一scanf函数调用。
                                                           * /

...不要忘了阅读scanf函数手册,仔细的!

ok so iffixed it up and me it presentable did some of the suggestions and i get the same error( im using codebloks btw ), the error of it just crashing without giving a reason. I got a second error with this. in the getinfo function after age is entered it prints the statement to get the gender then the statement to get the other persons name without letting me input(it seems to just skip that part

#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");
  }
}
解决方案

The code provided isn't compilable and thus we can't diagnose the problem correctly. Please update your post with a compilable testcase that has only the bare essentials required to reproduce your problem.

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.
       */

There's a comparison supposedly on ages, and then an assignment of an index to an age variable? Which does eldest store? Indexes, or ages? Take your pick, and make it consistent. Perhaps it might be a better idea to use more descriptive identifiers, such as "eldest_index" and "eldest_age". This would provide internal documentation that the "eldest_index" is the array index for the oldest person and the "eldest_age" is the age for the oldest person. As it seems largest stores ages, perhaps your code should look more like:

if (ag[y] > largest){
    largest = ag[y];
    eldest = y;
}

printf ("\nGender:\t%c", gender[eldest]); If eldest stores ages, not indexes, then wouldn't this cause your program to crash when eldest >= 50? I presume this statement will make more sense when you come up with better identifiers. Furthermore, gender[eldest] is a char *, where %c tells printf to expect an int with a character value.


edit: You wouldn't have encountered your second error if you had read this scanf manual before using scanf. In fact, using scanf before reading it's documentation is your error; Please stop.

If you place printf("The character entered for gender has an integer value of %d\n", (unsigned char) gender[y]); after scanf ("%c", &gender[y]);, what value do you get? Is '\n' a valid gender?

... so you've fixed the problem that occurs when the user presses enter between an integer read and a character read. Great! Without reading the manual, you've got a third problem. Supposing the user presses 'M' or 'F', followed by enter. This would result in either 'M' or 'F' being read from stdin by scanf ("%c", &gender[y]);, but the '\n' would remain on stdin. What is the effect of that? When the loop repeats, and you tell scanf to read up to but not including the next whitespace character (which is what '%s' indicates), you end up with a blank name! Ooops!

Assuming you expect the user to press enter before and after entering gender, I suggest changing scanf ("%c", &gender[y]); to:

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.
                                                           */

... and don't forget to read that scanf manual, carefully!

这篇关于发现最年长的年龄时,code崩溃(编辑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 12:50