NewandUneducated
NewandUneducated

Reputation: 25

Return words from a file alphabetically

I'm attempting a little self learning while on spring break I've created a program that will accept words store them in a file then print them out to the console. I am looking for the best way to print them in alphabetical order instead of the order they were inputted. I have seen a few examples online of how to do this with a vector but we haven't really touched on that in class yet so I'm wondering if there is a way to do it without using vectors?

This is what I have so far that takes input stores it in the file until "quit" is entered then it will print the words back to the console I'm using the using namespace std; for simplicity's sake I know most view it as bad practice. thanks for any help.

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main(){
    string input;

    ofstream file;
    file.open("words.txt");

    while(input != "quit"){
        cin >> input;
        file << input << std::endl;
        if(input == "quit"){
            file.close();
        }
    }

    fstream dataFile("words.txt", ios::in);

    if(dataFile.fail())
    {
        cout << "Error opening File..." << endl;
        return 0;
    }

    while(dataFile >> input)
    {
        cout << input << std::endl;
    }

    dataFile.close();   


    return 0;
}

Upvotes: 2

Views: 55

Answers (2)

Marius Bancila
Marius Bancila

Reputation: 16328

A vector is a homogenous container of variable size. It's the default container you should use, provided there are no special requirements for what you're doing.

If you put the words in a std::vector, you can then use std::sort() to sort it alphabetically. Here is an example:

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
 
int main()
{
    std::vector<std::string> words {"this", "is", "an", "example"};
    std::sort(words.begin(), words.end());
    
    for(auto const & word : words)
        std::cout << word << '\n';
}

This will print, in Yoda speak, the following:

an
example
is
this

Filling the vector with the words from the input file should not be difficult.

Upvotes: 2

TheEagle
TheEagle

Reputation: 5992

You can use an std::list, but aside from the variable declaration it's the same in usage. Here's the example taken from the linked site:

#include <algorithm>
#include <iostream>
#include <list>
 
int main()
{
    // Create a list containing integers
    std::list<int> l = { 7, 5, 16, 8 };
 
    // Add an integer to the front of the list
    l.push_front(25);
    // Add an integer to the back of the list
    l.push_back(13);
 
    // Insert an integer before 16 by searching
    auto it = std::find(l.begin(), l.end(), 16);
    if (it != l.end()) {
        l.insert(it, 42);
    }
 
    // Print out the list
    std::cout << "l = { ";
    for (int n : l) {
        std::cout << n << ", ";
    }
    std::cout << "};\n";
}

I'm using the using namespace std; for simplicity's sake I know most view it as bad practice

As long as you use only and ONLY things from the std namespace, it's ok I think. Just you must think of that, when you want to use something from another namespace, you will have to re-write your whole code to prepend std:: to everything from that namespace.

Upvotes: 1

Related Questions