Display students of specific age using struct My task for uni is to create a program using structures. In this program I have to make an entry of students, their name, birth date and group (a random number). And I have to search the students according to the input age.

Please note that I am still learning :)

#include &lt;iostream&gt;
#include &lt;cstring&gt;

using namespace std;

struct student
{
char name[50];
int year;
int group;

};

int main( )
{
int N;
int age;

cout&lt;&lt;"Enter how many entries:";
cin&gt;&gt;N;
struct student student1[N];

for (int i=0;i&lt;N;i++)
{
cout&lt;&lt;"enter the student's name:";
cin&gt;&gt;student1[i].name;
cout&lt;&lt;"enter the birth year:";
cin&gt;&gt;student1[i].year;
cout&lt;&lt;"enter the group:";
cin&gt;&gt;student1[i].group;
}
cout&lt;&lt;"enter the student's age:";
cin&gt;&gt;age;

if (age==2016-student1[N].year)
{
    cout&lt;&lt;"The student with the age "&lt;&lt;age&lt;&lt;" is:";
    cout&lt;&lt;student1[N].name;
    cout&lt;&lt;student1[N].group;
}
else
{
    cout&lt;&lt;"the student wasn't found.";
}
return 0;
}
#include &lt;cstring&gt;


You're not using any of the features in that header. Replace is with &lt;string&gt; and do use those. Also indent your code, it makes it more readable.

struct student
{
  std::string name;
  int year;
  int group;
};


Don't get into the habit of using this:

using namespace std;


See Why is “using namespace std;” considered bad practice?

int N;
int age;


You only need N a few lines down, and age only way below inside the loop. Declare them as close to their first use as possible, in the smallest scope possible.

cin&gt;&gt;N;
struct student student1[N];


A few issues:


You're not checking whether that read of an int actually worked (same for all the other inputs).
You don't need the struct keyword there. You need it in C where structs live in a separate namespace, but that's not the case in C++.
This is not standard C++: this uses a feature called variable-length arrays which are standard C but not part of C++ (but some compilers have support for it anyway). Instead, use a std::vector (don't forget to #include &lt;vector&gt; also).




return 0;


This isn't necessary, if main doesn't explicitly return anything, the standard guarantees that it will act as if it had returned 0. (This is only valid for the main function, all other non-void functions must return something on all their returning branches.)