944,015 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 319
  • C++ RSS
Oct 31st, 2009
0

STRUGGLING!! I am in dire need of some help

Expand Post »
I am in desperate need of help! I am having an issue inserting the word into the correct list. Each array index is a letter of the alphabet, and the words need to be put into the correct index according to the first letter of the word. Can I get some help with fixing this issue with my insertWord function?

Here is the text file I am using:

I am excited to be taking this class. Programming isn't
easy but I feel I am getting better. One day when I am
out of the military I hope to work for some IT business.
The job can't be part-time either!
67dd a09dd.

Here is the program file:


# include <iostream>
# include <iomanip>
# include <fstream>
# include <string>
# include <cctype>
# include <cstddef>

using namespace std;

const int MAX_SIZE = 26;

struct wordRec {
string newWord;
wordRec *previous;
wordRec *next;
};

void programIntro();
wordRec* initializeList ();
void initializeArray (wordRec *[], int& index);
void readTextFile (ifstream& wordIn, wordRec *letters[], int& index, int& wrdIndex);
void checkInputWord (ifstream& wordIn, string& word);
void insertWord (ifstream& wordIn, wordRec* [], int& index, string& word, int& wrdIndex);
void wordListPlacement (ifstream& wordIn, wordRec *letters[], int& index, int& wrdIndex);

int main (int argc, char* argv[]) {

wordRec* letters[MAX_SIZE];
wordRec *head = NULL;

int count;
int indexes;
int wordCount;
string words;
int wordIndex;

ifstream wordIn;

wordIn.open (argv[1]);

if (!wordIn) {
cout << "Error! Text File did not open!" << endl;
system ("pause");
return 1;
}
else
initializeArray (letters, indexes);
readTextFile (wordIn, letters, indexes, wordIndex);
wordIn.close ();

system ("pause");
return 0;
}

void initializeArray (wordRec *letters[], int& index)

{
for (index = 0; index < MAX_SIZE; index++) {
letters[index] = NULL;
}

return;
}

void readTextFile (ifstream& wordIn, wordRec *letters[], int& index, int& wrdIndex)

{
wordRec *head = letters[index];
string word;
char character;
char letter;

while (wordIn) {
wordIn >> word;

// calculates the specific list each word will go into
character = word[0];
letter = toupper(character);
wrdIndex = letter - 'A';

// checks the word format for errors
checkInputWord (wordIn, word);

// uppercases all the words in the file
for (int indx = 0; indx < word.size(); indx++) {
word[indx] = static_cast<char>(toupper(word[indx]));
}

insertWord (wordIn, letters, index, word, wrdIndex);
}
return;
}

void checkInputWord (ifstream& wordIn, string& word)

{

// strips off any punctuation if there is any
for (int index = 0; index < word.length(); index++) {
if ((ispunct(word[index])) && (word[index] != '\'') && (word[index] != '-')) {
word.erase(index);
}
}

// ignores the word if it contains any digits and does not add it to the list!
for (int index = 0; index < word.size(); index++) {
if (isdigit (word[index])) {
wordIn.ignore (word[index], word.length() );
}
}

return;
}

void insertWord (ifstream& wordIn, wordRec *letters [], int& index, string& word, int& wrdIndex)

{
wordRec *head = NULL;
wordRec *newNode = NULL;

if (head == NULL) {
newNode = new (nothrow) wordRec;
newNode->newWord = word;
newNode->previous = NULL;
newNode->next = newNode;

if (head != NULL)
head->previous = newNode;

head = newNode;
letters[index] = head;
letters[index]->next = head;
letters[index]->previous = head;

}
return;
}
Last edited by drake2212; Oct 31st, 2009 at 11:07 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
drake2212 is offline Offline
6 posts
since Jul 2009
Oct 31st, 2009
0
Re: STRUGGLING!! I am in dire need of some help
Please format code, and paste in the tags.
Reputation Points: 10
Solved Threads: 10
Light Poster
niXman is offline Offline
39 posts
since Oct 2009
Oct 31st, 2009
0
Re: STRUGGLING!! I am in dire need of some help
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Nov 1st, 2009
0
Re: STRUGGLING!! I am in dire need of some help
Better use maps than double link list.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How do I find the total sale for my program??
Next Thread in C++ Forum Timeline: Identify someone's age by entering the IC number...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC