Thread: Input student grades and report the # of students with A, B, C, & D.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    5

    Input student grades and report the # of students with A, B, C, & D.

    Greetings,

    I was given the task to create a program that:

    1. Lets you input grades from only 10 students.
    2. Prints a report with the number of students who got A, B, C, and Ds.

    I've been trying my hardest to figure it out, I think I'm onto something but I just need a little push in the right direction, specially with the part where "grade" gets saved into the memory and then it is supposed to add to the counters of the grades A, B, C, and D respectively according to what the user has input.

    If you can help me a little I'll appreciate it.
    Thanks in advance.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <conio.h>
    
    
    
    
    int main()
    {
        //initialize variables
        int grade;
        int A = 0;
        int B = 0;
        int C = 0;
        int D = 0;
        int count = 0;
    
    
    printf( "Enter 10 student's grades:\n");
    
    
        while ( count < 10 ) {
            scanf("%i", &grade);
            grade = getchar();
                if ( grade >= 90){
                A = A + 1;
                }
                else if ((grade >= 80) && (grade < 90)) {
                B = B + 1;
                }
                else if ((grade >= 70) && (grade < 80)) {
                C = C + 1;
                }
                else ((grade >= 1) && (grade < 70)); {
                D = D + 1;
                }
    
    
            count = count+1;
        }
    
    
    
    
        printf( "%i students got A\n", &A);
        printf( "%i students got B\n", &B);
        printf( "%i students got C\n", &C);
        printf( "%i students got D\n", &D);
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Code:
    scanf("%i", &grade);
            grade = getchar();
    if ( grade >= 90){
      /* etc.. */
    You're overwriting the actual grade with a newline character every time. Instead, use:

    Code:
    scanf(" %i", &grade);
    if (grade >= 90) {
     /* etc.. */

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    Thanks for your reply .
    Unfortunately I keep getting the same nasty results.

    Instead of getting :
    3 students got A
    2 students got B, etc

    I get:
    2686740 students got A
    2686736 students got B, etc

    I wonder what went wrong....

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Quote Originally Posted by motaro15 View Post
    printf( "%i students got A\n", &A);
    printf( "%i students got B\n", &B);
    printf( "%i students got C\n", &C);
    printf( "%i students got D\n", &D);




    [/CODE]
    In these printf statements you are printing address of A not value of A.
    Just remove the '&' before every variable in these printf statements.
    You'll get the actual result.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    One more thing,

    Code:
    else ((grade >= 1) && (grade < 70)); {
                D = D + 1;
                }
    This is wrong, because D=D+1 is getting executed every time as you have ';' at the end of else statement.
    You can write like this
    Code:
    else D = D + 1;

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    5
    Holy cow! it worked!
    I didn't know the difference between an address and a value of a variable, now I know.

    Thanks sana.iitkgp and memcpy for your help
    Last edited by motaro15; 09-12-2012 at 10:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input students score
    By wiz23 in forum C++ Programming
    Replies: 46
    Last Post: 04-29-2005, 05:31 AM
  2. c program on grades of a student
    By galmca in forum C Programming
    Replies: 14
    Last Post: 09-29-2004, 03:07 PM
  3. Do all students get student loans, or can you get it rejected?
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-05-2002, 01:56 PM
  4. Replies: 13
    Last Post: 08-15-2002, 09:20 AM
  5. Any Computeach International Students/ex students??
    By stevey in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 03-26-2002, 04:12 PM