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
// This program lets the user enter a filename.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
usingnamespace std;
void selectionSort(string [], int);
int main()
{
ifstream inputFile;
string filename;
constint 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;
}
}
On line 78: why are you comparing an integer with a string? minValue is an integer, and the array is an array of strings.
Same on line 80: why assign a string to an integer?