Thread: Issue with "Grading" Program

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Exclamation Issue with "Grading" Program

    I am to create a program that takes data from an input file and assigns a grade basd on averages. The program works great and everything, but I have one issue...

    Here is my code:

    Code:
    /* MAE 9 Final Assignment */
    
    #include <stdio.h>
    #include <math.h>
    
    void prob1(void);
    void prob2(void);
    
    
    main()
    {
            int menu;
            printf("Enter The Function Number To Execute (1-2): ");
            scanf("%d", &menu);
            switch(menu){
             case 1:
                prob1();
                break;
             default:
                printf("prob%d() does not exist.\n", menu);
            }
            exit(0);
    }
    void prob1(void)
    {
    	FILE *infile, *outfile;
    	char text[81], ch_grade; 
    	double average;
    	int lines, k, score [5], i_ave, name[30], last_name[30];
    	infile = fopen("score.dat", "r");
    	outfile = fopen("grades.dat", "w");
    	if(infile == NULL || outfile == NULL){
    		printf("***File Opening Error!***\n");
    		exit(101);
    }
    lines=0;
    while(fgets(text,81,infile)!=NULL){
    	lines++;
    	if (lines%2 !=0){
    		sscanf(text, "%s %s", 
    			name, last_name);
    	}else {
    		sscanf(text, "%d %d %d %d %d",
    			score, score+1, score+2, score+3, score+4);
    	for(k=0, average=0.0; k<5; k++)
    		average += (double)score[k];
    	average /= 5.0;
    	i_ave = (int)(average + 0.5);
    	if(i_ave = 100) ch_grade = 'A';
    	else if(i_ave >= 90) ch_grade = 'A';
    	else if(i_ave >= 80) ch_grade = 'B';
    	else if(i_ave >= 65) ch_grade = 'C';
    	else if(i_ave >= 55) ch_grade = 'D';
    	else ch_grade = 'F';
    	printf("Student %s %s: ", name, last_name);
    	printf("Average = %5.1f;", average);
    	printf(" Grade = %c.\n", ch_grade);
    	fprintf(outfile, "Student %s %s: ", name, last_name);
    	fprintf(outfile, "Average = %5.1f;", average);
    	fprintf(outfile, " Grade = %c.\n", ch_grade); 	
    	}
    	}
    	fclose(infile);
    	fclose(outfile);
    	exit(0);
    }
    I need the grade of 100% to print out with an "A+", but when I type in the plus I get warning messages. I tried it once just to see and once the plus was in just the first line, all the outputs got changed to just pluses.

    Ex:

    Student 1, Grade = +
    Student 2, Grade = +
    and so on...

    I only need the 100% student to print out an "A+" what do I need?

    Thank you

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    A bit of confusing code. You are using pointer arithmetic

    Code:
    average += (double)score[k];
    This is not really bad but then later you are calling

    Code:
    score[k]
    Not ideal to jump between styles. You should stay consistent. I could not clearly see where you add the '+' character when you read in a students grades that average 100?

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    My comment addressing your pointer arithmetic should have showed:

    Code:
    score, score+1, score+2, score+3, score+4
    sorry

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Try this

    Code:
    if(i_ave = 100) ch_grade = "A+";

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    you declared ch_grade as one character.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by slingerland3g View Post
    Try this

    Code:
    if(i_ave = 100) ch_grade = "A+";
    you mean : i_ave == 100

    But this won't work since his ch_grade is declared as one character.

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by DaveH View Post
    you mean : i_ave == 100

    But this won't work since his ch_grade is declared as one character.

    Yeah, correct. Either change the char to a string variable or simply add a printf statement to append the '+' to the grade within the if block.

  8. #8
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Then declare ch_grade as 2 characters.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. matrix program issue, recursion
    By sweener2001 in forum C Programming
    Replies: 1
    Last Post: 04-28-2005, 04:28 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM