【发布时间】:2021-05-26 11:13:30
【问题描述】:
我尝试过的
- 我尝试将百分比设为静态
我不明白为什么 setData 中的这个百分比变量显示错误的值
我公开了百分比变量。但这在 cpp 中是允许的。 节目说明
这是一个非常简单的程序。我这样做是为了了解我们如何在类外定义函数。
预期输出
If marks in subject 1 -> 10 (max 100)
subject 2 -> 10
subject 3 -> 10
subject 4 -> 10
subject 5 -> 10
Total -> 50
percentage -> 10 %
Source code
#include<iostream>
using namespace std ;
class result{
char name[30];
int a,b,c,d,e;
public:
int total;
double percentage;
int rollno;
int subjects;
int sum(){
int total;
total=a+b+c+d+e;
return 0;
}
void setdata();
void getdata();
};
void result::setdata(){
cout<<"enter the name of the student"<<endl;
cin>>name;
cout<<"enter the rollno of the student"<<endl;
cin>>rollno;
cout<<"marks in english:"<<endl;
cin>>a;
cout<<"marks in hindi:"<<endl;
cin>>b;
cout<<"marks in maths:"<<endl;
cin>>c;
cout<<"marks in science:"<<endl;
cin>>d;
cout<<"marks in social studies:"<<endl;
cin>>e;
percentage=(total/500)*100;
if (percentage<35.5){
cout<<"!!!!!!FAIL!!!!!!"<<endl;
}else(cout<<"!!!!!!PASS!!!!!!"<<endl);
}
void result::getdata(){
cout<<"name of student is:"<<name<<endl;
cout<<"the roll number is:"<<rollno<<endl;
cout<<"the number of subjects are:"<<subjects<<endl;
cout<<"marks in english is: "<<a<<endl;
cout<<"marks in hindi is: "<<b<<endl;
cout<<"marks in maths is: "<<c<<endl;
cout<<"marks in science is: "<<d<<endl;
cout<<"marks in social studies is: "<<e<<endl;
cout<<"total marks of all subjects :"<<total<<endl;
cout<<"the total pecentage of "<<name<<" is: "<<percentage<<endl;
}
int main(){
result abhishek;
abhishek.subjects=05;
abhishek.setdata();
abhishek.getdata();
return 0;
}
【问题讨论】:
-
请将文本信息显示为文本,而不是文本图片。
-
疯狂猜测:
percentage=(total/500)*100;那是一个整数除法。让它500.0 -
你得到什么输出?预期的输出是什么?你能描述一下区别吗?
-
我将 500 更改为 500.0 但答案仍然是突然的数字@Raildex
-
@Yunnosch 我已尝试添加预期结果。我不明白你的评论
Please show text info as text, not as picture of text.
标签: c++