Dev Cpp显示了这个Cpp程序中的错误百分比



我已经试过了

  • 我试图使百分比静态

我不明白为什么这个百分比变量在setData显示错误的值

我将百分比变量设为public。但这在cpp中是允许的。<<strong>程序描述/strong>

这是一个非常简单的程序。我这样做是为了学习如何在类之外定义函数。

预期输出

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;
}

输出

使用float来计算百分比的另一种方法是:

percentage=total/5;

如果不需要整数以外的精度,可以使用整数除法。所以0.2%会变成0% 25.1%会变成25%

同时改掉using namespace std的习惯。为什么?

代码的问题如下:

1-在计算之前(或其他任何地方)不调用sum()函数,因此total在开始时具有垃圾值,而不是total点。

2- sum()调用返回int,它应该返回void。

3-几乎所有语言的整数除法都返回数学结果的底值,而不是实际结果,c++就是其中之一。

与原始形式的除法不同,您应该将所有内容强制转换为float或double。

这是一个更好的,可以工作的代码版本。也重新格式化了你的代码,使它更容易阅读。

#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;
void sum(){
total=a+b+c+d+e;
}
void setdata();
void getdata();
};
void Result::setdata(){  // Capitalized first letter
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;
sum();  // calling sum
percentage=((float)total/5.0)*1.0;   // Used 5 and 1 instead of 500 and 100, converted numbers to float
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;

本行访问total。但是,您从未将total设置为一个值。它未初始化,如果您试图访问它,将包含垃圾值。

  • 你有一个函数int result::sum()计算total,你必须在计算percentage之前调用它。class Result也有成员total,但是在sum()方法中创建了新total。我不知道你的目的是什么。我会说你计算result::total
  • sum()也返回int,但是您返回0并计算total。修改函数返回void
  • 我还建议使用浮点除法而不是整数除法:percentage=(total/500.0)*100;
  • result abhishek;改为result abhishek{}为零初始化result的所有成员避免垃圾值。

这是你的代码的修改版本:
https://godbolt.org/z/r4ase1

最新更新