#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
main()
{
int subject_count = 1;//subject sequence
float point, sum_point = 0; //point section
float credit, sum_credit = 0; //credit section
int grade_x_credit = 0, sum_grade_x_credit = 0; //grade x credit , sum = grade x credit
char grade; // input grade with char var type;
float gpa; //gpa with float type
int subject_code=0;//subject code
printf(" ******************************************************************\n\n");
printf("\t\t WELCOME TO GPA CALCULATOR \n\n");
printf(" ******************************************************************\n\n");
printf("ENTER SUBJECT CODE AND GRADE AND CREDIT WHITH '0' WHEN YOU FISNISH\n\n");
do
{
printf("Please enter you #%d SUBJECT CODE\n\n", subject_count);
scanf("%d", &subject_code);
printf("Please enter your #%d SUBJECT GRADE\n\n", subject_count);
scanf(" %c", &grade);
printf("Please enter #%d SUBJECT CREDIT\n\n", subject_count);
scanf(" %f", &credit);
printf(" ******************************************************************\n\n");
printf("\t SUBJECT CODE : %d GRADE : %c CREDIT : %.0f\n\n",subject_code,grade,credit);
printf(" ******************************************************************\n\n");
subject_count++;
sum_credit = credit + sum_credit;
if (grade == 'A')
point = 4;
else if (grade == 'B+')
point = 3.5;
else if (grade == 'B')
point = 3;
else if (grade == 'C+')
point = 2.5;
else if (grade == 'C')
point = 2;
else if (grade == 'D+')
point = 1.5;
else if (grade == 'D')
point = 1;
else
point = '0';
sum_point = point + sum_point;
grade_x_credit = point*credit;
sum_grade_x_credit = grade_x_credit + sum_grade_x_credit;
/*printf("your grade x credit is %d\n", grade_x_credit);
printf("your grade x credit is %d\n", sum_grade_x_credit);*/
gpa = sum_grade_x_credit / sum_credit;
}
while (grade != '0');
printf("TOTAL SUBJECT : %d \n\n" , subject_count-2);
/*printf("your point is %d\n", sum_point);*/
printf("TOTAL CREDIT : %.0f\n\n", sum_credit);
printf(" ******************************************************************\n\n");
printf("\t\t\tYOUR GPA IS %.2f\n\n", gpa);
printf(" ******************************************************************\n\n");
}
above code is work i can finish program by intput '0' . but my gpa calculator can't caculate with A+ B+ C+ D+ grade becuase char type can contain only one character right ?
so i added array to char type but it won't finish seem like it stuck in loop even i already putted 0 .
char grade;
do{
printf("Please enter your #%d SUBJECT GRADE\n\n", subject_count);
scanf(" %c", &grade);
printf("\t SUBJECT CODE : %d GRADE : %c CREDIT : %.0f\n\n",subject_code,grade,credit);
}
while (grade != '0');
above code is work code i make it shorter and easy to read.
and this code after i added array and chage %c to %s it does not work. it stuck in loop even i already put 0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
main()
{
int subject_count = 1;//subject sequence
float point, sum_point = 0; //point section
float credit, sum_credit = 0; //credit section
int grade_x_credit = 0, sum_grade_x_credit = 0; //grade x credit , sum = grade x credit
char grade[2]; // input grade with char var type;
float gpa; //gpa with float type
int subject_code=0;//subject code
printf(" ******************************************************************\n\n");
printf("\t\t WELCOME TO GPA CALCULATOR \n\n");
printf(" ******************************************************************\n\n");
printf("ENTER SUBJECT CODE AND GRADE AND CREDIT WHITH '0' WHEN YOU FISNISH\n\n");
do
{
printf("Please enter you #%d SUBJECT CODE\n\n", subject_count);
scanf("%d", &subject_code);
printf("Please enter your #%d SUBJECT GRADE\n\n", subject_count);
scanf(" %s", grade);
printf("Please enter #%d SUBJECT CREDIT\n\n", subject_count);
scanf(" %f", &credit);
printf(" ******************************************************************\n\n");
printf("\t SUBJECT CODE : %d GRADE : %s CREDIT : %.0f\n\n",subject_code,grade,credit);
printf(" ******************************************************************\n\n");
subject_count++;
sum_credit = credit + sum_credit;
if (grade == 'A')
point = 4;
else if (grade == 'B+')
point = 3.5;
else if (grade == 'B')
point = 3;
else if (grade == 'C+')
point = 2.5;
else if (grade == 'C')
point = 2;
else if (grade == 'D+')
point = 1.5;
else if (grade == 'D')
point = 1;
else
point = 0;
sum_point = point + sum_point;
grade_x_credit = point*credit;
sum_grade_x_credit = grade_x_credit + sum_grade_x_credit;
/*printf("your grade x credit is %d\n", grade_x_credit);
printf("your grade x credit is %d\n", sum_grade_x_credit);*/
gpa = sum_grade_x_credit / sum_credit;
}
while (grade != '0');
printf("TOTAL SUBJECT : %d \n\n" , subject_count-2);
/*printf("your point is %d\n", sum_point);*/
printf("TOTAL CREDIT : %.0f\n\n", sum_credit);
printf(" ******************************************************************\n\n");
printf("\t\t\tYOUR GPA IS %.2f\n\n", gpa);
printf(" ******************************************************************\n\n");
}
and i make it shorter
char grade[2];
do{
printf("Please enter your #%d SUBJECT GRADE\n\n", subject_count);
scanf(" %s", grade);
printf("\t SUBJECT CODE : %d GRADE : %s CREDIT : %.0f\n\n",subject_code,grade,credit);
}
while (grade != '0');
thanks for your help and sorry for my language skill : )

New Topic/Question
Reply


MultiQuote



|