I am learning to code and found interesting task, but i do not know where to start in solving it. So i have a file with some titles and comments which need to be placed under the right title. So the first line of the input contains a number N which determines the quantity of the titles. Each row starts with a unique article id (integer), followed by the title in quotation marks. After there is no more titles, comments are given. At the beginning there is Title ID and comment (one word), but comments may recur for the same ID. so here is a structure of a file:

<N>                 
 <ID1>  "<Title1>"   
     ...                 
 <IDN> "<TitleN>"
 <ID1> <Comment1> 
        ...                
 <IDK> <CommentK>

Now in the output file each Title has two lines - first for the title and second one for comments. Titles must be in ascending order. And comments should be in reverse order (newest comments in the beginning) Structure of output file:

<Title1> <Comment11> ... < CommentK1>
...
<TitleN> <Comment1N> ... < CommentLN>
Example:

input:

3
1 "This is some title"
3 "Another title"
2 "And one more"
1 COmment
1 Another
3 Great
2 Awesome
3 Lucky
2 Stanley

output:

This is some title
Another COmment
And one more
Stanley Awesome
Another Title
Lucky Great

I do not now where to begin with.. Should I use arrays to save the data in memory and then try to sort it in the right pattern?Or is it better to load the text file into a data structure; in this case a linked list? Maybe someone can guide me in the right direction how to accomplish this task. (I do not ask to code it for me, just guide me or give some algorithm, it would be highly appreciated). Thanks!

Recommended Answers

All 5 Replies

You need a list of structures that are arranged by title, with a sub-structure of the comments associated. Then you can use either an array of structures (or in C++ a map ordered by title) or linked list. Make an effort to solve this and then post your code here if you want more substantial help.

What compiler are you using and what is considered "off-limits"?
You need to "group" your records by the ID.
If your grouping container does not keep the same order as the file, you will need to index the records when they are selected.

1) Test for the presence of quotes to determine the actual title
2) Reverse order the comments, join them together separated by a space.

So i have written this programm and it works, however, when i output the data to file, comments are written with one white space from the left side.

Then output should look like:

This is some title            // So it is the first title
Another COmment               // And then comments for this title, and so on..
And one more
Stanley Awesome
Another Title
Lucky Great

However, i get it like this:

This is some title
 Another COmment
And one more
 Stanley Awesome
Another Title
 Lucky Great

here is snippet of the code:

class LinkedList
    struct Node {
    char x[30];
    Node *next;
};

void addcomment(char * comment){

    commentList.addValue(comment);

}

// THis is how i read in comments:
int tid;
char * comment = new char[30];
while (inFile)
{
    inFile >> tid;

    inFile.getline(comment, 30);

    int index = getTitleIndex(list, tid, num);
    //cout << index << endl;
    list[index].addcomment(comment);
}

So i guess that getline actually reads the comments with whitespace that comes after the number, but how do i remove it so that they start from the left side as titles do? And if there are more comments for one title, they must remain seperated with whitespace, ofcourse.

EDIT: i can not use strings for this assignment.

If you can't use strings, then sort-order becomes very important.

Your code could definitely use some refactoring. However for your immediate problem, try skipping the space with ignore():

while (inFile)
{
    inFile >> tid;
    inFile.ignore();
    inFile.getline(comment, 30);
    int index = getTitleIndex(list, tid, num);
    //cout << index << endl;
    list[index].addcomment(comment);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.