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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int MAX_NAMES = 10;
const int MAX_COMBINED = 60;
const int MAX_SINGLE = 20;
//Declare Prototypes
//void displayInfo();
void getNames(char [][MAX_COMBINED + 1], const int , const int, const int);
void displayNames(char [][MAX_COMBINED + 1], const int);
void bubbleSort(char[][MAX_COMBINED +1], const int );
void swap(char *x, char *y);
int main()
{
char names[MAX_NAMES][MAX_COMBINED + 1];
//Call Functions
getNames(names, MAX_NAMES, MAX_SINGLE, MAX_COMBINED);
displayNames(names, MAX_NAMES);
bubbleSort(names, MAX_NAMES);
displayNames(names, MAX_NAMES);
}
/********************************************************************
*** FUNCTION displayInfo ***
*********************************************************************
*** DESCRIPTION: <Displays program information to user> ***
*** INPUT ARGS: <list of all input argument names> ***
*** OUTPUT ARGS: <list of all output argument names> ***
*** IN/OUT ARGS: <list of all input/output argument names> ***
*** RETURN: <return type and return value name> ***
********************************************************************/
void displayInfo() //displays program information
{
cout <<"*********************************************************" << endl;
cout << "This program will read names from a data file one name at a time " << endl
<<"then format and store them in a separate array. " << endl
<<"It will then print them as they were entered " << endl
<<"then sort them alphabetically and print again." << endl;
cout <<"*********************************************************" << endl;
}
void displayNames( char nameList[][MAX_COMBINED + 1],const int size)
{
for(int count = 0; count < size; count++) {
cout << nameList[count] << endl;
}
cout << endl;
}
void getNames(char nameList[][MAX_COMBINED + 1], const int maxSize, const int maxSingle, const int maxLength)
{
char firstName[maxSingle]; //in function
char middleName[maxSingle];//in function
char lastName[maxSingle];//in function
char fullName[maxLength + 1];
int index = 0; //in function
ifstream inputFile;
inputFile.open("names.txt");
while((index < maxSize) && (!inputFile.getline(firstName, maxSingle, '\n').eof()))
{
inputFile >> middleName;
inputFile >> lastName;
strcpy(fullName,lastName);
strcat(fullName, ", ");
strcat(fullName, firstName);
strcat(fullName, " ");
strcat(fullName, middleName);
strcpy(nameList[index], fullName);
index++;
}
inputFile.close();
// for(int i = 0; i < MAX_NAMES; i++)
// cout << names[i] << endl;
}
void bubbleSort(char nameList[][MAX_COMBINED+1], const int size)
{
for(int i = 0; i < size; i++)
// for(int j = 0; j < MAX_COMBINED +1; j++)
if(strcmp(nameList[i], nameList[i+1]) > 0)
swap(nameList[i], nameList[i+1]);
}
void swap(char *x, char *y)
{
char temp[MAX_COMBINED+1];
strcpy(temp, x);
strcpy(x, y);
strcpy(y, temp);
}
|