简体   繁体   English

程序崩溃?

[英]Program Crashes?

I'm new in programming and I picked up this as a hobby. 我是编程的新手,我将其作为爱好。 So I started solving an exercise but my program crashes unexpectedly. 因此,我开始练习,但是程序意外崩溃。
This is the exercise: 这是练习:

The Sensitive Society Club organizes a donation campaign. 敏感社会俱乐部组织了一次捐赠运动。 To put more fun in the campaign, they organize a competition among departments. 为了使活动更加有趣,他们组织了部门之间的竞赛。 In the competition top donating students and the department are awarded with a certificate. 在比赛中,最优秀的捐赠学生和系将获得证书。

Question: Write a program that reads the information for n students then it shows the name of the student who donated the most amount and the department whose average is the highest. 问题:编写一个程序,读取n个学生的信息,然后显示捐赠最多的学生的姓名和平均值最高的部门。

Input specification: You will be given one integer (n) in the beginning. 输入规范:您将在开头输入一个整数(n)。 Then, the following n lines will have three information: 然后,以下n行将具有三个信息:

Student name: names are at most 20 char strings containing only 26 English (uppercase or lowercase) letters his/her department: at most 4 chars long string (only 3 departments in the competition: CEN, ECE or BINF) 学生姓名:名称最多为20个字符字符串,仅包含26个英语(大写或小写)字母所属部门:最多4个字符长字符串(竞赛中只有3个部门:CEN,ECE或BINF)

Output specification : Show two strings : 输出规范 :显示两个字符串:
The name of the student who donated the most, 捐款最多的学生的名字,
The department whose average is the highest. 平均值最高的部门。

int n,i,CENc=0,ECEc=0,BINFc=0,CENa=0,ECEa=0,BINFa=0,amountS,amountH=0,avgCEN,avgECE,avgBINF;
char department[4],name[20],nameH[20];
scanf ("%d",&n);
for (i=0;i<n;i++);{
    gets (name);
    gets (department);
    scanf("%d",&amountS);
    if (strcmp(department,"ECE")==0){
        ECEa=ECEa+amountS;
        ECEc++;
    }
     else if (strcmp(department,"CEN")==0){
        CENa=CENa+amountS;
        CENc++;
    }
     else if (strcmp(department,"BINF")==0){
        BINFa=BINFa+amountS;
        BINFc++;
    }
     if (amountS>amountH){
            amountH=amountS;
            strcpy(nameH,name);
     }

}
avgCEN=CENa/CENc;
avgECE=ECEa/ECEc;
avgBINF=BINFa/BINFc;
 if (avgCEN>avgECE && avgCEN>avgBINF){
    printf("%d", avgCEN);
    printf("%s", nameH);
 }
 else if (avgECE>avgCEN && avgECE>avgBINF){
    printf("%d", avgECE);
    printf("%s", nameH);
 }
 else if (avgBINF>avgCEN && avgBINF>avgECE){
    printf("%d", avgBINF);
    printf("%s", nameH);
 }
  return 0

So i know it's a little badly done but here's the problem. 所以我知道这做得不好,但这是问题所在。
I enter the first and second line of inputs Johnny CEN 500 Mark BINF 600 and the program crashes after that. 我输入第一行和第二行输入Johnny CEN 500 Mark BINF 600 ,然后程序崩溃。
Any thoughts on why that happens? 关于为什么会发生任何想法? Note: I would not like to use arrays at this time as I am learning the basics and moving on with time to more complicated stuff. 注意:由于我正在学习基础知识并随着时间的流逝而转向更复杂的内容,因此我目前不希望使用数组。

You Enter as Second Input Mark BINF 600 , Now Department is only have 4 characters including Null Character \\0 . 您输入作为第二输入Mark BINF 600 ,现在Department只有4个字符,包括Null Character \\0 So please change char department[4] to char department[5] . 因此,请将char department[4]更改为char department[5]

Also From This Answer From What does gets() save when it reads just a newline On Working of gets() . 也是从这个回答是什么gets()函数保存时,它读取只是一个换行符在工作中gets()

This part in the description of gets might be confusing: gets描述中的这一部分可能会造成混淆:

It takes all the characters up to (but not including) the newline 它需要所有字符到(但不包括)换行符

It might be better to say that it takes all the characters including the newline but stores all characters not including the newline. 最好说它需要 包括换行符在内的所有字符,但要存储 不包括换行符在内的所有字符。

So if the user enters some string , the gets function will read some string and the newline character from the user's terminal, but store only some string in the buffer - the newline character is lost. 因此,如果用户输入some string ,则gets函数将从用户终端读取some string和换行符,但仅将some string存储在缓冲区中-换行符会丢失。 This is good, because no one wants the newline character anyway - it's a control character, not a part of the data that user wanted to enter. 这很好,因为无论如何都不需要换行符-它是控制字符,而不是用户想要输入的数据的一部分。

Therefore, if you only press enter , gets interprets it as an empty string. 因此,如果仅按Enter ,则gets会将其解释为空字符串。

And So On The Start on your line scanf ("%d",&n) , when you type the input and press enter , the new line character is stored in the buffer so when the execution arrives on gets (name) , gets read the new line character and interpret the string as empty and move forward , so to digest that new line character you could well do this , 因此,在行scanf ("%d",&n) ,当您键入输入并按Enter时,新行字符存储在缓冲区中,因此当执行执行时, gets (name) gets读取换行符并将字符串解释为空并向前移动,因此要消化该换行符,您可以这样做,

 char a ;
 scanf("%d" , &n);
 scanf("%c" , &a);

So the new line character that is present in buffer will get stored in a , and will not be read by gets . 所以这是出现在缓冲区中的新行字符将获得存储在,并不会阅读gets

Also If you are giving input on a line like Johnny CEN 500 , then gets() is probably storing all of it in a single string , because it does count white space. 另外,如果您在像Johnny CEN 500这样的行上进行输入,则gets()可能会将所有内容存储在单个字符串中,因为它确实会占用空格。 So You probably do not want that. 因此,您可能不希望那样。

Also Somewhere In This Part of Code You Are Probably Dividing By 0 . 在这部分代码的某个地方,您可能被0

  avgCEN=CENa/CENc;  
  avgECE=ECEa/ECEc;  
  avgBINF=BINFa/BINFc;  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM