d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Rather Long C++ Pointer Assignment - Help!
Prev12
Add Reply New Topic New Poll
Member
Posts: 950
Joined: Feb 19 2009
Gold: 2,595.00
Sep 25 2013 07:58pm
Quote (falco37 @ Sep 25 2013 09:51am)
hmm but the thing is it only printed out the first word which is charmeleon.. maybe where i have
Code
for (int i=0;i<p_w->num_words;i++)
        cout << p_w->list[i] << " ";


since num_words is only 13, it is stopping there... should i implement a for loop before to count the total number of characters in the whole list instead?


so i tried setting instead of num_words, i put 100 and now instead of crashing it, it just ends 2 loops after the word charmeleon.. any help would be appreciated it
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Sep 25 2013 08:03pm
I don't really get what you did, I think you missed a word in your last sentence. Anyways, the problem may not be at this line at all. Can you repost the whole code as it is now? It's getting messy to read with all the changes.
Member
Posts: 950
Joined: Feb 19 2009
Gold: 2,595.00
Sep 25 2013 08:07pm
Quote (flyinggoat @ Sep 25 2013 09:03pm)
I don't really get what you did, I think you missed a word in your last sentence. Anyways, the problem may not be at this line at all. Can you repost the whole code as it is now? It's getting messy to read with all the changes.


Code
/*
* Topics: Multidimensional Arrays, Pointers, Dynamic Allocation, Structs
*
*/
#include <iostream>
#include <cstring>
#include <string>
using namespace std;


/* Globally defined constants
*/
const int MAX_WORD_SIZE = 20;

/* Words structure
*
* This has three member variables list: which will contain a list
* of cstrings, size: the max number of words the list can store and
* num_words: the number of cstrings in list.
*/

struct Words {
unsigned int num_words; // Number of words currently in list
unsigned int max_words; // The total size allocated to the list.
char **list; // The list storing the words
};



/* Function prototypes:
* To be implemented
*/

Words * newList(const unsigned int max_words);
Words * newList(const char words[]);
int deleteList(Words *p_w);
int printList(Words *p_w);
int appendList(Words *p_w, const char words[]);
int appendList(Words *dst, const Words *src);
char* findWord(const Words *p_w, const char word[]);
int removeWord(Words *p_w, const char word[]);



/* Main function
*
* Note the newList and deleteList functions must be implemented before appendList so the
* calls to appendList are initally commented out.
* Your code should work with the main below. Including the appendList functionality that is initially commented out.
*/


int main()
{
Words *pokemon, *tb;
char *word;
int ok=0;
string poke="charmeleon mewtwo pikachu charmander squirtle caterpie raichu butterfree abra kadabra alakazam slowpoke spoink"; //declaring a string
char team_a[]="rayquaza mew snorlax"; //creating array c_string
string team_b="weavile shuckle ampharos toxicroak mrmime";



cout << poke.c_str() << endl;
pokemon=newList(5); // Words * newList(const unsigned int max_words);
ok=deleteList(pokemon); // int deleteList(Words *p_w);
cout << "OK? "<< ok << endl;
pokemon=newList(poke.c_str()); // Words * newList(const char words[]);
tb=newList(team_a); // Words * newList(const char words[]);
cout << "*pokemon"<< endl;
printList(pokemon); // int printList(Words *p_w);
cout << "*tb"<< endl;
printList(tb); // int printList(Words *p_w);
/*
//
// Note the newList and deleteList functions must be implemented before appendList so the
// calls to appendList are initally commented out.

ok=appendList(tb,team_b.c_str()); // int appendList(Words *p_w, const char words[]);
cout << "*tb"<< endl;
printList(tb); // int printList(Words *p_w);

ok=appendList(pokemon,tb); // int appendList(Words *dst, const Words *src);
cout << "*pokemon"<< endl;
printList(pokemon); // int printList(Words *p_w);
word=findWord(pokemon,"charmeleon"); // char* findWord(const Words *p_w, const char word[]);
cout << "*word "<< *word << " word " << word << " address " << &amp;word << endl;
ok=removeWord(pokemon,"squirtle"); // int removeWord(Words *p_w, const char word[]);
cout << "*pokemon"<< endl;
printList(pokemon); // int printList(Words *p_w);
*/
return 0;
}

/* Function: newList
*
*
* Takes the max_words of the list and creates a new Words
* with the given max_words without assigning any words, sets
* num_words to zero. returns the new list to the calling function.
* max_words must be greater than zero, otherwise newList returns
* NULL.
*/

Words * newList(const unsigned int max_words)
{
if(max_words < 1)
return NULL;
// Allocate list
Words * p_w = new Words;
p_w->list = new char*[p_w->max_words];
//for (int i=0;i<max_words;i++)

p_w->max_words = max_words;
p_w->num_words = 0;

return p_w;
}

/* Function: newList
*
* Overloaded version of newList, this one takes a character
* array with words separated by spaces, for example:
*
* "charmeleon mewtwo pikachu charmander squirtle caterpie raichu butterfree abra kadabra alakazam slowpoke spoink"
*
* has many words. This newList will allocate enough space
* for the words, then the list will be initialized to those
* words and returns the list to the calling function. If
* words is empty, newList does nothing and returns NULL.
*/

Words * newList(const char words[])
{
int spaceCount = 0, wordBegin = 0 , wordLength = 0, wordCount = 0;

// duplicate the words to handle literal input
char *dup = strdup(words);
int len = strlen(dup);

//Can't allocate zero memory
if(len == 0)
return NULL;

Words *p_w = NULL;
p_w = new Words;
for (int i =0;i<len;i++) //for loop to count the number of spaces
if (dup[i]==' ') //with knowing number of spaces, you know that there is 1 more word than space
spaceCount++;
p_w->list = new char*[p_w->max_words];
for (int i=0;i<(spaceCount+1); i++) //words on the list will be number of spaces + 1
p_w->list[i] = new char[MAX_WORD_SIZE];

string dupStr = (string)dup;

for (int i=0;i<len;i++)
{
if (words[i] != ' ')
wordLength ++;
else
{ string word1 = dupStr.substr (wordBegin,wordLength);
wordBegin = i+1;
char * localStr = new char[word1.length() + 1];
strcpy(localStr, word1.c_str());
p_w->list[i] = localStr;
wordLength = 0;
}
}

return p_w;
}

/* Function: deleteList
*
* Takes a dynamically allocated Words and deletes
* the list and then the Words itself, returns 0.
* If the Words is NULL deleteList does nothing and
* returns -1;
*/

int deleteList(Words *p_w)
{
if(p_w == NULL)
return -1;

if (p_w->list)
{
for (int i=0;i<(p_w->num_words);i++)
delete[] p_w->list[i];
delete[] p_w->list;
}
delete p_w;
return 0;

}

/* Function: printList
*
* Prints the words in the Words on a single Line
* with a single space between words. There is no
* space after the last word, but there is a newline,
* returns 0 for successful print. If p_w is NULL,
* printList does nothing returns -1.
*/

int printList(Words *p_w)
{
if(p_w == NULL)
return -1;
char *list = "";
if (p_w->list != NULL)
for (int i=0;i<100;i++)
cout << p_w->list[i] << "\n";

return 0;
}

/* Function: appendList
*
* Takes a Words and a character array consisting of words
* separated by spaces, for example:
*
* "charmander squirtle caterpie raichu"
*
* Contains four words. appendList will take the Words
* and append these words to list. If the Words does not
* have enough space, appendList will dynamically allocate
* more space to list to allow these new words to fit.
* Returns the number of words appended. If words is empty
* or if p_w is NULL then appendList does nothing and returns -1.
*/

int appendList(Words *p_w, const char words[])
{
// duplicate the words to handle literal input
char *dup = strdup(words);
int len = strlen(dup);

if(p_w == NULL || len == 0)
return -1;

int word_count = 0;

// FUNCTION CODE GOES HERE

return word_count;
}

/* Function: appendList
*
* Takes two Wordss, appends the contents of src
* to dst. If dst does not have enough space
* appendList should dynamically allocate additional space
* then append the contents of src to dst, returns
* 0 on success. If either Words is NULL appendList does
* nothing and returns -1.
*/

int appendList(Words *dst, const Words *src)
{
if(dst == NULL || src == NULL)
return -1;

// FUNCTION CODE GOES HERE

return 0;
}

/* Function: findWord
*
* Finds the first occurrence of the word in the Words
* returns a pointer to that word in the list. Otherwise
* returns NULL. If p_w is NULL return NULL
*/

char* findWord(const Words *p_w, const char word[])
{

// FUNCTION CODE GOES HERE

return NULL;
}


/* Function: removeWord
*
* If p_w is null, returns -1. Otherwise, searches for every
* occurrence of word[], and removes that word of the list, returns
* the number of words removed.
*
*/

int removeWord(Words *p_w, const char word[])
{
int num_removed = 0;
if(p_w == NULL)
return -1;
else
{
num_removed++;

}

return num_removed;
}


/*in c++, the arrow operator is used to access number variables and function when using a pointer
to an object and the dot operator when accesing objects directly
example

Words *pokemon, poke;

pokemon ->list //pointer acess
*(pokemon) ->list//pointer access

poke.list // object access
/*/


this is the whole code and i meant to say that instead of setting the i<num_words, I put i<100 to get more goes through the loop and it stops 2 after charmeleon
Member
Posts: 950
Joined: Feb 19 2009
Gold: 2,595.00
Sep 25 2013 11:18pm
I fixed my newlist function a bit but now i'm having issues near the end..

Code
Words * newList(const char words[])
{
int spaceCount = 0, i=0;

// duplicate the words to handle literal input
char *dup = strdup(words);
int len = strlen(dup);
//Can't allocate zero memory
if(len == 0)
return NULL;

Words *p_w = NULL;

for (int i =0;i<len;i++) //for loop to count the number of spaces
if (dup[i]==' ') //with knowing number of spaces, you know that there is 1 more word than space
spaceCount++;
spaceCount++;
Words * newList(const unsigned int spaceCount);
char * dupStr;
dupStr = strtok (dup, " ");
while (dupStr != NULL)
{
dupStr = strtok(NULL," ");
}
//p_w->list = dupStr;
return p_w;
}


i created Words *p_w = NULL but i'm not exactly sure how to convert the dupStr into the p_w->list.. when it returns p_w, it's returning a NULL Value... any help is appreciated guys, deadline is coming up eventually haha
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Sep 26 2013 07:03pm
Words * newList(const unsigned int spaceCount); -> don't understand what this line is doing here. It's disturbing, you should be allocating p_w, not some new array that holds the same name as your function, that will prolly have your compiler scream.

Start with "Words *p_w = (Words*)malloc(sizeof(Words));"
Once you have space count, allocate the list and set the data within p_w:

p_w->nm_word = spaceCount;
p_w->list = (char**)malloc(sizeof(char*)*spaceCount);

Now, within your strtok loop, assign each list element the right size and copy over the content of dupstr:

int z=0;
dupStr = strtok(dup, " ");
while(dupStr)
{
//copy first since you called strtok once before entering the loop
p_w->list[z] = (char*)malloc(strlen(dupStr)+1); //+1 for null byte
strcpy(p_w->list[z], dupStr);

//on to the next " "
dupStr = strtok(0, " ");

//increment z
z++
}

This post was edited by flyinggoat on Sep 26 2013 07:04pm
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll