So for an assignment at school, I have to dynamically allocate arrays of struct Pieces then fill the array with code from a .txt file. It seems to go just fine, but whenever I check after filling the array, it only ever states the last element. If I try to check any specific element or if I try incrementing, it crashes. I have poured over this code and I can't figure out what is wrong...please help
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
struct Pieces
{
char* word;
int jump;
};
void reset(char*, int);
int strLen(char*);
void createArr(char*, char*);
int main()
{
char* temp;
int numWords;
int numKeys;
int count = 0;
Pieces* cipher;
Pieces* resett;
ifstream input;
temp = new char[20];
input.open("Project4Data.txt");
input >> numWords;
input >> numKeys;
cipher = new Pieces[numWords];
resett = cipher;
while(input >> temp)
{
createArr(cipher->word, temp);
cipher->word = temp;
//cout << cipher->word << " ";
input >> cipher->jump;
//cout << cipher->jump << endl;
count++;
if(count == numWords)
break;
cipher++;
reset(temp, 20);
}
input.close();
cipher = resett;
cout << *(cipher)->word ;
delete[] resett;
delete[] cipher;
return 0;
}
int strLen(char* word)
{
int s = 0;
//cout << word;
for(int x = 0; *(word + x) != '\0'; x++)
s++;
return s;
}
void reset(char* arr, int size)
{
for(int x = 0; x < size; x++)
*(arr + x) = '\0';
}
void createArr(char* a, char* b)
{
//cout << b << b;
a = new char[strLen(b)];
}
Heres the text file we're supposed to use:
23 11
Java 2 linux 3 fear 0 pool 2 do 0 red 1 lock. 1 I 0 random 2 computers, 0 not 0 the 0 open 2 car! 2 C, 0 lack 0 of 0 dog 1 green 2 C++ 0 bottle 2 wrong, 2 them. 0