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;
}
}