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 101
|
#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 << "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
cout << left;
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: " << endl;
cin.get();
getline(cin, theName, '\n');
spotInArray = search(student, theName, count);
cout << theName << " is taking: " << student[spotInArray].courseCode << endl;
cout << endl << "Enter next choice: ";
cin >> userChoice;
cin.get();
}
if (userChoice == 'C') { //display all students enrolled in a class
string theCourse;
cout << "Enter course name: ";
cin >> theCourse;
cin.get();
}
}
}
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;
}
|