【问题标题】:Dev Cpp shows wrong percentage in this cpp programDev Cpp 在此 cpp 程序中显示错误的百分比
【发布时间】: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;
    }

Output

【问题讨论】:

  • 请将文本信息显示为文本,而不是文本图片。
  • 疯狂猜测:percentage=(total/500)*100; 那是一个整数除法。让它500.0
  • 你得到什么输出?预期的输出是什么?你能描述一下区别吗?
  • 我将 500 更改为 500.0 但答案仍然是突然的数字@Raildex
  • @Yunnosch 我已尝试添加预期结果。我不明白你的评论Please show text info as text, not as picture of text.

标签: c++


【解决方案1】:

使用浮点数计算百分比的另一种方法是编写:

percentage=total/5;

如果您不需要整数以外的精度,则整数除法就足够了。所以 0.2% 会变成 0%,25.1% 会变成 25%。

也戒掉using namespace std的习惯。 why?

【讨论】:

    【解决方案2】:

    代码的问题如下。

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

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

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

    您应该将所有内容都转换为浮点数或双精度数,而不是以原始形式除数。

    这是您的代码的更好的工作版本。还稍微重新格式化了您的代码,以便于阅读。

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

    【讨论】:

    • sum()成员函数中仍然是通过局部变量total隐藏了成员变量total
    【解决方案3】:

    有几个问题。

    percentage=(total/500)*100;
    

    此行访问total。但是,您永远不会将 total 设置为一个值。它未初始化,如果您尝试访问它,将包含垃圾值。

    • 你有一个计算total的函数int result::sum(),你 必须在计算 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

    【讨论】:

    • 它就像一个魅力!非常感谢您的帮助。
    猜你喜欢
    • 2014-10-31
    • 1970-01-01
    • 2013-06-10
    • 2014-12-25
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多