Problem with class arrays.

Hi, I'm writing a program that will read a text file, save the data into a class array, and then print it out.

I'm not sure why, but its not working as intended. It seems to be taking in the data, but not saving it to the array. Meaning that, I checked Li[Lcounter].setsalary(salary1) when Lcounter is equal to 0. The setsalary seems to be working then. However, when Lcounter becomes 1, I checked back at Li[0], and all the previous data seems to have gone.
This is the function where I put everything into the arrays.

When I'm printing out all the data, only the most recent data taken in is displayed. The rest are all gone.
My global variables.
1
2
3
4
5
6
7
8
9
int SMax=0;
int LMax=0;
int Scounter = 0;
int Lcounter =0;



student * St;
lecturer * Li;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  void insertLec(string name,string NRIC,string room,string salary,string year,string month,string date)
{
	Li = new lecturer [LMax];

	float salary1 = stof(salary);
	float year1 = stof(year);
	float month1 = stof (month);
	float date1 = stof(date);

	Li[Lcounter].setsalary(salary1);
	Li[Lcounter].setroom(room);
	Li[Lcounter].setname(name);
	Li[Lcounter].setNRIC(NRIC);
	Li[Lcounter].setyear(year1);
	Li[Lcounter].setmonth(month1);
	Li[Lcounter].setdate(date1);
	
}

This is the function that reads the data. It first reads the file to see how many of each class is needed, then reads again. At the second reading, it'll read the data and send them to insertLec or insertStd.
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
void inserData()
{
	int counter =0;
	int count = 0;
	string data[11];
	string buffer;
	string buffer2;

	string student = "student";
	string lecturer = "lecturer";
	ifstream inData;
	inData.open("data.txt");

	
	if(!inData)
		{	
			cout<< "error";
		}
	else
	cout<<"File Opened. 1st Reading Now"<<endl;

	stringstream iss;
	while(!inData.eof())
	{
		getline(inData,buffer);		//first read to find all the lecturers and students needed
		iss.str(buffer);
		getline(iss, buffer2,',' );
		if (buffer2 == "lecturer")
		{
			LMax++;
		}
		if (buffer2 == "student")
		{
			SMax++;	
		}
		iss.clear(); 
	}
	
	inData.close();

	inData.open("data.txt");

	if(!inData)
		{	
			cout<< "error";
		}
	else 
	cout <<"File Opened. 2nd Reading Now"<<endl;
	

	stringstream * ss;
	ss = new stringstream [LMax+SMax];
	while(!inData.eof())
	{
		getline(inData,buffer);			//after getting line into buffer, transfer line into a variable ss
		ss[count].str(buffer);			//for each individual line to reuse. Because errors will happen if you don't
		getline(ss[count],data[0],',' );//this part of the func will officially read the data and pass it to 
		if (data[0] == "lecturer")		//insertLec and insertStd for input into arrays
		{
			for(int x=1; x<8; x++)
			{
				getline(ss[count],data[x],',' );
			}
			insertLec(data[1],data[2],data[3],data[4],data[5],data[6],data[7]);
			Lcounter++;		
		}

		if (data[0] =="student")
		{
			for (int a=1; a<11;a++)
			{
				getline(ss[count],data[a],',' );	
			}
			insertStd(data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8],data[9],data[10]);
			Scounter++;
			
		}
		count++;
	}

	inData.close();
	delete[] ss;
}


This is my class lecturer cpp and header.
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
#pragma once
#include "person.h"
#include <string>
#include <iostream>
using namespace std;
class lecturer :
	public person
{
private:
	string room;
	float salary;

public:	
	int static countL;
	lecturer(void);
	~lecturer(void);
	lecturer(string room1, float salary1);

	void setroom(string room1);
	void setsalary(float salary1);

	string getroom() const;
	float getsalary() const;

	void increment();

};


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 "lecturer.h"


lecturer::lecturer(void)
{
	countL++;
}


lecturer::~lecturer(void)
{
}

lecturer::lecturer(string room1, float salary1)
{
	room=room1;
	salary=salary1;
	countL++;
}


void lecturer::setroom(string room1)
{
	room=room1;
}
void lecturer::setsalary(float salary1)
{
	salary=salary1;
}

string lecturer::getroom() const
{
	return room;
}

float lecturer::getsalary() const
{
	return salary;
}

void lecturer::increment()
{
	salary *= 1.1;
}

int lecturer::countL =0;
Last edited on
Topic archived. No new replies allowed.