| | |
STRUGGLING!! I am in dire need of some help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2009
Posts: 6
Reputation:
Solved Threads: 0
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;
}
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; 24 Days Ago at 11:07 pm.
0
#4 24 Days Ago
Better use maps than double link list.
![]() |
Other Threads in the C++ Forum
- Previous Thread: How do I find the total sale for my program??
- Next Thread: C++ Calculator Program
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






