C++ dynamic array
Apr 17, 2014 at 12:25am UTC
So i have this class i am doing this code for where i have to stick the data from a outside source into a dynamic array but when i excute it and when i enter the option D it show the file all messed up and doesn't sort it. I am using xcodes on the mac and i think my problem is that its not going into the dynamic array popular is there any tips or advice?
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <iomanip>
using namespace std;
struct Classes
{
string names; //Holds names of the students
string *courses; //Number of students
int num; //Holds the courses the student is taking
};
int main(int argc, const char * argv[])
{
fstream fin;
fin.open ("courses.txt" );
int size; //Number of students
fin >> size;
Classes * a = new Classes[size]; //Array of struct which holds a students name and classes
char el;
fin.get(el);
for (int ct = 0; ct < size; ct++)
{
getline (fin, a[ct].names);
fin >> a[ct].num;
a[ct].courses = new string [a[ct].num]; //Array that holds a student courses
for ( int ct2=0; ct2 < a[ct].num; ct2++)
{
fin >> a[ct].courses[ct2];
}
fin.get(el);
}
cout << "Enter menu choice or Q to quit: \n" ;
cout << "D to display all students and courses \n" ;
cout << "S to display courses for a student \n" ;
cout << "C to display students taking a course \n" ;
char choice; //Variable giving the user an option
cin >> choice;
while (choice != 'Q' )
{
if (choice == 'D' )
{
cout << left << setw(20) << "Name" << setw(50) << "Courses" << "\n" ;
for (int ct = 0; ct < size; ct++)
{
cout << left << setw(20) << a[ct].names;
for (int ct2 = 0; ct2 < a[ct].num; ct2++)
{
cout << a[ct].courses[ct2] << " " ;
}
cout << "\n" ;
}
cout << "\n" ;
}
if (choice == 'S' )
{
string sname; //Searches Name
cout << "Enter students name: " ;
cin.get(el);
getline(cin, sname );
bool found = false ;
int i = 0; //Counter Variable
while (!found && i < size)
{
if (a[i].names == sname)
found = true ;
else
i++;
}
if (found)
{
cout << "\n" << sname << " is taking: " ;
for (int ct = 0; ct < a[i].num; ct++)
{
cout << a[i].courses[ct] << " " ;
}
}
cout << "\n" ;
}
if (choice == 'C' )
{
string courses; //Searches Course
bool enrolled = false ;
cout << endl << "Enter course name: " ;
cin >> courses;
cout << endl << "Student taking " << courses << ":" << endl;
for (int ct = 0; ct < size; ct++)
{
for (int ct2 = 0; ct2 < a[ct].num; ct2++)
{
if (a[ct].courses[ct2] == courses)
{
enrolled = true ;
cout << a[ct].names << endl;
}
}
}
if (enrolled == false )
{
cout << endl << "No one is enrolled in " << courses << "." << endl << endl;
}
else
{
cout << endl;
}
}
char x; //Variable used to pick up the endl
cin.get (x);
cin >> choice;
}
system("pause" );
}
course.txt
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
Apr 19, 2014 at 2:24am UTC
This is homework? If the reply is no, use a vector instead.
www.cplusplus.com/reference/vector/vector/
Topic archived. No new replies allowed.