Searching an array of structs for a string, then output struct data

Hope everyone had a great Thanksgiving! I was hoping you guys could help, during my program, at the if (userChoice == 'C') part, I need to search to see if the string they entered into theCourse is found under the struct Records, and if the course is found, return the name of the individual(s) who are enrolled in the class. For example if Tommy Jill and Susan are enroled in MAT100, if the user types MAT100 into theCourse, it should cout Tommy Jill and Susan. I am thinking that I need to run a for loop than runs for the amount of time held in size, but I am unsure on the condition.


Here is the text file if it matters:

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
12
John Milligan
3 CIS100 CIS105 MAT113
Jill Kerning
5 CIS100 CIS105 MAT232 BIO100 ENG101
Aaron Spencer
4 CIS201 CIS225 MAT232 ENG101
Damon Hill
2 CIS334 CIS400
Kaitlyn Stamen
4 CIS100 CIS10 MAT113 BIO100
Debbie Martin
5 CIS100 CIS105 MAT232 CHY112 ENG101
Greg Nolan
2 CIS334 CIS450
Lynn Sanders
4 CIS334 CIS450 MAT250 BIO100
Alicia Thomas
4 CIS226 CIS450 MAT232 CHY112
Alan Turner
5 CIS100 CIS105 MAT232 BIO100 ENG101
Paul Henley
5 CIS100 CIS105 CIS334 ENG101 MAT232
Tim Copeland
1 CIS450



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;
struct myCourses //This struct in inside the one below it
{
	string myClass;
};

struct Records	//struct definition
{
	string name;
	int numCourses;
	myCourses courseCode[6];
};

int search(Records inventory[], string name, int size);
ifstream fin;

int main()
{
	fin.open("courses.txt");
	int size = 0; //size of dynamic array
	int count = 0; //counter for index
	char userChoice;

	fin >> size; //returns the first line of the document containing array size (12)
	Records* student = new Records[size]; //Dynamic allocation of array

	while (count < size) {						 //while array is not filled and file status is good, read/store the following:
		fin.ignore(1000, '\n');					 //clears buffer and ready for next line of text
		getline(fin, student[count].name, '\n'); //stops when it finds end of line

		fin >> student[count].numCourses;	     //reads in # of classes (keep simple)

		for (int ndx = 0; ndx < student[count].numCourses; ndx++)  //uses for loop to insert classes for student	
			fin >> student[count].courseCode[ndx].myClass;

		count++;
	}
	fin.close();
	cout << left;
	cout << "Enter menu choice or Q to quit:" << endl << "D to display all students and courses" << endl << "S to display courses for a student" << endl << "C to display students taking a course" << endl << endl << endl;
	cin >> userChoice; //Prompts the user for their choice, while not "Q", do the following

	while (userChoice != 'Q') {
		if (userChoice == 'D') {		//display all students and courses
			for (int i = 0; i < size; i++) {
				cout << setw(20) << student[i].name;

				for (int x = 0; x < student[i].numCourses; x++)
					cout << student[i].courseCode[x].myClass << " ";
					cout << endl;
			}
			cout << endl << "Enter next choice: ";
			cin >> userChoice;
		}
		if (userChoice == 'S') {		//display the courses a student is taking
			int spotInArray;
			string theName;
			cout << "Enter student name: ";
			cin.get();
			getline(cin, theName, '\n');
			spotInArray = search(student, theName, count);
			cout << endl;
			cout << theName << " is taking: ";
			for (int x = 0; x < student[spotInArray].numCourses; x++)
				cout << student[spotInArray].courseCode[x].myClass << " ";

			cout << endl << endl << "Enter next choice: ";
			cin >> userChoice;
		}

		if (userChoice == 'C') {		//display all students enrolled in a class
			string theCourse;
			int spotInArray;
			cout << "Enter course name: ";
			cin.get();
			getline(cin, theCourse, '\n');
			spotInArray = search(student, theCourse, count);
			//below needs to list all students who are taking the class stored in theCourse shown above


		}
	}
}



int search(Records record[], string name, int size) {
	int loc = -1;
	for (int i = 0; i < size; i++)
		if (record[i].name == name)
			loc = i;
	return loc;
}
Last edited on
Still stuck on this if anyone could offer any help I would appreciate it greatly!

1
2
3
4
5
6
7
8
9
10
for each student
{
    for each student_course
    {
        if course == course_to_find then
        {
             add student to found list
        }
    }
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//below needs to list all students who are taking the class stored in theCourse shown above

for (int s=0; s<size; ++s) // for all students
{
	for (int c=0; c<student[s].numCourses; ++c) // for all student courses
	{
		if (student[s].courseCode[c] == theCourse)
		{
			cout << student[s].name << endl;

			// no point searching the rest of this students records, break out of the student course loop
			break;
		}
	}
}
Last edited on
if (student[s].courseCode[c] == theCourse) seems to be invalid, used to compare ints if I am not mistaken. Also because courseCode may contain CIS100 MAT230 BIO100 etc this wouldnt work, would it? I feel like the loop structure is perfect though. I'm not sure if there is a way to see if courseCode contains theCourse?
== compares anything there is a suitable overload for.

my mistake was here...
if (student[s].courseCode[c].myClass == theCourse)
Topic archived. No new replies allowed.