Click here to Skip to main content
15,887,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

What I have tried:

#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";
 
}
Posted
Updated 20-Mar-22 17:23pm
v3
Comments
Garth J Lancaster 12-Mar-17 1:09am    
is there a reason you're using your own sort mechanism ? if so, I'd suggest stepping through your program with the debugger to make sure its doing what you want it to do - not what you 'think' the computer should be doing.

Else, there's always qsort https://www.tutorialspoint.com/c_standard_library/c_function_qsort.htm or http://www.anyexample.com/programming/c/qsort__sorting_array_of_strings__integers_and_structs.xml

First, add a semi-colon to this:
outputFile.open("earlfoz.txt");

Next, comment out the outputFile in
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;
}
then output the sorted result line by line to file
for(i=0;i<20;i++)
{
  outputFile << name[index[i]] << "        "
  << grade[index[i]] << endl;
}
 
Share this answer
 
Quote:
I want to know what I did wrong and how can I fix my program to output the 20 results alphabetically

Lets guess, you get the sorted list on screen but the file contain the list as it was input.
Reread your code and pay attention to what you do with outputFile, a bell should start to ring.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900