How to turn this into a function

Hi, can someone help me make this into a function please. Is it possible to make one function to traverse the arrays and another function to check the condition and then join it together? Or how do I do it. I'm going to be traversing the arrays many times in my program so I think creating a function is better instead of writing the code over and over, but I really don't know how to do it. Please help.
Thanks

 
  

All the necessary declarations have been made. This is just a snippet.
Last edited on
It looks like you want to print a list of all students registered for a specific course.

I'm not entirely sure I understand your registration_array[] (or why you are using arrays instead of a vector or map).

The registration array seems to duplicate a lot of information. For each course, you should have an array of registered students.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

struct Student
{
  typedef unsigned ID;
  
  ID id;
  string last_name;
  string first_name;
};

ostream& operator << ( ostream& outs, const Student& student )
{
  if (student.id)
    return outs << student.id << "\t" 
                << student.last_name << "\t"
                << student.first_name;
  return outs << "ID\tLNAME\tFNAME";
}

typedef std::string CourseName;

typedef map <CourseName, vector <Student::ID> > Registrations;
typedef map <Student::ID, Student> Students;

int main()
{
  Students students = {
    { 1, { 1, "Bourne", "Jason" } },
    { 2, { 2, "Greene", "Alice" } },
    { 3, { 3, "Idol",   "Billy" } },
    { 4, { 4, "Moore",  "Mandy" } }
  };
  Registrations registrations = {
    { "Algebra", { 1, 2    } },
    { "English", { 2, 3    } },
    { "Art",     { 1, 3, 4 } }
  };

  cout << "Courses:\n";
  for (auto r : registrations)
    cout << "  " << r.first << "\n";
  cout << "\n";
    
  cout << "Enter course name: ";
  string course;
  getline( cin, course );

  if (!registrations.count( course ))
    cout << "That course does not exist.\n";
  else
  {
    cout << Student() << "\n";
    for (auto id : registrations[ course ])
      cout << students[ id ] << "\n";
    cout << "\n";
  }
}

I know this doesn't really answer your question using your existing code. If you want to stick with arrays, etc, write some small functions to do what you want -- a function to lookup a course, etc. You should at least have a function to print out a student.

Good luck.
Hi, thanks for this. Unfortunately, we haven't covered vectors or maps so arrays is the only option. But thanks again :)
You can make a function that will return a bool. Then another function to traverse through the array. Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool checkCondition(registration_array myArray[], string course){
for(int i = 0; i < registration_size; i++){
		if(registration_array[i].getCourse() == course)
                         return true;
                return false
}

void traverseArray(registration_array myArray[], string course){
       if(checkCondition(myArray,course)){
         for(int j = 0; j < student_size; j++)
		{
		     if(student_array[j].getId() == registration_array[i].getId())
			{
			 cout << student_array[j].getId() << "\t"
		         << student_array[j].getLname() << "\t\t"
			 << student_array[j].getFname() << "\t\t"
		         << student_array[j].getAge() << "\t"
			 << student_array[j].getPhone() << endl;
			}
	  }
       }
}


You can do it using one function.
Last edited on
Topic archived. No new replies allowed.