I am writing a program for a class in which I need to scan a text document for a word, insert the word into an array, then later organize the array alphabetically. If the word is already in the array, it skips over that repeated word. This I have working.
The next step is where I'm stuck. Every time I process a word, I have to note the line number of where the word was found. If the word is in the same line multiple times, it'll only record the line number once. It is to record the line number in an int array.
*also note - I can only use cstrings, not "regular" strings, as I call them.
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype>
#include <fstream>
// Defined Constants /////////////////////
const size_t MAX_WORDS = 100;
usingnamespace std;
// structs
struct keyword_struct
{
char index[MAX_WORDS][MAX_WORDS]; // Word index
int line_number [MAX_WORDS][MAX_WORDS]; //counter for line numbers
};
//function prototypes
void print_index (int size, keyword_struct &words);
int main()
{
//variable declarations
keyword_struct words;
char file_name[MAX_WORDS];
ifstream file(file_name);
int position = 0;
char buffer[MAX_WORDS];
char buffer_space;
bool add = false;
int size = 0;
int line_counter = 1;
//initialize words.line_counter to all 0's
for (int a = 0; a < MAX_WORDS; a++)
{
for (int b = 0; b < MAX_WORDS; b++)
{
words.line_number[a][b] = 0;
}
}
//initialize words.index to all 0's
for (int a = 0; a < MAX_WORDS; a++)
{
for (int b = 0; b < MAX_WORDS; b++)
{
words.index[a][b] = '0';
}
}
// Get the input file name from the user
do {
file.clear(); // Clear any residual errors
cout << "Enter input file name: "; // Prompt the user
cin.getline(file_name, MAX_WORDS);
file.open(file_name,ios::in); // Attempt to open the file for input
if(file.good() != true) { // If file open failed...
cout << "?Unable to open \"" << file_name << "\"" << endl;
}
} while (file.good() != true);
// File is open. Process all words in the file and add them to our list
while(file.good() == true && file.peek() != EOF)
{
//add one to the line counter if the next item is the null, end of line character
buffer_space = file.get();
file.putback(buffer_space);
if (buffer_space == '\n')
{
line_counter++;
}
file >> buffer; // Read next word from the buffer
if (file.good() != true) continue; // Error - terminate the loop
// Scan loop: determine where new word should be inserted
add = true; // Default is to add the word
for(position = 0; position < size; position++)
{
// Avoid duplicates
if (strcmp(buffer,words.index[position]) == 0)
{
add = false; // Don't add to the list
break; // Stop the loop: word already in list
}
// Locate insertion point
if (strcmp(buffer,words.index[position]) < 0 )
{
add = true; // Add to the list
break; // Stop the loop: past desired insertion
}
}
//add line number
words.line_number[position][(line_counter - 1)] = line_counter;
// Make room at words.index[position] and insert the new word
if (size < (MAX_WORDS - 1) && add == true)
{
for (int idx = size; idx > position; idx--)
{
strcpy(words.index[idx], words.index[idx-1]);
}
strcpy(words.index[position], buffer);
size++; // Increment
}
}
print_index(size, words);
file.clear();
file.close();
return 0;
}
//functions-------------------------------------------------------------
void print_index (int size, keyword_struct &words)
{
for(size_t i = 0; i < (size); ++i)
{
cout << words.index[i];
cout << ": appears on line: ";
for (int a = 0; a < MAX_WORDS; a++)
{
if (words.line_number[i][a] != 0)
{
cout << words.line_number[i][a];
cout << ", ";
}
}
cout << endl;
}
return;
}
*update - this code appears to be working, counting lines as normal. The problem I'm facing now is that if a word is part of another word (ie - "this" and "is", "is" is part of the other word), it will print out the word "is", but it won't add a line_counter to it. I think because it's thinking it's part of that other word.
aka - how to I count the line for a word that is part of another word?
I am writing a program for a class in which I need to scan a text document for a word, insert the word into an array, then later organize the array alphabetically. If the word is already in the array, it skips over that repeated word.
So far so good.
The next step is where I'm stuck. Every time I process a word, I have to note the line number of where the word was found. If the word is in the same line multiple times, it'll only record the line number once. It is to record the line number in an int array.
Is this part of the assignment or is it the logic you choose to complete the previous assignment?