Dev Cpp shows wrong percentage in this cpp program

What I have tried

  • I tried to make percentage static

I don't understand why this percentage variable in setData displays wrong value

I made percentage variable public. But that's allowed in cpp. Program description

This is a very simple program. I was doing it to learn how we can define function outside class.

Expected Output

 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



Read more here: https://stackoverflow.com/questions/66345726/dev-cpp-shows-wrong-percentage-in-this-cpp-program

Content Attribution

This content was originally published by Jack at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: