0

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.

Community
  • 1
  • 1
Arlington
  • 19
  • 1
  • 5
    Please provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Your code can't be compiled as-is, so it's going to be much harder for people to help you. – Random Davis Jan 24 '17 at 19:37
  • 2
    `department[4]`. Strings in C contain a NUL terminator. So "BINF" requires 5 `chars` in the array to store. – kaylum Jan 24 '17 at 19:39
  • And never use `gets`. It even tells you that in the [gets manual page](https://linux.die.net/man/3/gets). – kaylum Jan 24 '17 at 19:41
  • " I would not like to use arrays at this time". `department[4],name[20],nameH[20]` You do realise these are arrays? – kaylum Jan 24 '17 at 19:43
  • I know the code is mess , but please write question neatly , refer to http://stackoverflow.com/help/how-to-ask – Suraj Jain Jan 24 '17 at 20:03

1 Answers1

0

You Enter as Second Input Mark BINF 600 , Now Department is only have 4 characters including Null Character \0 . So please change char department[4] to char department[5].

Also From This Answer From What does gets() save when it reads just a newline On Working of gets().

This part in the description of gets might be confusing:

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.

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 ,

 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.

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. So You probably do not want that.

Also Somewhere In This Part of Code You Are Probably Dividing By 0.

  avgCEN=CENa/CENc;  
  avgECE=ECEa/ECEc;  
  avgBINF=BINFa/BINFc;  
Community
  • 1
  • 1
Suraj Jain
  • 4,463
  • 28
  • 39