ofstream problem

I made a program that ask for 20 names and grade.
Then the program automatically arrange it alphabetically.
Now I tried to output the result the a ".txt" file. But the .txt file didn't arrange the names in the notepad
I want to know what I did wrong and how can I fix my program to output the 20 results alphabetically

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
 ofstream outputFile;
 outputFile.open("earlfoz.txt")
 string name[20];
 int grade[20];
 int index[20];
 int i, j;

 for(i=0;i<20;i++)
 {
  cout << "Please enter name: ";
  cin >> name[i];
  outputFile << name[i] << endl;
  cout << "Please enter grade: ";
  cin >> grade[i];
  outputFile << grade[i] << endl;
  
 }

 for(i=0;i<20;i++)
 {
  index[i]=i;
 }

 for(i=0;i<20;i++)
 {
  for(j=i+1;j<20;j++)
  {
   int temp;
   
   if(name[index[i]] > name[index[j]])
   {
    temp = index[i];
    index[i] = index[j];
    index[j] = temp;
   }
  }
 }
 cout << endl;

 for(i=0;i<20;i++)
 { 
  cout << name[index[i]] << "        "
   << grade[index[i]] << endl;
 }

 cin.ignore();
 cin.get();

 outputFile.close();
 cout << "Done!\n";
 
}
It looks like the code is writing to the outputFile as the names and grades are entered by the user, before they have been sorted.
Topic archived. No new replies allowed.