Write a program to define a class to input the name of a student and marks of three subjects
(s1, s2, s3), calculate the total marks and average marks in a member function of a class and also
display the output in another member function of a class. Each subject has a maximum of 100
marks.
#include<iostream>
#include<string>
using namespace std;
class student
{
  string name;
  float s1,s2,s3;
public:
  student()
  {
    cout<<"Enter the name of student : ";
    getline(cin,name);
    s1m:
      cout<<"Enter marks in first subject : ";
      cin>>s1;
      if(s1>100)
        {
          cout<<"Invalid input marks should be less than 100 ";
          goto s1m;
        }
    s2m:
      cout<<"Enter marks in second subject : ";
      cin>>s2;
      if(s2>100)
        {
          cout<<"Invalid input marks should be less than 100 ";
          goto s2m;
        }
    s3m:
      cout<<"Enter marks in third subject : ";
      cin>>s3;
      if(s3>100)
        {
          cout<<"Invalid input marks should be less than 100 ";
          goto s3m;
        }
  }
  float total_marks()
  {
    float total;
    total=s1+s2+s3;
    return total;
  }
  float average_marks()
  {
    float average;
    average=(total_marks())/3;
    return average;
  }
  void display()
  {
    cout<<"\nName of student : "<<name;
    cout<<"\nMarks in first subject : "<<s1;
    cout<<"\nMarks in second subject : "<<s2;
    cout<<"\nMarks in third subject : "<<s3;
    cout<<"\nTotal marks = "<<total_marks()<<"/300";
    cout<<"\nAverage marks = "<<average_marks();
  }
};
int main()
{
  student s1;
  s1.display();
}
Comments
Dear Asif , please post a new question
Write a program to initialize the data members through passing two parameters using a constructor. Calculate their sum and print the output on the screen using a separate member function of a class.
Leave a comment