I wrote a code to get the text file names in a directory, get the length of the articles which are number of words in article, and the two steps seem to work well, and the outputs are what I wanted. Here I used linked list to store all the file names, length and so on. But when I want to deal with the results stored in these structures. For example, get the number of text files whose lengths are within a range like 0-3, or get the maximum length of all the articles. There are no outputs of the corresponding codes. Would you please help me to figure out what the problem is? Here is my code, I wrote it in C++, IDE is Xcode:
///////////////////////////////////////////////////////
//part 1 works correctly, part 2 and 3 is not working//
///////////////////////////////////////////////////////
#include <iostream>
#include <string>
using namespace std;
struct WORDS{
int number_of_words;
string name;;
WORDS *next;
};
WORDS *head = new WORDS();
WORDS *current = new WORDS();
int main(){
string article[] = {"article1", "article2", "article3", "article4", "article5"};
head->name=article[0];
head->number_of_words = 0;
current = head;
for (int i = 1; i < 5; i ++) {
current->next = new WORDS();
current->next->name = article[i];
current->next->number_of_words = i;
current = current->next;
}
//part 1: print all the file names and their lengths
cout << "length of article (in words): \n";
current = head;
while (current->name != "\0") {;
cout << current->name << " " << current->number_of_words << '\n';
current = current->next;
}
//part 2: find out the number of articles whose lengths are within range 0-3
current = head;
unsigned long count = 0;
while (current->name != "\0") {
if (current->number_of_words > 0 && current->number_of_words <= 3){
count ++;
cout << "Articles with length from 0 to 3 are: " << '\n';
cout << current->name << " " << current->number_of_words << '\n';
}
current = current->next;
}
cout << "Number of articles within range is: " << count << '\n';
//part 3:find out the maximum length of all the articles
WORDS *max = new WORDS();
current = head;
max = head;
while (current->name != "\0") {
if (current->next->number_of_words > max->number_of_words) {
max = current->next;
}
current = current->next;
}
cout << "maximum length of all articles is: " << max->number_of_words << '\n';
return 0;
}
The output is just like this: (sorry I can't post images yet) length of article (in words):
article1 0
article2 1
article3 2
article4 3
article5 4
After "cout" in part 1, no more is printed.