How to count a new line

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.

Any help is appreciated!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype>
#include <fstream>


// Defined Constants /////////////////////
const size_t MAX_WORDS = 100;

using namespace 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;
}
Last edited on
*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?
Topic archived. No new replies allowed.