Thread: Please help me!

  1. #1
    Asian Pride
    Join Date
    Apr 2004
    Posts
    6

    Question Please help me!

    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]);
    		}
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    In function sfun, you don't initialize qr before using it. Try setting it to 0 before going into the loop. Also, in function sortscr, you treat tssno as an array of strings instead of a string, which is what you declare it as. Try replacing your string swap with this:
    Code:
    strcpy(tssno,ssno[i]);
    strcpy(ssno[i],ssno[j]);
    strcpy(ssno[j],tssno);
    My best code is written with the delete key.

  3. #3
    Asian Pride
    Join Date
    Apr 2004
    Posts
    6
    oh yeah ... thanks a lot!!! It works now What can I do in reward?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What can I do in reward?
    Be happy?
    My best code is written with the delete key.

  5. #5
    Asian Pride
    Join Date
    Apr 2004
    Posts
    6
    Quote Originally Posted by Prelude
    >What can I do in reward?
    Be happy?
    I already am By the way, this is my first time taking a programming course, and C is my first language. You guys are all experts in here. I think I'll visit these cool forums often and asking for more help hehe...

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You guys are all experts in here.
    Well, not everyone. But there are a handful that know an int from a double.

    >this is my first time taking a programming course
    So we'll be seeing more of you.

    >and C is my first language
    You have my condolences. Knowing what I do now, I wouldn't choose C as a first language. C was designed to be used, not taught. It's powerful and flexible, but confusing when first learning it.
    My best code is written with the delete key.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >But there are a handful that know an int from a double.
    Ooh! Ooh! I know! doubles have those point thingies!!! I think they're called decimal points or something...

    >C was designed to be used, not taught.
    I agree. Languages like BASIC and Turing are meant to be an introductory languages.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >doubles have those point thingies!!!
    Yea! Point thingies:
    Code:
    a->b;
    They call it a double because it has two parts, one on each side of the point. This is a single:
    Code:
    a;
    It doesn't have the point or the right hand side, but they call it an int to confuse you. That's why C isn't a good teaching language.
    My best code is written with the delete key.

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    ROFL. That has to be the funniest thing I've seen in a good while.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    how cruel

Popular pages Recent additions subscribe to a feed