I'm having trouble trying to importing the names from my text file and displaying them correctly.
Every time I do, they display in the order, but they display in this order
I'm not very experienced in file I/O in either C or C++, but if the names are in the same format in the entire document, i.e. last name, \nfirst name\nlast name, and so on, couldn't you just separate each string by every second newline character, put the two in a single string, then cout the entire thing and repeat until the EOF?
Once again I don't know much about file I/O in C or C++, but that's how I would work around it.
Still Nothing, I figured out how to display the text correctly using a getline()
but now transferring that data into a vector, I get nothing on the screen.
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
usingnamespace std;
int main()
{
// Empty Vector Holding All The Names from The File
vector<string> nameList;
// Read Names from File UnsortedNames.txt
ifstream openFile("UnsortedNames.txt");
//Pushes Each Line of Data In Text
//To Word, Which Is Pushed to
//The Vector nameList
string word;
while (getline(openFile,word))
nameList.push_back(word);
//Sorting Using STL Liberary
sort(nameList.begin(), nameList.end());
//Output To Text File
ofstream outputfile;
outputfile.open("orderedNamesText.txt");
//Loop To Display Names
//Also Outputs to Text File
cout << "Name List:";
outputfile << "Name List:";
for (int i = 0; i < nameList.size(); i++)
{
cout << nameList[i] << '\n';
outputfile << nameList[i] << '\n';
}
}