Structures and Pointers
Task 1:
https://www.educative.io/courses/learn-cpp-from-scratch
Complete following exercises from the above link:
Reading:
https://www.programiz.com/cpp-programming/structure
Task 2:
Write a structure to store the roll no., name, age (between 11 to 14) and address of students (more than 10). Store the information of the students.
1 - Write a function to print the names of all the students having age 14.
2 - Write another function to print the names of all the students having even roll no.
3 - Write another function to display the details of the student whose roll no is given (i.e. roll no. entered by the user).
// Write a structure to store the roll no., name, age (between 11 to 14) and address of students (more than 10). Store the information
// of the students.
// 1 - Write a function to print the names of all the students having age 14.
// 2 - Write another function to print the names of all the students having even roll no.
// 3 - Write another function to display the print_specific of the student whose roll no is given (i.e. roll no. entered by the user).
#include <iostream>
#include <string>
using namespace std;
struct student
{
int roll;
string name;
int age;
string add;
}stu[20];
void print_age(int i)
{
if (stu[i].age == 14)
{
cout << stu[i].name << endl;
}
}
void print_even(int i)
{
if (stu[i].roll % 2 == 0)
{
cout << stu[i].name << endl;
}
}
void print_specific(int i)
{
cout << stu[i].name << endl;
cout << stu[i].roll << endl;
cout << stu[i].age << endl;
cout << stu[i].add << endl;
}
int main()
{
int n;
cout << "Enter the number of students: ";
cin >> n;
cin.ignore();
for (int i = 0; i < n; i++)
{
cout << "Enter Name of the Student " << i + 1 << " : ";
cin.clear();
getline(cin,(stu[i].name));
cout << "Enter Roll No of the Student " << i + 1 << " : ";
cin >> stu[i].roll;
cout << "Enter Age of the Student " << i + 1 << " : ";
cin >> stu[i].age;
cout << "Enter Address of the Student " << i + 1 << " : ";
cin.clear();
cin.ignore();
getline(cin,(stu[i].add));
cout << endl;
}
cout << "\\nStudents having age 14: " << endl;
for (int i = 0; i < n; i++)
{
print_age(i);
}
cout << "\\nStudents having even Roll no.: " << endl;
for (int i = 0; i < n; i++)
{
print_even(i);
}
cout << "\\nStudent Details: " << endl;
for (int i = 0; i < n; i++)
{
print_specific(i);
}
}