function - Why mycourses[i].getGrade() doesn't return anything (C++)? -


i having problem getgrade() function in header file not returning letter grade based on marks entered user in main file. when compile , run program doesn't display letter grade according marks entered.

header file (course.h)

#include <iostream> #include <string>  using namespace std;  class course { private:     int totalmarks;     char grade;   public:     void marksinfo(int tm)     {         totalmarks = tm;     }      int getmarks(void)     {         return totalmarks;     }      void setgrade(char c)     {         if(totalmarks <= 39)             c = 'e';          if(totalmarks >= 40 && totalmarks <= 49)             c = 'd';          if(totalmarks >= 50 && totalmarks <= 64)             c = 'c'     }      char getgrade(void)     {         return grade;     }  }; 

main file

 #include <iostream>  #include <string>  #include "course.h"   using namespace std;   int main()    {       int tm;        course course[5];        (int = 0; < 5; i++)       {           cout << "subject #" << i+1 << endl;            cout << "total marks #" << i+1 << ": ";           cin >> tm;            course[i].marksinfo(tm);            cout << endl;            course[i].getgrade();       }        cout << "grade: " << course[0].getgrade();   } 

your code never sets grade to anything. have problem-in-waiting:

void setgrade(char c) {     if(totalmarks <= 39)         c = 'e';      if(totalmarks >= 40 && totalmarks <= 49)         c = 'd';      if(totalmarks >= 50 && totalmarks <= 64)         c = 'c' } 

this function never changes grade, populates local variable called 'c' value based on totalmarks , forgets it. think wanted more this:

class course { private:     int totalmarks;     char grade;   public:     void marksinfo(int tm)     {         totalmarks = tm;          if(totalmarks <= 39)             grade = 'e';         else if(totalmarks >= 40 && totalmarks <= 49)             grade = 'd';         else if(totalmarks >= 50 && totalmarks <= 64)             grade = 'c';         else if(totalmarks >= 65 && totalmarks <= 84)             grade = 'b';         else             grade = 'a';     }      int getmarks(void)     {         return totalmarks;     }      char getgrade(void)     {         return grade;     }  }; 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -