Hello everyone. Trying to pass array of structures to a function named ageprint to print names of students of age 14. Error in lines 16 and 18. Please help
error: no match for 'operator[]' (operand types are 'Student' and 'int')
Thanks in advance
#include <iostream>
#include <string>
constint num_students = 4;
struct Student
{
std::string name;
int age;
int rollno;
};
void ageprint(Student stud[])
{
std::cout << "age 14 students\n";
for (int i = 0; i < num_students; i++)
{
if (stud[i].age == 14)
{
std::cout << stud[i].name << '\n';
}
}
}
int main()
{
Student stud[num_students];
for (int i = 0; i < num_students; i++)
{
std::cout << "enter name student" << " #" << i + 1 << ": ";
std::cin >> stud[i].name;
stud[i].rollno = i + 1;
std::cout << "enter age: ";
std::cin >> stud[i].age;
}
std::cout << '\n';
ageprint(stud);
}
enter name student #1: Dave
enter age: 14
enter name student #2: Sarah
enter age: 15
enter name student #3: Joe
enter age: 14
enter name student #4: Pete
enter age: 13
age 14 students
Dave
Joe
Dependence on global variables and magic constants is restrictive. Consider:
1 2 3 4 5 6 7 8 9 10 11 12
void ageprint( Student stud[], int num, int age )
{
std::cout << "age " << age << " students\n";
for ( int i = 0; i < num; ++i )
{
if ( stud[i].age == age )
{
std::cout << stud[i].name << '\n';
}
}
}