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;
}

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.

Sounds good so far.

I am almost done, but the only thing is, the assignment says that we shouldn't change the case of words,

So don't change the case.

but I am changing everything to lowercase, ...

So don't.

...because I'm using arrays to compare letters.

Why does that necessitate changing the case?

Either you are under a misconception vis-a-vis the assignment
or
You haven't given us the entire picture vis-a-vis upper/lower case

Side thought --
When you compare, why not just compare the lower case values using tolower() ?

Sounds good so far.


So don't change the case.


So don't.


Why does that necessitate changing the case?

Either you are under a misconception vis-a-vis the assignment
or
You haven't given us the entire picture vis-a-vis upper/lower case

Side thought --
When you compare, why not just compare the lower case values using tolower() ?

Oh so here's the assignment:
Write a program which sorts a list of words contained in a file alphabetically. Your program will sort these words alphabetically, and store the sorted list in a new file. You shouldn’t change the case of any of the letters involved.

So suppose I have a file with the following words:

File edit format view help fact

The program with that snippet tolower() is going to sort them and output them all fine, it's just that the uppercase letters that were initially in the file will become lowercase. So with tolower() my output is:

edit
fact
file
format
help
view

See that's perfect, but the thing is, originally I had File, now I have file. But the assignment says don't change cases. Now if I remove the tolower(), my output becomes:

File
File
File
File
edit
edit
edit
edit
fact
fact
fact
fact
format
format
format
format
format
format
help
help
help
help
view
view
view
view

Well actually in this case I have a question. Why are the words getting repeated so many times? I know why File is not being sorted, uppercase values are lower than lowercase values. But I am not changing my loop, so why am I seeing so many additional words? By the way, my output should be this:

edit
fact
File
format
help
view

Okay, I figured out why I was getting so many outputs, the loop:

for(i=0; i<word.length(); i++)

was only used so that I convert all characters to lowercase in a word, but when I dropped that part it was affecting the rest of the code.

Oh so here's the assignment:
Write a program which sorts a list of words contained in a file alphabetically. Your program will sort these words alphabetically, and store the sorted list in a new file. You shouldn’t change the case of any of the letters involved.

See that last line? Do not change the letters. Period. Remove the line for(i=0; i<word.length(); i++) word.at(i)=tolower(word.at(i)); and think of another way to sort.
Like maybe use the toupper() in the sort area to compare but not change the characters.

See that last line? Do not change the letters. Period. Remove the line for(i=0; i<word.length(); i++) word.at(i)=tolower(word.at(i)); and think of another way to sort.
Like maybe use the toupper() in the sort area to compare but not change the characters.

I've tried to use the toupper() in the sort area, but I'm getting a lot of weird errors, I think it's because all the strings are now in an array and my toupper is not working. Is there any other way to accomplish this?

Can someone please help?

I've tried to use the toupper() in the sort area, but I'm getting a lot of weird errors, I think it's because all the strings are now in an array and my toupper is not working. Is there any other way to accomplish this?

Completely useless information.

First thing is stop ALL conversion. Do not use toupper() at all. Do not use tolower() at all. Get the program working with no conversions.

When you get that working, post code, explain what it's doing correctly (so far) and explain how you need it changed. With examples.

Can someone please help?

Can you please post your final code?

Completely useless information.

First thing is stop ALL conversion. Do not use toupper() at all. Do not use tolower() at all. Get the program working with no conversions.

When you get that working, post code, explain what it's doing correctly (so far) and explain how you need it changed. With examples.

I'm having trouble with the same thing. It is hard to sort letters that are both uppercase and lowercase. Is there another way of doing this since the code above turns all words into lower case first and then sorts it?

Member Avatar for v3ga

Why don't u use strcmpi()?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.