gpt4 book ai didi

c - 程序崩溃?

转载 作者:行者123 更新时间:2023-11-30 16:51:16 24 4
gpt4 key购买 nike

我是编程新手,我把它作为一种爱好。所以我开始解决一个练习,但我的程序意外崩溃了。
这是练习:

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.

Input specification: You will be given one integer (n) in the beginning. Then, the following n lines will have three information:

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)

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

所以我知道这做得有点糟糕,但这就是问题所在。
我输入第一行和第二行 Johnny CEN 500 Mark BINF 600 然后程序崩溃了。
对于为什么会发生这种情况有什么想法吗?注意:我现在不想使用数组,因为我正在学习基础知识并随着时间的推移继续学习更复杂的东西。

最佳答案

您作为第二个输入输入 Mark BINF 600 ,现在部门只有 4 个字符,包括空字符 \0 。所以请更改char department[4]char department[5] .

也来自 What does gets() save when it reads just a newline 的回答关于gets()的工作.

这部分在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. 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.

等一下您的线路就开始了scanf ("%d",&n) ,当您键入输入并按 Enter 时,新行字符将存储在缓冲区中,因此当执行到达 gets (name) 时, gets读取新行字符并将字符串解释为空并向前移动,因此要消化该新行字符,您可以这样做,

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

因此缓冲区中存在的新行字符将存储在 a 中,并且不会被 gets 读取。 。

此外,如果您在类似 Johnny CEN 500 的行上进行输入,然后gets()可能将所有内容存储在一个字符串中,因为它确实计算空白。所以你可能不希望这样。

在这部分代码中,您可能除以 0 .

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

关于c - 程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41837428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com