0

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

Jack
  • 3
  • 2
  • 1
    Please show text info as text, not as picture of text. – Yunnosch Feb 24 '21 at 06:29
  • 1
    wild guess: `percentage=(total/500)*100;` thats an integer division. make it `500.0` – Raildex Feb 24 '21 at 06:29
  • What output do you get? What would be the expected output? Can you describe the difference? – Yunnosch Feb 24 '21 at 06:30
  • I changed the 500 to 500.0 but answer is still abrupt number @Raildex – Jack Feb 24 '21 at 06:37
  • @Yunnosch I have tried to add a expected result. I don't understand your comment `Please show text info as text, not as picture of text. ` – Jack Feb 24 '21 at 06:39
  • @Jack you never call `result::sum()` and thus, `total` is never set. There are more issues with your code. I'll write that in an answer – Raildex Feb 24 '21 at 06:41
  • Thanks for adding info to the question. You show a link to a picture of text. I recommend to instead show the textual info in the picture directly as a text quote in your question. For similar reasons like https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557 – Yunnosch Feb 24 '21 at 06:41
  • You have a member variable `int total`, but in the `sum()` member function you have a local variable `int total`. The local variable is **hiding** the member variable. – heap underrun Feb 24 '21 at 06:44
  • Compile your code with [GCC](http://gcc.gnu.org/) invoked as `g++ -Wall -Wextra -g` to get all warnings and debug info. Improve your code to get no warnings. Perhaps use the [Clang static analyzer](https://clang-analyzer.llvm.org/) on your C++ code, if allowed to. Use the [GDB debugger](https://www.gnu.org/software/gdb/) to understand the behavior of your executable – Basile Starynkevitch Feb 24 '21 at 07:06
  • Of course, read a good [C++ programming book](https://www.stroustrup.com/programming.html), see also [this C++ reference](https://en.cppreference.com/w/cpp) and take inspiration from existing open source C++ software like [fish](https://fishshell.com/), [FLTK](https://fltk.org/), [RefPerSys](http://refpersys.org/). Consider using [GNU emacs](https://www.gnu.org/software/emacs/) to edit your C++ code, and [astyle](http://astyle.sourceforge.net/astyle.html) to indent or reformat it – Basile Starynkevitch Feb 24 '21 at 07:10

3 Answers3

2

An alternative to resorting to float for calculating percentage would be to write:

percentage=total/5;

Integer division suffices if you don't need precision beyond whole numbers. So 0.2% will become 0 percent and 25.1% will become 25%.

Also drop the habit of using namespace std. why?

kdcode
  • 524
  • 2
  • 7
1

The problems of the code are as follows.

1- You do not call sum() function before calculation (or anywhere else), so the total has a garbage value at the beginning instead of total of the points.

2- sum() call returns an int, it should return void.

3- Integer division on almost all languages returns the floor value of the mathematical result instead of real result, C++ is one of those languages.

Instead of dividing numbers in raw form, you should cast everything to either float or double.

Here is a better, working version of your code. Also reformatted your code a little bit, so that it is easier to read.

#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;
    }
gsan
  • 549
  • 1
  • 4
  • 14
0

There are several problems.

percentage=(total/500)*100;

This line accesses total. However, you never set total to a value. It is uninitialized and will contain garbage value if you try to access it.

  • You have a function int result::sum() which calculates total, you have to call it before you calculate percentage. class Result also has a member total, but in your sum() method you create a new total. I don't know what your intended use is. I'd say you should calculate result::total
  • sum() also returns an int but you return a 0 and calculate total. Change the function to return void instead.
  • I'd also suggest to use a floating point division instead of an integer division: percentage=(total/500.0)*100;
  • Change result abhishek; to result abhishek{} to zero-initialize all members of result to avoid garbage values.

Here's a revamped version of your code:
https://godbolt.org/z/r4ase1

Raildex
  • 3,406
  • 1
  • 18
  • 42