Hello all I've look all over and I cann't find a way to sort a txt file.
This is my code......
This is my text file.....Code:#include <iostream.h> #include "fstream.h" class Counter { private: char A[100]; // array of characters int n; // character counter public: Counter(int counter=0); void readStr(); void writeStr(); int wordct(); void selectionSort(); }; // constructor used to initialize n zero Counter::Counter(int counter) : n(counter) {} // reads file story.txt and assigns to array A void Counter::readStr() { ifstream file; // creates fin file object file.open("test.txt"); // opens file fl.txt if(!file) // prints error if file fl.txt not opened properly cout << "Could not open fl.txt" << endl; while(file) // retreives a list of characters from fl.txt and stores them in A[i] for(int i=0;i !='\n'; i++) { A[n]=file.get(); //retrives character n++; // counts the character } } // prints file to screen void Counter::writeStr() { // prints list of characters just like whats inside fl.txt int i=0; while(A[i]!='~') { cout << A[i]; i++; } } void Counter::selectionSort() { char A[100]; int counter=0; if (n > 1) { int ti=0; for(int i=1; i <= counter-1; i++) { if (A[i] > A[ti]) { ti=i; } int t = A[ti]; A[ti]=A[counter-1]; A[counter-1]=t; //selectionSort(A, (counter)-1); } } } // free function prints menu to screen void menu() { cout << " ______________________________________________________\n"; cout << "| |\n"; cout << "| 1. Read String |\n"; cout << "| 2. Print String |\n"; cout << "| 3. Count Words |\n"; cout << "| 4. Exit |\n"; cout << "|______________________________________________________|\n"; } // main program void main() { Counter object; char input; do //do/while loop { menu(); // prints menu cin >> input; // user input cout << endl; // menu selection cases switch (input) { case '1': object.readStr(); // calls readStr member function cout << endl; break; case '2': object.writeStr(); // calls writeStr member function cout << endl; break; case '3': object.selectionSort(); object.writeStr(); break; case '4': cout << "Thanks for using my program" << endl; cout << endl; break; default : cout << "Invalid input" << endl; cout << endl; break; } }while(input!='4'); cout << endl; }
a
a
b
b
b
c
c
d
d
d
e
e
This is the output that I want....
a
b
c
d
e
Nothing fancy just want a simple sort....
I have no clue I have tried everything, any help would be apperiated. I have tried all different types of sorts but I cann't get anything wo work.
Thank You
ACHILLES