Thread: Help with a grading program in C

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    7

    Help with a grading program in C

    so I am doing this assignment for homework and I got everything working except for 1 portion and I am pretty sure it's something obvious that I just can't see. the program needs to be able to take the grades you enter tell you how many are passing and also give you an error when it goes over 100 and then the program will stop once given a -1 input, these portions are working the part I am stuck on is it also needs to take all the numerical grades entered and tell you what percent are passing. The program keeps giving me an outlandish percentage when i enter the test case which i will put below. I am not the best math student so this could be a simple math calculation i'm missing. I would appreciate any nudge in the right direction. Thanks!

    Test Case:
    45
    90
    70
    87
    123
    100
    -1

  2. #2
    Registered User
    Join Date
    Jan 2017
    Posts
    7
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    main() {
    
    
    	int grade = 0, sum = 0, pass = 0, percent, fail = 0;
    
    
    	
    
    
    	while (grade != -1) {
    		printf("Enter a numeric grade (-1 to quit):");
    		scanf_s("%i", &grade);
    
    
    		if (grade >= 70 && grade <= 100)
    			pass = pass + 1;
    
    
    
    
    		if (grade > 100)
    			printf("This is not a valid grade.\n");
    
    
    		if (grade > 0)
    			sum = sum + 1;
    
    
    		if (grade < 70 || grade > 100)
    			fail = fail + 1;
    		}
    printf("You entered %i passing grades.\n", pass);
    
    
    percent = fail / sum * 100;
    printf("%i%% of the valid grades entered are passing grades.\n", &percent);
    
    
    	system("pause");
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > percent = fail / sum * 100;
    You might be falling foul of integer truncation on the division.

    > printf("%i%% of the valid grades entered are passing grades.\n", &percent);
    You also don't need & when printing things.

    > if (grade < 70 || grade > 100)
    This will trigger on your -1 exit flag.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a grading program
    By compugeek915 in forum C Programming
    Replies: 7
    Last Post: 10-19-2007, 12:38 AM
  2. Grading Program pt.II (classes) help...
    By eun-jin in forum C++ Programming
    Replies: 7
    Last Post: 05-10-2006, 04:50 PM
  3. Grading Program Error
    By eun-jin in forum C++ Programming
    Replies: 9
    Last Post: 05-09-2006, 07:45 PM
  4. Program for grading
    By MasterLika in forum C Programming
    Replies: 3
    Last Post: 04-04-2005, 01:33 AM
  5. Grading Program
    By hockeydrummer88 in forum C++ Programming
    Replies: 6
    Last Post: 03-09-2005, 12:05 PM

Tags for this Thread