-1

I have written this code to take a student's Letter grade and calculate their GPA. When I run the program, I can get the student's name and the subjects correctly, but I cannot get the grades or GPA to show up.

Sample Input:

Sally
1
A
N

Sample Output:

Enter Student Name: Sally
How many subjects: 1
Grades for subject 1: A
Student Name: Sally
Grade: 4.00
Do you want to enter another student? N

Here is the code snippet I have tried.

#include <stdio.h>

int main()
{
    char studentName[20];
    char grade[10];
    int k1;
    int totalSub1,totalGrade;
    float gpa1;
    char ch1;
    do
    {
        //Student Name
            scanf("%s",studentName);
            printf("Enter Student Name: %s\n", studentName);

        //Number of subjects
            scanf("%d",&totalSub1);
            printf("How many subjects: %d\n",totalSub1);

        //Get Grades
            for(k1=0;k1<totalSub1;k1++)
            {
                printf("Grades:\n%c",grade[k1]);
                scanf("%c",&grade[k1]);
            }
        //Find the gade points
            for(k1=0;k1<totalSub1;k1++)
            {
                totalGrade=0;
                switch(grade[k1])
                {
                    case 'A':
                    totalGrade+=4;
                    break;
                case 'B':
                totalGrade+=3;
                break;
                case 'C':
                totalGrade+=2;
                break;
                case 'D':
                totalGrade+=1;
                break;
                case 'F':
                totalGrade+=0;
                break;
            }
        }
        //Calculate GPA
        gpa1=totalGrade/totalSub1;
        //Print Student name with GPA
        printf("Student Name: %s\n",studentName);
        printf("Grade: %.2f\n",gpa1);
            printf("Do you want to enter another student?\n");
            scanf("%c",&ch1);
        }while(ch1=='Y'||ch1=='y');
    getchar();
    return 0;
}
ballade4op52
  • 2,142
  • 5
  • 27
  • 42
Chels
  • 1
  • 2
  • 2
    What does the debugger say? Where does it go wrong? " I cannot get the grades" is no **specific** problem statement. Always check the result of functions which might encounter an error. `scanf` is one of them! – too honest for this site Sep 20 '15 at 10:20
  • `scanf` is very unintuitive at times. For example `%s` scans only one word, so that a studen name like "Norman Bates" will be read as "norman" and the next scan then tries to read an integer from the pending input "Bates". Also, while most formats skip leading white space, `%c` does not, so your grades might end up being new lines and spaces. – M Oehm Sep 20 '15 at 11:00
  • Please [edit] your question title to something meaningful that describes the problem you're having or the specific question you're asking. The fact it's C code is apparent from the c tag you used, and *What am I doing wrong?* is meaningless. Your title should be something that will be of use to future readers of this site who find it in a search result. A clear, descriptive title will usually get you help more quickly, too. Thanks. – Ken White Apr 02 '16 at 18:13

2 Answers2

2

There are two issues in your code :

   scanf("%c",&grade[k1]); 
   scanf("%c",&ch1);

should be replaced by,

   scanf(" %c",&grade[k1]); //notice the whitespace
   scanf(" %c",&ch1);

This tells scanf to ignore Whitespaces! Read about it here

Also your code to Calculate GPA should be replaced by:

   gpa1=totalGrade/(float)totalSub1; 

Suppose a guy gets a GPA of 2.67 if you dont add that (float) field gpa1 would store 2.00 !

Community
  • 1
  • 1
Rahul Jha
  • 1,131
  • 1
  • 10
  • 25
0

There are several issues with your code. Here is your fixed code.

#include <stdio.h>

int main()
{
    char studentName[20];
    char grade[10];
    int k1;
    int totalSub1,totalGrade;
    float gpa1;
    char ch1;
    do
    {
        //Student Name
        scanf("%s",studentName);
        scanf("%d",&totalSub1);
        //Get Grades
        for(k1=0; k1<totalSub1; k1++)
        {
            //getchar();  //If you not interested to give extra space in scanf
            scanf(" %c",&grade[k1]);   //extra white space needed to discard ENTER from previous scanf

        }
        //Find the gade points
        totalGrade=0;                     //Initialize totalGrade here not in the loop
        for(k1=0; k1<totalSub1; k1++)
        {
            switch(grade[k1])
            {
            case 'A':
                totalGrade+=4;
                break;
            case 'B':
                totalGrade+=3;
                break;
            case 'C':
                totalGrade+=2;
                break;
            case 'D':
                totalGrade+=1;
                break;
            case 'F':
                totalGrade+=0;
                break;
            }
        }
        //getchar();
        scanf(" %c",&ch1);
        printf("Enter Student Name: %s\n", studentName);
        printf("How many subjects: %d\n",totalSub1);
        for(k1=0; k1<totalSub1; k1++)
        {
            printf("Grades for subject %d: %c\n", k1+1, grade[k1]);
        }
        //Calculate GPA
        gpa1=(float)totalGrade/totalSub1;      //If you don't cast to float then gpa1 get result from integer division of totalGrade and totalSub1
        //Print Student name with GPA
        printf("Student Name: %s\n",studentName);
        printf("Grade: %.2f\n",gpa1);
        printf("Do you want to enter another student? %c\n", ch1);
    }
    while(ch1=='Y'||ch1=='y');
    getchar();
    return 0;
}
ballade4op52
  • 2,142
  • 5
  • 27
  • 42
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
  • When the input is entered say: Sally 1 A N What I get for the Output i: Enter Student Name: How many subjects: Grades for subject 1: Student Name: Sally Grade: 4.00 Do you want to enter another student? Why are the first three lines not printing the student name, number of subjects, or grades for subject? Am I putting in wrong? – Chels Sep 20 '15 at 19:50
  • What do you mean by f? – ashiquzzaman33 Sep 20 '15 at 19:53
  • Please specify what is your expected input and output format with example. Add this to your question. – ashiquzzaman33 Sep 20 '15 at 20:11
  • @Chels Answer edited, this match your input and output format. – ashiquzzaman33 Sep 20 '15 at 21:52