concatenate two char arrays into single char array..

I am trying to concatenate two words from a file together. ex: "joe" "bob" into "joe bob". I have provided my function(s) below. I am somehow obtaining the terminal readout below. I have initialized my memory (I have to use dynamic, dont suggest fixing that). I have set up my char arrays (I HAVE TO USE CHAR ARRAYS (c-style string) DONT SUGGEST STRINGS) I know this is a weird way to do this, but it is academic. I am currently stuck. My file will read in to my tempfName and templName and will concatenate correctly into my tempName, but I am unable to correctly get into my (*playerPtr).name. Please help!! I cant figure out what is wrong..i think I am going out of bounds with the pointer somehow....



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
/* this is my terminal readout
joe bob
           <- nothing is put into (*playerPtr).name, why not?
joe bob joe bob
seg fault*/
/****************************************************************/
//This is here to show my struct/playerInit
////////////////////
#include <iostream>
#include <fstream>
#include <ctime>
#include <stdlib.h>

using namespace std;

struct player{
char *name;
};
////////////////
/*here isetup readin file*/
int main()
{
int numPlayers = 5;
player * playerPtr = new player[numPlayers];
player * playerHome = playerPtr;
for(int i=0;i<numPlayers;i++
   {
     (*playerPtr).name = new char[40];
     playerPtr++;
   }
char *finName2 = new char[11];
playerPtr = playerHome;
cout << "Enter file name...(Players.txt)" << endl;
cin >> finName2;
readPlayer(finName2,playerPtr,numPlayers);
delete[]finName2;
}

/*************************************************************/
//The problem occurs in this function I have noted where

void readPlayer(char *finName2, player *playerPtr,int numPlayers)
{
player *playerHome = playerPtr;
ifstream fin;
char *tempfName= new char[20];
char *templName= new char[20];
char *tempN = new char[40];
cout << numPlayers << endl;
char *tempfHome = tempfName;
char *templHome = templName;
char *tempHome = tempN;
fin.open(finName2);

if(!fin.good())
{
cout << "Error with player file!" << endl;       
}
else
{
fin >> tempfName;
fin >> templName;

//cout << tempfName << " " << templName << endl;
while(fin.good())
{
  for(int i =0;i<1;i++)
  {
      strconcat(tempN,tempfName);
      strconcat(tempN,templName);
      strcopy((*playerPtr).name,tempfName); //this doesnt work :(
      cout << tempN << endl;
      playerPtr++;
      tempN = tempHome;
      tempfName = tempfHome;
      templName = templHome;
  //fin >> tempfName;
 // fin >> templName;
      cout << (*playerPtr).name <<  endl;

      }
   }
}

}
/**************************************************/
//these are my custom strcpy/strcat functions (we had to make them, this is academic)
void strconcat(char *dest, const char *source)
{
  while(*dest != '\0')
       {
         dest++;
       }  
  *dest = ' ';
  dest++;
  while((*dest++ = *source++) != '\0');
}

char * strcopy(char *destination, const char* source)
{
   char *p = destination;
   while(*p++ = *source++);
   return destination;
}
Unclear how you're getting a segfault because your program won't compile.


1>.\main.cpp(27) : error C2143: syntax error : missing ')' before '{'
1>.\main.cpp(35) : error C3861: 'readPlayer': identifier not found
1>.\main.cpp(69) : error C3861: 'strconcat': identifier not found
1>.\main.cpp(70) : error C3861: 'strconcat': identifier not found
1>.\main.cpp(71) : error C3861: 'strcopy': identifier not found
@ OP: After you add the forward declarations and the other stuff that AbstractionAnon mentioned, your segfault is from your strconcat() function. Or more accurately from the fact that you are not reading from your source file from within your for loop on Line 67, so you are never reaching the end of that file and therefore never exiting the while loop. This is causing your program to repeatedly write to your 'tempN' variable until you over run the buffer you set aside for it.
Last edited on
Topic archived. No new replies allowed.