Search code examples
c++functiontypename

the class that i defined as student is storing the variable but not processing it and doing the desired action


#include <iostream>

#include <string>

class student {

    public :
    int total_percentage {};

    
public:

    int eng_marks {31};
    int maths_marks {64};
    int sst_marks {98};
    int comp_marks {89};
    int sports_marks {56};
    

public:

    int percentage(){
        total_percentage = ((eng_marks + maths_marks + sst_marks + comp_marks + sports_marks)/500)*100;
        return total_percentage;
    }
public:

    int grade() {

        if (total_percentage >= 90){
          std::cout<<"The grade of the student is A. Congratulations ! " << std::endl;
        } else if(total_percentage >= 80 && total_percentage<90  ) {
          std::cout<<"The grade of the student is B. COOL !! "<<std::endl;
        } else if (total_percentage >= 70 && total_percentage<80){
          std::cout<<"The grade of the student is C. UH huh !! "<<std::endl;
        } else if (total_percentage >= 60 && total_percentage<70){
          std::cout<<"The grade of the student is D. UH huh !! "<<std::endl;
        } else {
          std::cout<<"The grade of the student is F. FAIL WORK HARD !! !! "<<std::endl;
        }
        return 0;
        
    }
    
};

int main (){
    
    
    
    student student1;
    std::cout<<"The percentage of default student is = " << student1.percentage() << std::endl;
    std::cout<< student1.grade() << std::endl;
    std::cout<< std::endl;
    std::cout<< std::endl;
    
    student student2;
    student2.comp_marks = 34;
    student2.sst_marks = 78;
    student2.eng_marks = 42;
    std::cout<<"The percentage of  student2 is = " << student2.percentage() << std::endl;
    std::cout<<"  student2 eng_marks  = " << student2.eng_marks << std::endl;
    std::cout<<student2.grade() << std::endl;
    std::cout<< std::endl;
    std::cout<< std::endl;
/* the value is being stored in the eng_marks variable but still the code is unable to calculate the total percentage */
    
    student student3;
    student3.maths_marks = 95;
    student3.sports_marks = 90;
    student3.sst_marks = 93;
    student3.comp_marks = 98 ;
    std::cout<< "The percentage of student3 is = " << student3.percentage() << std::endl;
    std::cout<<"  student3 sst_marks  = " << student3.sst_marks << std::endl;
    std::cout<<student3.grade() << std::endl;
    std::cout<< std::endl;
    std::cout<< std::endl;
    
    
    std::cout<<"Program end hit !! Thanks " << std::endl;
    
    
    
    
    return 0;
}




/*

OUTPUT 

    The percentage of default student is = 0
The grade of the student is F. FAIL WORK HARD !! !! 
0


The percentage of  student2 is = 0
  student2 eng_marks  = 42
The grade of the student is F. FAIL WORK HARD !! !! 
0


The percentage of student3 is = 0
  student3 sst_marks  = 93
The grade of the student is F. FAIL WORK HARD !! !! 
0

*/

Solution

  • You are performing an integer division. Following division will produce incorrect results.

    total_percentage = ((eng_marks + maths_marks + sst_marks + comp_marks + sports_marks)/500)*100;
    

    Since all variables involved are integer, compiler will perform the following division.

    total_percentage = ( (31+64+98+89+56) / 500 ) * 100
    total_percentage = ( (338) / 500 ) * 100
    total_percentage = ( 0 ) * 100
    

    Please change the data type of total_percentage to double and update total_percentage calculation as follows:

    total_percentage = ((eng_marks + maths_marks + sst_marks + comp_marks + sports_marks)/500.)*100.;
    

    Note 500. instead of 500 in the above calculation.

    For more details kindly refer to implicit conversion.