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