Confused about dynamic memory allocation + arrays

For now I'm just trying to read in an input file that looks like:
1
2
3
4
5
6
7
8
Eastern Conference

Atlantic Division
Boston Celtics
Brooklyn Nets
New York Knicks
Philadelphia 76ers
Toronto Raptors


while dynamically allocating the basketball team names (underneath the Atlantic Division) to an array of structs.

here's my code:
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
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<fstream>
#include<cctype>
#include<string>
using namespace std;

struct NBA_Team 
{
    char* name;
    char conference[8];
    char division[10];
    unsigned short wins;
    unsigned short losses;
    float pct;
};

const int teamnames = 0;
const int win = 1;
const int loss = 2;
const int percent = 3;
const int col = 4;
const int dteamn = 5;

void getname (const string&, NBA_Team *, NBA_Team &);


int main()
{
	NBA_Team NBAT;
	NBA_Team teaminfo[dteamn];
	
	string con, divi;
	char buffer[16], buffer1[16];
	
	const string namefile = "c:/temp/namefile.txt";
	const string scorefile = "c:/temp/scorefile.txt";

	getname(namefile, teaminfo, NBAT);
	
		
		

	system("PAUSE");
}
void getname (const string &f, NBA_Team *p, NBA_Team &q)
{
	
	string con, divi;
	char buffer[90], buffer1[16];
	char* namez = new char[90];
	
	ifstream fin(f);
    if (!fin)
    {
        cerr << "Unable to open file " << f << endl;
        exit(1);
    }
	
	fin >> q.conference >> con >> q.division >> divi;

	for (int i = 0; i < dteamn; i++)
		{
			fin>>buffer;
			char* pch = strchr(buffer,'\n');
			
			while (pch==NULL) 
			{
				strcat(buffer," ");
				fin >> buffer1;
				strcat(buffer,buffer1);
				
				pch=strchr(buffer1,'\n');
				
			
			}
			
			p[i].name = new char[strlen(buffer+1)];
			p[i].name = buffer;
			cout << p[i].name<<" " << p[0].name<<endl;
		}
		
		
		
		
		


		cout << q.conference << " " << con << " " << q.division << " " << divi << endl;
		for (int i = 0; i < dteamn; i++)
		{
			cout << p[i].name<<endl;
		}
}


do ignore the myriad of const variables and the like, i'd like help on the getname function itself.

I'm having two main issues:
a) I set char* pch = strchr(buffer,'\n'); and pch=strchr(buffer1,'\n'); so i can check if the buffer/buffer1 has a newline. it isn't really working as in my while loop, it loops indefinitely with no end when theoretically it should stop when fin gets the word "Celtics" of my input file. any tips on that? I can't use getline or any c++ string functions.

b)
when i took out my while loop to test what was happening with my array:
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
void getname (const string &f, NBA_Team *p, NBA_Team &q)
{
	
	string con, divi;
	char buffer[90], buffer1[16];
	char* namez = new char[90];
	
	ifstream fin(f);
    if (!fin)
    {
        cerr << "Unable to open file " << f << endl;
        exit(1);
    }
	
	fin >> q.conference >> con >> q.division >> divi;

	for (int i = 0; i < dteamn; i++)
		{
			fin>>buffer;
			char* pch = strchr(buffer,'\n');
			
			//while (pch==NULL) 
		//	{
				strcat(buffer," ");
				fin >> buffer1;
				strcat(buffer,buffer1);
				
				pch=strchr(buffer1,'\n');
				
			
			//}
			
			p[i].name = new char[strlen(buffer+1)];
			p[i].name = buffer;
			cout << p[i].name<<" " << p[0].name<<endl;
		}
		
		
		
		
		


		cout << q.conference << " " << con << " " << q.division << " " << divi << endl;
		for (int i = 0; i < dteamn; i++)
		{
			cout << p[i].name<<endl;
		}
}


my output is:
1
2
3
4
5
6
7
8
9
10
11
12
Boston Celtics Boston Celtics
Brooklyn Nets Brooklyn Nets
New York New York
Knicks Philadelphia Knicks Philadelphia
76ers Toronto 76ers Toronto
Eastern Conference Atlantic Division
76ers Toronto
76ers Toronto
76ers Toronto
76ers Toronto
76ers Toronto
Press any key to continue . . .

I'm wondering why my dynamically allocated char arrays to p[i].name isn't saving and is instead being overwritten. i've indexed i so i'm assuming it should allocate new char array memory to each new index.



thank you!

edit: i'm able to save the buffer into p[i] with the strcpy() function, but why could i not save it via p[i].name = buffer; ?
Last edited on
> so i can check if the buffer/buffer1 has a newline
`buffer' would never have a newline,
fin>>buffer; discards whitespace


> but why could i not save it via p[i].name = buffer; ?
there you are assigning a pointer (leaking memory, btw)
when you use `strcpy()' the pointer does not change, it changes the buffer pointed by it.
i'm able to save the buffer into p[i] with the strcpy() function, but why could i not save it via p[i].name = buffer;


Because setting one pointer equal to another pointer doesn't do anything at all with what is pointed to.

Say there's a house at 100 First Street and a house at 100 Third Street, and there's a garage sale sign near both. If it originally says the garage sale is at 100 First Street and we change it to say the sale is at 100 Third Street, the house at 100 First Street doesn't suddenly become a copy of the house at 100 Third Street. The only thing we've changed was the sign.

That's what you're doing with the pointer assignment. Changing the sign.
Topic archived. No new replies allowed.