1 Replies - 2124 Views - Last Post: 18 February 2015 - 12:08 PM Rate Topic: -----

#1 biscuitrainbow   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-February 15

i need help about my gpa calculator

Posted 18 February 2015 - 11:53 AM

this is all of my code


#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 : )

Is This A Good Question/Topic? 0
  • +

Replies To: i need help about my gpa calculator

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: i need help about my gpa calculator

Posted 18 February 2015 - 12:08 PM

Part of your problem is this line: char grade[2]; This allocates memory for exactly two characters for example 'A' and '\0'. Remember C-strings require a terminating end of string character ('\0') so you would need at least a size of 3 to hold 'A' '+' '\0'. Also note you can't use the comparison operators with a C-string, you'll need to use strcmp() instead.

Jim

This post has been edited by jimblumberg: 18 February 2015 - 12:31 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1