My Thinking:

struct student
{
int *subjects_number = new int [NoOfSubjects];
}
main()
{
int NoOfSubjects;
cin >> NoOfSubjects;
}

Objective: students could have taken different number of subjects. Now modify the structure such that its variable subjects_numbers has type pointer to int so that we can store scores for different number of subjects for each student.

My question is how can I input 2 subjects, using when number of subjects (2) is given on runtime.

To do this properly, you need to do a lot of work, and it will become a full-blown class (with private members, non-trivial constructor / destructor / copy-constructor / copy-assignment operator, etc.). Here is a good tutorial about this.

Objective: students could have taken different number of subjects. Now modify the structure such that its variable subjects_numbers has type pointer to int so that we can store scores for different number of subjects for each student.

Is this a self-imposed problem statement from you? Or is it what was given to you as an assignment? Because if this is what is required of you (have a pointer to int as member and do the allocation of a dynamic array with new/delete), then you have to follow the tutorial I linked to and adapt it to your problem context. But if you are not required to do that, then you should use the standard class std::vector:

#include <iostream>
#include <vector>

struct student
{
  std::vector<int> subjects_number;
};

int main()  // <--- NOTE: main() must return an integer.
{
  int NoOfSubjects = 0;
  cin >> NoOfSubjects;

  // create a student record:
  student my_student;

  // resize the vector of subject numbers:
  my_student.subjects_number.resize(NoOfSubjects);

  //... now you can access each subject number as 'my_student.subjects_number[i]'
  //     just like a normal array.

  return 0;
}
Comments
Thanks, but there should be a simple solution, because teacher can't ask what he hasn't taught yet.

Thanks, but there should be a simple solution, because teacher can't ask what he hasn't taught yet.

Well, programming is all about being able to learn on your own, because no amount of course-work is going to be enough to be proficient. So, who says the teacher can't ask for what he hasn't taught yet? If I were a teacher, I know I would do exactly that.

The other possibility is that your teacher has very very low expectation on the quality of the code that his students are supposed to produce. For example, he could expect you to produce the following:

#include <iostream>
#include <vector>

struct student
{
  int* subjects_number;
};

int main()  // <--- NOTE: main() must return an integer.
{
  int NoOfSubjects = 0;
  cin >> NoOfSubjects;

  // create a student record:
  student my_student;

  // allocate the array of subject numbers:
  my_student.subjects_number = int[NoOfSubjects];

  //... now you can access each subject number as 'my_student.subjects_number[i]'

  delete[] my_student.subjects_number;

  return 0;
}

But the above code, although correct for this trivial example, is absolutely horrible, and teaching to do this can be very damaging to the students' future coding habits. In the real world, the above code is complete garbage, it's a "throw it away and fire the guy who wrote it" situation, because anyone who thinks it's OK to code like this shouldn't be working for you.

Comments
Thanks Mike.
This article has been dead for over six months. Start a new discussion instead.