提问人:SHANTANU LOKHANDE 提问时间:6/14/2022 最后编辑:πάντα ῥεῖSHANTANU LOKHANDE 更新时间:6/14/2022 访问量:36
我定义为 Student 的类正在存储变量,但不处理它并执行所需的操作
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
*/
答:
1赞
DailyLearner
6/14/2022
#1
您正在执行整数除法。以下除法将产生不正确的结果。
total_percentage = ((eng_marks + maths_marks + sst_marks + comp_marks + sports_marks)/500)*100;
由于涉及的所有变量都是整数,编译器将执行以下除法。
total_percentage = ( (31+64+98+89+56) / 500 ) * 100
total_percentage = ( (338) / 500 ) * 100
total_percentage = ( 0 ) * 100
请更改 to 的数据类型并更新计算,如下所示:total_percentage
double
total_percentage
total_percentage = ((eng_marks + maths_marks + sst_marks + comp_marks + sports_marks)/500.)*100.;
请注意,而不是在上面的计算中。500.
500
有关更多详细信息,请参阅隐式转换。
评论