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