Hey, In my duplicateWords function, I have been trying to figure out for a couple of hours how to create duplicates for 4 different names in the list. Will someone help lead me in the right direction in how to complete this task?

My program is reading in 50 words from an input file, sorted in alphabetically, capitalize first 20 letters, but I am having difficulty duplicating 4 different words in the list.

Example of duplication:

marvin
marvin
jack
jack
mary
mary
bill
bill

#include <fstream>
#include <string>


using namespace std;

struct nodeType {
    string info;
    nodeType *link;
};

void createList(nodeType*& first, nodeType*& last);
void alphasort (nodeType*& first, nodeType*& last);
void upperCase (nodeType*& first, nodeType*& last);
void duplicateWords (nodeType*& first, nodeType*& last);
void printList(nodeType*& first);

int main(int argc, char *argv[]) 
{
    nodeType *first, *last;
    string words;

    ifstream inData;
    ofstream outData;

    createList(first,last);
    alphasort (first,last);
    upperCase (first,last);
    duplicateWords (first,last);
    printList(first);

    system ("PAUSE");
    return 0;
}

void createList(nodeType*& first, nodeType*& last) 
{
    ifstream inData("input.txt"); // opens input file
    string words;
    nodeType *newNode;

    first = new nodeType;
    first->info = string("head");
    first->link = NULL;
    last = first;

    while(inData >> words) // reads data in input file
    {
        newNode = new nodeType; // create new node
        newNode->info = words;
        newNode->link = NULL;
        last->link = newNode;
        last = newNode;
    }
}




void nodeSwap(struct nodeType *p, struct nodeType *q) // swaps node
{
    struct nodeType *t1, *t2, *t3;
    if (p != q) 
    {
        t1 = p->link; t2 = q->link; t3 = q->link->link;
        p->link = q->link;
        q->link = t1;
        t2->link = t1->link;
        t1->link = t3;
    }
}



void alphasort (nodeType*& first, nodeType*& last) // sorts alphabetically by using Selection Sort
{
    struct nodeType *i,*j,*min;

    for (i = first; i->link != NULL; i = i->link) 
    {
        min = i;
        for (j = i->link; j->link != NULL; j = j->link) 
        {
            if (j->link->info < min->link->info) min = j;
        }
        nodeSwap(i,min);
    }
}


void upperCase (nodeType*& first, nodeType*& last)
{
struct nodeType *i;
int wordCount = 0;

for (i = first; i->link != NULL; i = i->link)
{
char tempWord[20] = {0};
strcpy(tempWord, i->info.c_str());
tempWord[0] = toupper(tempWord[0]);
i->info = tempWord;

wordCount++;

if (wordCount == 21)
break; // only want 1st 20 words
}
}


void duplicateWords (nodeType*& first, nodeType*& last)
{
     
struct nodeType *i;
int wordCount = 0;

for (i = first; i->link != NULL; i = i->link)
{
char temporaryWord[3];

if (i->link->info == temporaryWord[i])

wordCount++;      
}    
     
     
}

void printList(nodeType*& first) 
{
    ofstream outData("output.txt"); // Opens the outputfile

    nodeType *current = first->link;
    
    while (current != NULL) 
    {
        outData <<current->info <<endl; // sends results to output file
        current = current->link;
    }
}

How about you explain this code. Tell me what each line does.
Then tell me what you think you need to do in order to duplicate a word.

void duplicateWords (nodeType*& first, nodeType*& last)
{
 
struct nodeType *i;
int wordCount = 0;
 
for (i = first; i->link != NULL; i = i->link)
{
char temporaryWord[3];
 
if (i->link->info == temporaryWord[i])
 
wordCount++;      
}    
 
 
}

Edited 7 Years Ago by firstPerson: n/a

The code below is my last attempt. In this code, I created an array to store 4 temporary words from my word list, I used info to get information from the word list and compared it to temporary words. When the first word in info is presented, I used wordCount++ to try to duplicate the word when it found a word in the list. It probably doesn't make sense, but I thought it would work. Does it look like I am in the right direction though?


I believe one way is hard code the names in the array like "bill", "tommy" and then match it with words in the list, but I don't think that is the best way to do it.

This is the latest code that I have to duplicate 4 words in my list. I am currently experiencing "base operand of '->' is not a pointer errors", and I can't figure out how to fix it. Am I going in the right direction in duplicating 4 words in my word list and not all of them?

void duplicateWords (nodeType*& first, nodeType*& last)
{

string temp;
 
for(int i = first; i->link != NULL; i = i->link)
{
newNode = new nodeType;
newNode->link = i->link;
strcpy (i->temp,newNode->temp);
i->link = newNode;
}   
     
     
}

This is the latest code that I have to duplicate 4 words in my list. I am currently experiencing "base operand of '->' is not a pointer errors", and I can't figure out how to fix it. Am I going in the right direction in duplicating 4 words in my word list and not all of them?

void duplicateWords (nodeType*& first, nodeType*& last)
{

string temp;
 
for(int i = first; i->link != NULL; i = i->link)
{
newNode = new nodeType;
newNode->link = i->link;
strcpy (i->temp,newNode->temp);
i->link = newNode;
}   
     
     
}

newNode doesn't have a type, or at least if it does, it's not declared here. It's just some variable name. If its type is a pointer to nodType, you need to declare that:

nodeType* newNode = new nodeType;

And how is the program to know what you want duplicated and what you don't? Also, "duplicate" can mean all sorts of things. Where do you want these duplicated nodes to go? At the end? Right after the original node?

Thanks for the reply.

I want to duplicate any 4 words in my word list, and it could be anywhere in the output. I probably prefer to have it at the beginning of the list though and hopefully that is where my code is heading.

I forgot to change to newNode to a pointer and I appreciate that. My code is doesn't like the pointers that I put. I changed string to nodeType *string to see if it would fix the error, but it doesn't. I am still receiving that pointer error. It seems to be something else wrong with the code.

void duplicateWords (nodeType*& first, nodeType*& last)
{
 
for(int i = first; i->link != NULL; i = i->link)
{
nodeType *temp;
nodeType *newNode = new nodeType;
newNode->link = i->link;
strcpy (i->temp,newNode->temp);
i->link = newNode;
}   
     
     
}

Full program:

#include <fstream>
#include <string>


using namespace std;

struct nodeType {
    string info;
    nodeType *link;
};

void createList(nodeType*& first, nodeType*& last);
void alphasort (nodeType*& first, nodeType*& last);
void upperCase (nodeType*& first, nodeType*& last);
void duplicateWords (nodeType*& first, nodeType*& last);
void printList(nodeType*& first);

int main(int argc, char *argv[]) 
{
    nodeType *first, *last;
    string words;

    ifstream inData;
    ofstream outData;

    createList(first,last);
    alphasort (first,last);
    upperCase (first,last);
    duplicateWords (first,last);
    printList(first);

    system ("PAUSE");
    return 0;
}

void createList(nodeType*& first, nodeType*& last) 
{
    ifstream inData("input.txt"); // opens input file
    string words;
    nodeType *newNode;

    first = new nodeType;
    first->info = string("head");
    first->link = NULL;
    last = first;

    while(inData >> words) // reads data in input file
    {
        newNode = new nodeType; // create new node
        newNode->info = words;
        newNode->link = NULL;
        last->link = newNode;
        last = newNode;
    }
}




void nodeSwap(struct nodeType *p, struct nodeType *q) // swaps node
{
    struct nodeType *t1, *t2, *t3;
    if (p != q) 
    {
        t1 = p->link; t2 = q->link; t3 = q->link->link;
        p->link = q->link;
        q->link = t1;
        t2->link = t1->link;
        t1->link = t3;
    }
}



void alphasort (nodeType*& first, nodeType*& last) // sorts alphabetically by using Selection Sort
{
    struct nodeType *i,*j,*min;

    for (i = first; i->link != NULL; i = i->link) 
    {
        min = i;
        for (j = i->link; j->link != NULL; j = j->link) 
        {
            if (j->link->info < min->link->info) min = j;
        }
        nodeSwap(i,min);
    }
}


void upperCase (nodeType*& first, nodeType*& last)
{
struct nodeType *i;
int wordCount = 0;

for (i = first; i->link != NULL; i = i->link)
{
char tempWord[20] = {0};
strcpy(tempWord, i->info.c_str());
tempWord[0] = toupper(tempWord[0]);
i->info = tempWord;

wordCount++;

if (wordCount == 21)
break; // only want 1st 20 words
}
}


void duplicateWords (nodeType*& first, nodeType*& last)
{
 
for(int i = first; i->link != NULL; i = i->link)
{
nodeType *temp;
nodeType *newNode = new nodeType;
newNode->link = i->link;
strcpy (i->temp,newNode->temp);
i->link = newNode;
}   
     
     
}

void printList(nodeType*& first) 
{
    ofstream outData("output.txt"); // Opens the outputfile

    nodeType *current = first->link;
    
    while (current != NULL) 
    {
        outData <<current->info <<endl; // sends results to output file
        current = current->link;
    }
}
for(int i = first; i->link != NULL; i = i->link)

Check Line 4 in the code in your post. it consists the above code.You have declared variable i to be of type int then you use the -> operator . THAT would be the error. I am not going to give the solution But a tip is to think what exactly you want in the for loop. And then you should figure it out.

Edited 7 Years Ago by Sky Diploma: code tag error

Thanks for replies.

Right now, I am trying to resolve this error: no matching function for call to `strcpy(std::string[3], const char*)'

void duplicateWords (nodeType*& first, nodeType*& last)
{
 
for(nodeType *i = first; i->link != NULL; i = i->link)

{
string tempWord[3];
struct nodeType *newNode,*temp;
newNode = new nodeType;
newNode->link = i->link;
strcpy (tempWord,i->info.c_str());
i->link = newNode;
}   
     
     
}

I added a few adjustments to my function, but this error "no matching function for call to `strcpy(const char*, std::string&)' " keeps holding me back from my program to run. Any hints?

void duplicateWords (nodeType*& first, nodeType*& last)
{
int counter = 0;
for(nodeType *i = first; i->link != NULL && counter < 4; ) // i = i->link removed

{
struct nodeType *newNode;

newNode = new nodeType;
newNode->link = i->link;
strcpy (i->info.c_str(), newNode->info); // copy i's info into newNode
i->link = newNode;

i = newNode->link;
counter++;
}

}

I finally got it compiled with this. However, the names that I am duplicating have disappeared in the output, but the rest of the list is still there. I thought that what I had would actually work but now I don't know what is going on.

void duplicateWords (nodeType*& first, nodeType*& last)
{
int counter = 0;
for(nodeType *i = first; i->link != NULL && counter < 4; ) // i = i->link removed

{
struct nodeType *newNode;

newNode = new nodeType;
newNode->link = i->link;
//strcpy (i->info.c_str(), newNode->info); // copy i's info into newNode
i->info = newNode->info;
i->link = newNode;

i = newNode->link;
counter++;
}

}

Thank you sir.

I reversed newnode->info and i->info, it printed out the duplicates like it was suppose to. However, at the top of the word list in the output it says "Head". Please see output example below.

The reason I put line 15 is because it was the only way to get my words to print duplicates. If I took out line 15, it will print out "5 heads", but 4 of the heads is where I want to store my duplicates. So, when I go to print without this line, it will just give me "head" 5 times. I have set my counter different ways to get rid of that extra head in the output but no luck so far. I am still working on it though. I have the duplicates as shown below, but that extra head is there.

Example of current Output:


Head
Jamal
Jamal
Abigail
Abigail
Alexander
Alexander
Anthony
Anthony


This is what my function looks like:

void duplicateWords (nodeType*& first, nodeType*& last)
{
int counter = 0;
for(nodeType *i = first; i->link != NULL && counter <= 4; ) // i = i->link removed

{
struct nodeType *newNode;

newNode = new nodeType;
newNode->link = i->link;
newNode->info = i->info;
i->link = newNode;

i = newNode->link;
counter++;
}

}
strcpy (i->info.c_str(), newNode->info.c_str()); // copy i's info into newNode
i->link = newNode;

strcpy takes in c style string.

Thanks firstPerson for your reply. I took strcpy out and did newNode->info = i->info;

Are there any hints to how I can take out "head" at the top of my output in my last post? I tried to adjust the counter several times.

Edited 7 Years Ago by NinjaLink: n/a

Well its pretty unclear for me to understand this, Please Post down the Input , Desired Output and Current Output. That way it would be easy.

My apologies.

Below is my input file, current output, and expected output. The difference between current and expected output is that "head" is at the top of the list in current, and I am still trying to get rid of it!

I have tried to adjust my counter in my duplicateWords function, but I experienced no luck. Any ideas?


Input file: (50 Random names)

kathy
mary
bill
matt
aaron
mike
marion
barber
cannon
durell
shelby
laura
anthony
christian
kerry
kristen
matthew
.
.
.

Current Output:


Head
Aaron
Aaron
Abigail
Abigail
Alexander
Alexander
Anthony
Anthony
Ava
Barber
Benson
Bill
Cannon
Chloe
Christian
Christopher
Daniel
David
Durell
Elizabeth
.
.
.

Expected Output:


Aaron
Aaron
Abigail
Abigail
Alexander
Alexander
Anthony
Anthony
Ava
Barber
Benson
Bill
Cannon
Chloe
Christian
Christopher
Daniel
David
Durell
Elizabeth
.
.
.
.

duplicateWords function:

void duplicateWords (nodeType*& first, nodeType*& last)
{
int counter = 0;
for(nodeType *i = first; i->link != NULL && counter <= 4; ) // i = i->link removed

{
struct nodeType *newNode;

newNode = new nodeType;
newNode->link = i->link;
newNode->info = i->info;
i->link = newNode;

i = newNode->link;
counter++;
}

}

Full Program:

#include <fstream>
#include <string>


using namespace std;

struct nodeType {
    string info;
    nodeType *link;
};

void createList(nodeType*& first, nodeType*& last);
void alphasort (nodeType*& first, nodeType*& last);
void upperCase (nodeType*& first, nodeType*& last);
void duplicateWords (nodeType*& first, nodeType*& last);
void printList(nodeType*& first);

int main(int argc, char *argv[]) 
{
    nodeType *first, *last;
    string words;

    ifstream inData;
    ofstream outData;

    createList(first,last);
    alphasort (first,last);
    upperCase (first,last);
    duplicateWords (first,last);
    printList(first);

    system ("PAUSE");
    return 0;
}

void createList(nodeType*& first, nodeType*& last) 
{
    ifstream inData("input.txt"); // opens input file
    string words;
    nodeType *newNode;

    first = new nodeType;
    first->info = string("head");
    first->link = NULL;
    last = first;

    while(inData >> words) // reads data in input file
    {
        newNode = new nodeType; // create new node
        newNode->info = words;
        newNode->link = NULL;
        last->link = newNode;
        last = newNode;
    }
}




void nodeSwap(struct nodeType *p, struct nodeType *q) // swaps node
{
    struct nodeType *t1, *t2, *t3;
    if (p != q) 
    {
        t1 = p->link; t2 = q->link; t3 = q->link->link;
        p->link = q->link;
        q->link = t1;
        t2->link = t1->link;
        t1->link = t3;
    }
}



void alphasort (nodeType*& first, nodeType*& last) // sorts alphabetically by using Selection Sort
{
    struct nodeType *i,*j,*min;

    for (i = first; i->link != NULL; i = i->link) 
    {
        min = i;
        for (j = i->link; j->link != NULL; j = j->link) 
        {
            if (j->link->info < min->link->info) min = j;
        }
        nodeSwap(i,min);
    }
}


void upperCase (nodeType*& first, nodeType*& last)
{
struct nodeType *i;
int wordCount = -1;

for (i = first; i->link != NULL; i = i->link)
{
char tempWord[20] = {0};
strcpy(tempWord, i->info.c_str());
tempWord[0] = toupper(tempWord[0]);
i->info = tempWord;

wordCount++;

if (wordCount == 20)
break; // only want 1st 20 words
}
}


void duplicateWords (nodeType*& first, nodeType*& last)
{
int counter = 0;
for(nodeType *i = first; i->link != NULL && counter <= 4; ) // i = i->link removed

{
struct nodeType *newNode;

newNode = new nodeType;
newNode->link = i->link;
newNode->info = i->info;
i->link = newNode;

i = newNode->link;
counter++;
}

}

void printList(nodeType*& first) 
{
    ofstream outData("output.txt"); // Opens the outputfile

    nodeType *current = first->link;
    
    while (current != NULL) 
    {
        outData <<current->info <<endl; // sends results to output file
        current = current->link;
    }
}

Edited 7 Years Ago by NinjaLink: n/a

So "head" is supposed to be in there or not? Your output ends with dots after "Elizabeth". "head" is after "Elizabeth" in the alphabet, so the dots are ambiguous. If you have 50 names in the file, how many words are you supposed to have in the output, and is "head" one of them? And how many do you have now?

head is not suppose to be there. I just put the dots to indicate that the names go up to 50. Everything is correct except that extra head sitting at the top.

head is not suppose to be there. I just put the dots to indicate that the names go up to 50. Everything is correct except that extra head sitting at the top.

Well, if "head" isn't supposed to be there, why did you put it there in the first place in line 43?

I dont know why I do these things lol. Thank you all for your help. Problem solved.

This question has already been answered. Start a new discussion instead.