Creating a dynamic array

Hello and happy Thanksgiving. I am trying to get my code, as shown by the comment in the while statement, to instead of reading the entire line of courses into record[count].courseCode, create a dynamic array that is sized appropriately depending on the number of courses they are enrolled in as shown in the number before the course codes.

For example:
John Milligan
3 CIS100 CIS105 MAT113

This would create a dynamic array[2] and store each course code into each member of the dynamic array. Would this require a for statement during the while statement to create a dynamic array of a certain size? Also, I would have to change the definition of my struct to: string courseCode[] to allow for an array, correct? I hope my question makes sense! Thanks for your help guys!


Text File:
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
struct Records	//struct definition
{
	string name;
	int numCourses;
	string courseCode;
};


  int main()
{
	int size;
	int count = 0;
	ifstream fin;
	char userChoice;\


	fin.open("courses.txt");
	fin >> size; //returns the first line of the document containing array size
	if (!fin) {
		cout << "Could not read number of students from file\n";
		return 1;
	}
	fin.ignore(1000, '\n'); //ignore everything until end of line

	Records* record = new Records[size]; //Dynamic allocation of array

	while (count < size) {			//while array is not filled and file status is good, read/store the following:
		getline(fin, record[count].name);
		fin >> record[count].numCourses;
		fin.ignore();
		getline(fin, record[count].courseCode); //this needs to store each course code into an element of array, sized determined by record[count].numCourses
		count++;
	}
Last edited on
closed account (NCRLwA7f)
I don't know if your input text file is formatted correctly but I'm going to assume that the number "12" in line 1 is not supposed to be there. Based on your example
John Milligan
3 CIS100 CIS105 MAT113


I am assuming that you first encounter the students' name then in the second line you get the numbers of classes and the class names (or class codes).

If I am interpreting all your data correctly then one of the problems with your code is that in line 18 where you have
fin >> size; //returns the first line of the document containing array size
you are actually pulling in a student name.
maybe you can do something like this
1
2
3
fin >> f_Name;
fin >>l_Name;
fin >> size; // you will grab the size of the dynamic array to make 


I don't know how else you plan to use the student names but reading them in as a string will technically put them in an "array" and it will stop at the end of each 1st and last name.

1
2
fin >> f_Name; // John
fin>> l_Name; // Milligan 
Hello, the 12 is supposed to be there, it signifies the amount of entries in the text document. It is read in by fin >> size; and because the document isn't closed after, it keeps reading after that with the below code which is properly storing the data.

1
2
3
4
5
6
7
while (count < size) {			//while array is not filled and file status is good, read/store the following:
		getline(fin, record[count].name);
		fin >> record[count].numCourses;
		fin.ignore();
		getline(fin, record[count].courseCode); //this needs to store each course code into an element of array, sized determined by record[count].numCourses
		count++;
	}


My question is, How can I, after knowing that John Milligan is taking 3 classes, make a dynamic array, classes[2], and store his classes into it.

Later I will need to pull all students pulled in CIS201 for example.
closed account (NCRLwA7f)
ok, You need to actually have 2 structs, they will be nested into one another.

so for example you have

student 1: Name
class: 1
class: 2
class: 3

student2: name
class:1
class: 2


1
2
3
4
5
6
7
8
9
10
11
12
struct secondStruct
{
  string myclass;
};

struct Records	//struct definition
{
	string name;
	int numCourses;
	secondStruct courseCode[6]; // max number of classes
};



insert whatever name you want for "secondStruct"

*notice that courseCode "type" is secondStruct and not string anymore.

the nested struct needs to be on top of the "outer struct" so it can be found by your 1st struct.

sorry so late doing Thanksgiving stuff


also, this is just a preference but I would rename "record" to student, that way you can read the struct Records student and it's a little less confusing.
Last edited on
closed account (NCRLwA7f)
hey, I messed with your code a little and made some changes.

hopefully, it is not too late for help.

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
struct myCourses //This struct in inside the one below it
{
    string myClass;
};

struct Records	//struct definition
{
    string name;
    int numCourses; //this number will define up to what array place courseCode    prints out
    myCourses courseCode[6];
};



ifstream fin;

  int main()
{

fin.open("courses.txt");

    int size = 0; //Size of Dynamic Array
    int count = 0; // counter for index
    int clssNub = 0;
    char userChoice;
	
     fin >> size; //read in 12 records just regular (keep it simple)
     Records *student = new Records[size]; //make struct array 12 records total
	
	
	while (count < size)
	{
		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)
		
         //uses for loop to insert classes for student	
		for(int ndx = 0; ndx < student[count].numCourses ; ndx++) 
		fin >> student[count].courseCode[ndx].myClass;

		count++;
	}



//to test the input, should print all data on each student
  for(int i = 0; i < size ; i++)
  {
		cout << student[i].name <<endl;
		cout << student[i].numCourses <<" ";
		
       for(int x = 0; x < student[i].numCourses; x++)
		cout << student[i].courseCode[x].myClass <<" ";

		cout << endl;
  }


 return 0;
	
}


*note that I changed "Records record" to "Records student"
also, I made an array for courseCode[6] six total classes max, change this if a student can take on more classes, on a small file this should be ok, but on a larger file there probably is more effective way to save space.

I pulled line 23fin.ignore(1000, '\n');
and put it inside the loop to clear buffer each loop

added a delimiter to line 28 getline(fin, record[count].name); to
getline(fin, record[count].name, '\n'); end of line \n

I got rid of line 30 fin.ignore();

changed line 31 to a for loop and used fin >> instead of getline(fin, record[count].courseCode);
Last edited on
instead of reading the entire line of courses into record[count].courseCode, create a dynamic array


This means the struct changes from this:
1
2
3
4
5
6
struct Records
{
    string name;
    int numCourses;
    string courseCode;
};

to this, note the * because courseCode is a pointer to the dynamic array:
1
2
3
4
5
6
struct Records
{
    string name;
    int numCourses;
    string* courseCode;
};


For example,
John Milligan
3 CIS100 CIS105 MAT113


The above example would have
1
2
3
4
5
6
    name          = "John Milligan";
    numCourses    = 3;
    courseCode    = new string[3];
    courseCode[0] =  "CIS100";
    courseCode[1] =  "CIS105";
    courseCode[2] =  "MAT113";


The code in the program would have this sort of structure:
1
2
3
4
5
6
        fin >> record[count].numCourses;

        record[count].courseCode = new string[  record[count].numCourses  ];

        for (int i=0; i<record[count].numCourses; i++)
            fin >> record[count].courseCode[i]; 




It also affects the way the arrays allocated with new[] are de-allocated with delete[] at the end.

1
2
3
4
5
    // de-allocate the arrays when finished
    for (int i=0; i<count; i++)
        delete [] record[i].courseCode; 
        
    delete [] record;


Last edited on
Topic archived. No new replies allowed.