I have written these codes since last week. The compiler (microsoft visual c++ 6.0 ) didn't give me any error. However, after I execute the program, then open the output file ( I route the outputs to a file), the output scores are all big negative number (suppose to be positive). I spent a whole week to look for my mistake but couldn't find it. Can anyone compile & run this program and tell me what's my mistake? Thanks all!
Code:
/*
Description: Correct the quiz with a given correct key.
Get input of each student's social security # and their answer of
the quiz. Grade the quiz, then print social security # and student's
score to an output file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define nostu 30
int sfun(char stus[], char k[], int n2);
void sortscr(int s[], int nstu, char ssno[nostu][10], FILE*fout);
main()
{
char k[26] = "FFTFTFTTTFFFTFTFFTFFTTFTT";
char ssn[nostu][10];
char gd[nostu][26];
int n; int s1[30]; int i;
FILE*fout;
fout=fopen("quiz.out","w");
printf("Enter number of students <= %d please!\n",nostu);
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter social security number with no spaces or hyphens\n");
scanf("%s",ssn[i]);
printf("Enter answer\n");
scanf("%s",gd[i]);
}
for(i=0;i<n;i++)
{
s1[i]=sfun(gd[i],k,25);
}
sortscr(s1, n, ssn, fout);
fclose(fout); // close output file
return(0);
}
/* Write Functions */
//Function to calculate score
int sfun(char stus[], char k[], int n2)
{
int i, qr;
for(i=0;i<n2;i++)
{
if(stus[i]==k[i])
qr++;
}
return(qr);
}
//Function to sort score & social #
void sortscr(int s[], int nstu, char ssno[][10], FILE*fout)
{
int t, i , j; char tssno[10];
i=0;
for(j = i+1; j<nstu; j++)
{
if(s[i]<s[j])
{
t=s[i];
s[i]=s[j];
s[j]=t;
//swap social #
strcpy(tssno[i],ssno[i]);
strcpy(ssno[i],ssno[j]);
strcpy(ssno[j],tssno[i]);
}
}
//print output to a file
fprintf(fout,"Social Security # \t Score \n");
for(i=0; i<nstu; i++)
{
fprintf(fout,"%s\t %d\n",ssno[i],s[i]);
}
}