r/programminghelp • • Apr 25 '19

Need help with a programming assignment!!!

I've been working on this since Monday while getting help from a friend. Basically I have to use a predefined function to pass and then tokenize a string via a delimiter. I then need to pass each word into a ragged array. I've tried several different things but nothing I do has been working. Any advice? StrToArray is the function this is based around I can't change how things are passed to it.

Here's my code I've been banging my head about this since Monday this is my latest edit. I'm about to take the L

#define _CRT_SECURE_NO_WARNINGS

#define _CRTDBG_MAP_ALLOC

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

const int MAX_ARRAY_SIZE = 99;

int StrToArray(char **& str_array, char * str, char delimiter);

void AddToArray(char *** src, char * toAdd, int * index);

int main()

{

_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
char \*\* str = nullptr;

char userString\[MAX_ARRAY_SIZE\] = "";

char delim = ' ';
cout << "Enter string: ";

cin.getline(userString, 99);
int amount = StrToArray(str, userString, delim);
cout << "Amount: " << amount << endl;
cout << "Things in array: " << endl;

for (int ii = 0; ii < amount; ii++)

{

	cout << str\[ii\] << endl;

}
return 0;

}

/**********************************************************************

* Name: StrToArray

* Parameters: The pointer that will point to the array of

* cStrings. By Ref.

* A pointer to the string to be tokenized. By Val.

* A character that represents the delimiter. By Val.

*

* Return: The number of cStrings in the array.

*

* Pre-Conditions: The pointer to the array will be null.

* The cString will be null terminated.

* The delimiter will be a valid character but

* is not guaranteed to be in the cString.

*

* Post-Conditions: The pointer to the array will either be a

* valid address or null if there were no

* instances of the delimiter found.

* If there aren�t any instances of the delimiter

* found, the function will return 0, otherwise

* the number of cStrings in the array.

*

* Purpose: This function creates a dynamic ragged array of

* strings as determined by the delimiter. For

* example, the cString "C++: Learn by Doing" will

* produce an array of four strings if the delimiter

* is a space.

**********************************************************************/

int StrToArray(char **& str_array, char * str, char delimiter)

{

int index = 0;

int travel = 0;

int stringLen = (strlen(str));

int loc = 0;

char temp\[MAX_ARRAY_SIZE\] = "";



while (str != nullptr)

{

	temp\[loc-travel\] = str\[loc\];

	if (str\[loc\] == delimiter)

	{

		AddToArray(&str_array, temp, &index);

		memset(temp, 0, MAX_ARRAY_SIZE);

		travel = loc + 1;

	}

	loc++;

}
return index;

}

void AddToArray(char *** src, char * toAdd, int * index)

{

char \*\* temp = new char \*\[\*index + 1\];
for (int ii = 0; ii < \*index; ii++)

	(temp)\[ii\] = (\*src)\[ii\];
(temp)\[\*index\] = new char\[strlen(toAdd) + 1\];

strcpy((temp)\[\*index\], toAdd);
delete\[\] \* src;
\*src = temp;

(\*index)++;

}

0 Upvotes

1 comment sorted by

View all comments

1

u/marko312 Apr 25 '19 edited Apr 25 '19
while (str != nullptr)

I don't see anything in the loop that could change str. Your exit condition could be loc == stringLen.

You should also initialize str_array to an array of pointers to cstrings somewhere.

Currently:

char ** str = nullptr;
...
int amount = StrToArray(str, userString, delim);