Structures and Arrays

I need help with the following question:
Design a structure Student containing the following members:
1) First Name;
2) Last name;
3) Date of birth (it should be in turn a structure containing three members Year, Month, Day); 4) Phone number;
5) Major field of study.

1. Write a function print_student_info that takes in a Student struct and print all its information.
2. Write another function search_students_by_lastname that take in an array of Student struct and string lastname as arguments and print the information of all students that match the given lastname. Use the print_student_info function in (1). If no structure matches, print a message “0 Matches found for the given lastname“.
3. Write another function search_students_by_yob that search for a student born in a particular year. It should take in an array of Student struct and an int yob as arguments and then search for all students with a birth year that matches it. Use print_student_info function to print the information of the matched structures. If no structure matches, print a message “0 Matches found for the given year of birth“
4. Write a main function to test the developed aforementioned functions. You should create an array of struct Student and fill it in with six students of arbitrary appropriate values. Then demonstrate the usage of search_students_by_lastname and search_students_by_yob functions.

This is what I have so far:
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
#include <iostream>
#include <string>
using namespace std;

struct Student                  //creates structure student
{
    char firstname;             //listing variables inside structure
    char lastname;
    struct dob
    {
        int year;
        int month;
        int day;
    };
    dob a;
    double number;
    char major;
};

void print_student_info(Student s) //function that calls to structure Student
      {
          cout << "First Name: " << s.firstname << endl;      //Prints student info
          cout << "Last Name: " << s.lastname << endl;
          cout << "Date of Birth(MMDDYYYY): " << s.a.month << s.a.day << s.a.year << endl;
          cout << "Phone Number: " << s.number << endl;
          cout << "Major Field of Study: " << s.major << endl;
      }

void search_students_by_lastname(Student s)
{
    struct Student lname[6];
    int i;
    for(i=0; i < 6; i++)
    {
        
    }
}

void search_students_by_yob(Student s)
{
    
}

int main()
{
    
}

Last edited on
Hi, it's good that you posted your code, but what do you actually need help with? Specifically.
I need help with completing sections 2, 3 and 4 of the question posted, I started some of 2 but don't know how to finish. Im not sure how to "take in an array of Student struct and string lastname as arguments and print the information of all students that match the given lastname. Use the print_student_info function in (1). If no structure matches, print a message “0 Matches found for the given lastname". "
Last edited on
take in an array of Student struct and string lastname as arguments


void search_students_by_lastname(Student s)

First, read the instructions, and then write the code to match it. The instructions tell you to have 2 arguments: and array of Students and a string. Your function has 1 argument which is a single Student.

By the way, you should be testing this code as you go. You wrote the function for step 1, but because you have nothing in your main, you could not have tested it. You need to write a main function that creates and populates a Student object and then calls print_student_info. Make sure that's correct before moving on to step 2.

When you are ready to test step 2, first create and populate an array of Students, and call the search_student_by_lastname function. Don't try to test the whole thing at once when you are done!!
How can I refer to the function print_student_info in the main function?

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 <iostream>
#include <string>
using namespace std;

struct Student                  //creates structure student
{
    char firstname;             //listing variables inside structure
    char lastname;
    struct dob
    {
        int year;
        int month;
        int day;
    };
    dob a;
    double number;
    char major;
};

   void print_student_info(Student s) //function that calls to structure Student
   {
       cout << "First Name: " << s.firstname << endl;      //Prints student info
       cout << "Last Name: " << s.lastname << endl;
       cout << "Date of Birth(MMDDYYYY): " << s.a.month << s.a.day << s.a.year << endl;
       cout << "Phone Number: " << s.number << endl;
       cout << "Major Field of Study: " << s.major << endl;
   }

void search_students_by_lastname(Student s)
{
    int i;
    for(i=0; i < 6; i++)
    {
        
    }
}

void search_students_by_yob(Student s)
{
    
}

int main()
{
  Student stdnt[6];
    
  for (int i = 0; i < 6; i++)
  {
      cout << "Enter first name" << endl;
      cin >> stdnt[i].firstname;
      cout << "Enter last name" << endl;
      cin >> stdnt[i].lastname;
      cout << "Enter date of birth (YYYYMMDD)" << endl;
      cin >> stdnt[i].a.year >> stdnt[i].a.month >> stdnt[i].a.day;
      cout << "Enter phone number" << endl;
      cin >> stdnt[i].number;
      cout << "Enter major" << endl;
      cin >> stdnt[i].major;

      for (i = 0; i < 6; i++)
      {
          stdnt[i].print_student_info();
      }
  }
Last edited on
Do you realize that char firstname; creates a variable for a single character not string. In C++ you should be using a std::string: std::string firstname; instead of a character.


Sorry guys, this is what I have edited, still incomplete but I'm working on it
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
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>
using namespace std;

struct Student                  //creates structure student
{
    string firstname;             //listing variables inside structure
    string lastname;
    struct dob
    {
        string year;
        string month;
        string day;
    };
    dob a;
    string number;
    string major;
};

   void print_student_info(Student s) //function that calls to structure Student
   {
       cout << "First Name: " << s.firstname << endl;      //Prints student info
       cout << "Last Name: " << s.lastname << endl;
       cout << "Date of Birth(MMDDYYYY): " << s.a.month << s.a.day << s.a.year << endl;
       cout << "Phone Number: " << s.number << endl;
       cout << "Major Field of Study: " << s.major << endl;
   }

void search_students_by_lastname(Student s)
{
    int i = 6;
    Student stdnt[6];
    if(stdnt[i].lastname == s.lastname)
    {
        cout << print_student_info;
    }
    else if(stdnt[i].lastname != s.lastname)
    {
        cout << "0 Matches found for the given lastname";
    }
}

void search_students_by_yob(Student s)
{
    
}

int main()
{
  Student stdnt[6];
    
  for (int i = 0; i < 6; i++)
  {
      cout << "Enter first name" << endl;
      cin >> stdnt[i].firstname;
      cout << "Enter last name" << endl;
      cin >> stdnt[i].lastname;
      cout << "Enter date of birth (YYYYMMDD)" << endl;
      cin >> stdnt[i].a.year >> stdnt[i].a.month >> stdnt[i].a.day;
      cout << "Enter phone number" << endl;
      cin >> stdnt[i].number;
      cout << "Enter major" << endl;
      cin >> stdnt[i].major;

  }

    for (int i = 0; i < 6; i++)
    {
        print_student_info(stdnt[i]);
    }
 
    return 0;
}
Topic archived. No new replies allowed.