Guys, I'm actually almost done. Basically what I have to do is to read from file with a list of words, sort them alphabetically and write them into another file. I am almost done, but the only thing is, the assignment says that we shouldn't change the case of words, but I am changing everything to lowercase, because I'm using arrays to compare letters. So how can I keep the case? Or change everything to lowercase and then change the originals back after sorting, while writing?
#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{
ifstream inFile, inSortFile;
ofstream outFile;
ifstream checkFile;
string inFileName, outFileName;
string word;
int count = 0;
do
{
cout << "What is the name of the file you would like to read from? ";
cin >> inFileName;
inFile.open(inFileName.c_str());
if(inFile.fail()) cout << "The file called " << inFileName << " does not exist." << endl;
}
while(inFile.fail());
cout << "The file was opened." << endl;
cout << "Where would you like to output the data: ";
cin >> outFileName;
checkFile.open(outFileName.c_str());
if(!checkFile.fail())
{ //opening outer if
cout << "A file with the name " << outFileName << " already exists." << endl
<< "Do you wish to overwrite the file? Enter n or N if no, anything else if yes: ";
char answer;
cin >> answer;
if(answer=='n'||answer=='N')
{ //opening inner if
cout << outFileName << " will not be overwritten.\n";
system("PAUSE");
exit(1);
} //closing inner if
} //closing outer if
checkFile.close();
outFile.open(outFileName.c_str());
cout << "The data was successfully output to " << outFileName << "." <<endl;
int i=0;
while(inFile.good())
{ //opening while
inFile >> word ;
for(i=0; i<word.length(); i++) word.at(i)=tolower(word.at(i));
{ //opening for
outFile << word << " ";
count++;
} //closing for
} //closing while
inFile.close();
outFile.close();
const int SIZE=count;
string Words[SIZE];
inSortFile.open(outFileName.c_str());
int j=0;
while(j<SIZE)
{
inSortFile >> Words[j];
j++;
}
inFile.close();
string max=Words[0];
int a=0;
int b=0;
int c;
while(a<SIZE)
{ //opening first while
c=a;
max=Words[c];
while(c<SIZE)
{ //opening second while
if(max<Words[c])
{ //opening if
max=Words[c];
b=c;
Words[b]=Words[a];
Words[a]= max;
} //closing if
c++;
} //closing second while
a++;
} //closing first while
outFile.open(outFileName.c_str());
for(int k=SIZE; k>0; k--)
outFile << Words[k-1] << endl;
inFile.close();
outFile.close();
system("pause");
return 0;
}