I'm supposed to create a program to sort a names.dat file. It compiles but nothing is displayed.

names.dat
    Collins, Bill
    Smith, Bart
    Michalski, Mel
    Griffin, Jim
    Sanchez, Manny
    Rubin, Sarah
    Taylor, Tyrone
    Johnson, Jill
    Adams, Andrew
    Moreno, Juan
    Wolfe, Bill
    Whitman, Jean
    Moretti, Bella
    Wu, Jeff
    Patel, Renee
    Harrison, Rose
    Smith, Cathy
    Conroy, Patrick
    Kelly, Sean
    Holland, Beth




#include<iostream>
#include<fstream>
#include<cctype>
using namespace std;
void read_names(string names[],ifstream& infile,int& index);
void selection_sort(string names[],int size);
void print_names(string names[],int size);

int main()
{
ifstream infile("names.dat");
if(!infile)
{
cout <<"" << endl;
}
int index = 0;
string array[100];
read_names(array, infile, index);
selection_sort(array,index);

print_names(array,index);
//system("pause");
return 0;
}

void read_names(string names[],ifstream& infile,int& index)
{
   index = 0;
   infile >> names[index];
   while(!infile.eof())
   {
       index++;
       infile >> names[index];
   }
}
void selection_sort(string names[],int size)
{
   for(int i=0; i<=size-2; i++)
   {
       int smallpos = i;
       for(int j=i; j<=size-1; j++)
       {
           if(names[j].compare(names[i])<0)
           smallpos = j;
       }
       if(smallpos!=i)
       {
           string temp = names[smallpos];
           names[smallpos] = names[i];
           names[i] = temp;
       }
   }
}
void print_names(string names[],int size)
{
   for(int i=0; i<size; i++)
   {
   cout << names[i] << " ";
   }
   cout << endl;
}

your code is so complicated i decided to write a new one
here is a code i wrote for you so you can sort your file
sorry if i didn't use a proper way i'm newbie too :D

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    string names[6] = { "Collins", "Bill",
                        "Smith", "Bart",
                        "Michalski", "Mel" };
    string access;
    ofstream newfile("names.dat", ios::out); // creating the file
    if (!newfile)
    {
        cout << "File not found" << endl;
        exit(1);
    }
    for (int i = 0; i < 6; ++i){
        newfile << names[i]<<" ";
    }

    newfile.close();

    ifstream openfile("names.dat", ios::in); // reading the file and close it

    if (!openfile){
        cout << "File not found" << endl;
        exit(1);

    }
    while (openfile >> access)
    {

        cout << access << endl;
    }

    openfile.close();

    string mode[6];

    ifstream modfile("names.dat", ios::in); //Sorting the file
    for (int i = 0; i < 6; ++i)
    {
        modfile >> mode[i];
    }
    sort(mode, mode+ 6);
    cout << endl << "After sorting" << endl << endl;
    for (int i = 0; i < 6; ++i)
    {
        cout << mode[i]<<endl;
    }
    modfile.close();
    system("PAUSE");
    return 0;
}

Seeing as your data file contains white space between last and first names, you'd be better off using std::getline for the reading.

void read_names(string names[], ifstream& infile, int& index)
{
    index = 0;
    std::string str;

    while (std::getline(infile, str))
    {
        if (!str.empty())
            names[index++] = str;
    }
}

At line 68: if(names[j].compare(names[i])<0) should be
if (names[j].compare(names[smallpos]) < 0) //or (names[j] < names[smallpos])

The outer loop in selection_sort should be for (int i = 0; i < size-1; i++)
The inner loop for (int j = i + 1; j < size; j++)

Don't forget to close the infile once you've finished reading.

Both of your solutions worked Thank you so much.

![![Okay guys. It turns out it didn't work. The code I used.

#include<iostream>
#include<fstream>
#include<cctype>
using namespace std;
void read_names(string names[],ifstream& infile,int& index);
void selection_sort(string names[],int size);
void print_names(string names[],int size);

int main()
{
ifstream infile("names.dat");
if(!infile)
{
cout <<"" << endl;
}
int index = 0;
string array[100];
read_names(array, infile, index);
selection_sort(array,index);

print_names(array,index);
//system("pause");
return 0;
}

void read_names(string names[], ifstream& infile, int& index)
{
    index = 0;
    std::string str;

    while (std::getline(infile, str))
    {
        if (!str.empty())
            names[index++] = str;
    }
}

void selection_sort(string names[],int size)
{
for (int i = 0; i < size-1; i++)
    {
       int smallpos = i;
       for (int j = i + 1; j < size; j++)
       {
if (names[j].compare(names[smallpos]) < 0);            smallpos = j;
       }
       if(smallpos!=i)
       {
           string temp = names[smallpos];
           names[smallpos] = names[i];
           names[i] = temp;
       }
   }
}
void print_names(string names[],int size)
{
   for(int i=0; i<size; i++)
   {
   cout << names[i] << " ";
   }
   cout << endl;
}

It's supposed to look like the one with the comment saying output your name.
b51fdd3fac9eebf4d3eed8b5324de1d1d1a8c3e48336c09624aa73f6c5480887

It's supposed to input the names.dat file which is a seperate file.

What is the purpose of the following code?

if(!infile)
{
    cout << "" << endl;
}

At line 45: if (names[j].compare(names[smallpos]) < 0); smallpos = j;
this translates to:

if (names[j].compare(names[smallpos]) < 0)
// do nothing
    ;
smallpos = j;

In print_names, you're putting a space between the names when what you want is a new line.

for(int i=0; i<size; i++)
{
    cout << names[i] << " "; // "\n" or '\n' not " "
}

You need to add <string> to your includes.
If you format all your code properly, the errors you're making will be far more easily recognized.

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.