Game Face
Game Face

Reputation: 11

Why are the words from the text file displayed in the program in alphabetical order instead of the order of how the text files put it?

So, I have the work mostly done here, but I can't seem to figure out how to have the words in the order of how the text files displays it. It just takes the words from the text files, and puts the words in alphabetical order. Text Files are at the bottom of this page.

Text File Analysis

Write a program that reads the contents of two text files and compares them in the following ways:

• It should display a list of all the unique words contained in both files. • It should display a list of the words that appears in both files. • It should display a list of the words that appears in the first file, but not the second. • It should display a list of the words that appears in the second file, but not the first. • It should display a list of the words that appears in either the first or second file, but not in both.

Hint: Use set operations to perform these analyses. Also, see Chapter 10 for a discussion of string tokenizing.

This is what I have so far:

#include <iostream>
#include <set>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;

void displaySet(set<string>);
void displayDifference(set<string>, set<string>);

int main()
{
    ifstream inputFile;
    string name;
    const int MIN_VALUE = 1;
    const int MAX_VALUE = 2;
    int numbers;
    unsigned seed = time(0);
    
    srand(seed);
    
    set<string> firstFileWords;
    set<string> secondFileWords;
    
    inputFile.open("firstTextFile.txt");
    cout << "Reading data from the first file.\n";
    
    cout << endl;
    
    while (inputFile >> name)
    {
        firstFileWords.insert(name);
    }
    
    displaySet(firstFileWords);
    
    inputFile.close();
    
    inputFile.open("secondTextFile.txt");
    
    cout << endl;
    
    cout << "Reading data from the second file.\n";
    
    while (inputFile >> name)
    {
        secondFileWords.insert(name);
    }
    
    cout << endl;
    
    displaySet(secondFileWords);
    
    cout << endl;
    
    cout << "The differences between the two files:\n";
    
    cout << endl;
    
    displayDifference(firstFileWords, secondFileWords);
    
    displayDifference(secondFileWords, firstFileWords);
    
    cout << endl;
    
    numbers = rand() % 2 + 1;
    
    if(numbers = 2)
    {
        displaySet(secondFileWords);
        cout << "\nThe Second File Words";
    }
    else
    {
        displaySet(firstFileWords);
        cout << "\nThe First File Words";
    }
    return 0;
}

void displaySet(set<string> s)
{
    for (auto element : s)
    {
        cout << element << " ";
        cout << endl;
    }
}

void displayDifference(set<string> set1, set<string> set2)
{
    vector<string> result(set1.size() + set2.size());
    
    auto iter = set_difference(set1.begin(), set1.end(),
                               set2.begin(), set2.end(),
                               result.begin());
                               
    result.resize(iter - result.begin());
    
    for (auto element : result)
    {
        cout << element << " ";
    }
}

These are the words from the text files I mentioned:

//firstTextFile.txt

It should display a list of all the unique words contained in both files. It should display a list of the words that appears in both files. It should display a list of the words that appears in the first file, but not the second. It should display a list of the words that appears in either the first or second file, but not in both.

//secondTextFile.txt

It should display a list of all the unique words contained in both files. It should display a list of the words that appears in both files. It should display a list of the words that appears in the second file, but not the first. It should display a list of the words that appears in either the first or second file, but not in both.

The differences between them are the third sentences.

Upvotes: 1

Views: 304

Answers (1)

Captain Hatteras
Captain Hatteras

Reputation: 519

Instead of reading into a set, use a std::list for bidirectional iteration or std::forward_list for forward iteration. The words in your text file were displayed in alphabetical order because std::set automatically sorts its data.

Upvotes: 0

Related Questions