HELP WITH PROJECT c++ (Array, string file, sort)

Okay, I have this project due today. I have written the codes.
I need to read a .txt file and put it in an array.
Next, i need to sort the array alphabetically.
I was capable of reading the string file, however it does not display properly.
It suppose to display: first, lastname
however, mine displays: first,
lastname
Here is my code. It would be much appreciated if anyone could help me.
1. Displaying the name properly.
2. sort all of the names alphabetically
3. send the sorted name into different .txt file

This is the file (names.txt):
Collins, Bill
Smith, Bart
Allen, Jim,
Griffin, Jim
Stamey, Marty
Rose, Geri,
Taylor, Terri
Johnson, Jill,
Allison, Jeff
Looney, Joe
Wolfe, Bill,
James, Jean
Weaver, Jim
Pore, Bob,
Rutherford, Greg
Javens, Renee,
Harrison, Rose
Setzer, Cathy,
Pike, Gordon
Holland, Beth


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// This program lets the user enter a filename.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;

void selectionSort(string [], int);


int main()
{
	ifstream inputFile;
	string filename;


	const int ARRAY_SIZE = 20; // Array size
	string names[ARRAY_SIZE];    // Array with 100 elements
	
	int count = 0;
	
								 // Get the filename from the user.
	cout << "Please enter the name of the file to read numbers for Number Analysis Program:\n";
	cin >> filename;

	// Get the numbers of array 
	cout << "Please enter the numbers of name to read:" << endl;
	cin >> names[count];
	
	// Open the file.
	inputFile.open(filename);

	// If the file successfully opened, process it.
	if (inputFile)
	{
		// Read the names from the file and
		// display them.
		while (count < ARRAY_SIZE && inputFile >> names[count])
			count++;

		// Close the file.
		inputFile.close();

		// Display the names read.
		cout << "The names are: " << endl;
		for (int index = 0; index < count; index++)
		{
		
			cout << names[index] << " ";
			cout << endl;
			}


	}
	else
	{
		// Display an error message.
		cout << "Error opening the file.\n";
	}
	
	selectionSort(names, ARRAY_SIZE);

	system("pause");
	return 0;
}

void selectionSort(string array[], int size)
{
	int startScan, minIndex, minValue;
	
	for (startScan = 0; startScan < (size - 1); startScan++)
	{
		minIndex = startScan;
		minValue = array[startScan];
		for (int index = startScan + 1; index < size; index++)
		{
			if (array[index] < minValue)
			{
				minValue = array[index];
				minIndex = index;
			}
		}
		array[minIndex] = array[startScan];
		array[startScan] = minValue;
	}
}
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
#include <iostream>
#include <string>
#include <fstream>

void selectionSort( std::string array[], int size ) ;

int main()
{
    std::cout << "Please enter the name of the file: " ;
    std::string filename ;
    std::cin >> filename ; // std::getline( std::cin, filename ) ;

    if( std::ifstream file{ filename } ) // if the file was opened successfully
    {
        const int MAX_NAMES = 20 ;
        std::string names[20] ;

        std::cout << "Please enter the maximum number of names to read: " ;
        int count ;
        std::cin >> count ;
        if( count > MAX_NAMES ) count = MAX_NAMES ;

        // read up to a maximum of count names from the file
        std::string first_name, last_name ;
        int nread = 0 ; // number of names actually read
        // read last name, first name from the file
        while( nread < count && file >> last_name >> first_name )
        {
            // and store it as first name, last name in the array
            names[nread] = first_name + ' ' + last_name ;
            ++nread ;
        }

        // sort the names
        selectionSort( names, nread ) ;

        // display the names
        // ...
    }

    else std::cerr << "Error opening the file.\n";
}
Thank you so much.
Although i don't what std: i see the pattern of the code.
Appreciate it so much
Topic archived. No new replies allowed.