Sort Char Array Alphabetically

I want to sort the records by alphabetical order in the index to make the output look like this:

Record Anderson, Paul 30 20.00
Record Barbieri, Jesse 20 100.00
Record Doe, John 10 95.20
Record Foster, Sam 30 15.00
Record Gordon, Jeff 10 10.00
Record Hancock, John 15 150.00
Record Smith, Joe 15 100.50
Record Johnson, Jimmie 45 50.50
Record O'Donell, Miriam 10 24.30
Record Turner, Timmy 20 100.00

Can anyone steer me in the right direction here? Here's my code.

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <vector>

using namespace std;

const int MAX_SIZE = 50;
const int NAME_SIZE = 30;
const int COLUMNROW = 4;
typedef char STRING_30[NAME_SIZE];
typedef STRING_30 LIST_S30[MAX_SIZE];
typedef STRING_30 LIST_COL[COLUMNROW];
typedef int LIST_I[MAX_SIZE];
typedef float LIST_F[MAX_SIZE];

//******************************* main ********************************

int main(int argc, char *argv[])
{
 int		choice;

 
 char		drive[2],
			disk_file[15],
			file[9];
 
float       sum = 0, 
 			tot_balance = 0,
			indx;

ofstream	outfile;

system ("cls"); // clear the screen 

    ifstream inf;
    inf.open("input.txt");
    LIST_S30 lastname, firstname;
    LIST_I daysofrent;
    LIST_F balancedue;
    LIST_COL headers;
    int highest = 0; 
    int highbal = 0; 
    int listcount = 0;
   
    while ( listcount <= 10)
    {
          if (listcount == 0)
          {
             inf >> headers[listcount] >> headers[listcount] >> headers[listcount] >> headers[listcount];
          }
          else
          {
              inf >> lastname[listcount-1] >> firstname[listcount-1] >> daysofrent[listcount-1] >> balancedue[listcount-1];
          }
          listcount ++;
    
    }
    
	listcount=0;
    
	while ( listcount < 10)
    {
          cout << "Record: " <<  lastname[listcount] << ", " << firstname[listcount];
          cout << " " << daysofrent[listcount] << " " << balancedue[listcount] << endl;
          listcount ++;
    }
    
    for (int i=0; i < 10; i++ ){
    	tot_balance += balancedue [i]; 
    }
    
    for (int i = 1; i < 10; i++) {
    	if (daysofrent [highest] < daysofrent [i]) {
    		highest = i;
    	}
    }
    
    for (int i = 0; i < 10; i++) {
    	if (balancedue [highbal] < balancedue [i]) {
    		highbal = i;
    	}
    }

	system ("PAUSE");

                           								// output section
  system ("cls");
  cout << "Output to console (1) or disk file (2):   ";
  cin >> choice;
  if ( choice == 1 )
	  {
		system ("cls");
		outfile.open("con");
	  }
  else				  //routine allows interactive entry of external file name
	  {
		cout << "Which drive:  a, b, c, d, e, or f ?   ";
		cin >> drive;
		strcpy(disk_file, drive);
		strcat(disk_file, ":");
		cout << "Enter a results file name:   ";
		cin >> file;
		strcat(disk_file, file);
		strcat(disk_file, ".dta");
		outfile.open(disk_file);
		}
  outfile << setiosflags(ios::showpoint | ios::fixed) << setprecision(2);
    
    listcount=0;
    
    while ( listcount < 10)
    {
          outfile << "Record: " <<  lastname[listcount] << ", " << firstname[listcount];
          outfile << " " << daysofrent[listcount] << " " << balancedue[listcount] << endl;
          listcount ++;
    }
    
    outfile << endl; 
    
    outfile << left << "Total Balance Due for File: $ " << tot_balance << endl; 
    
        for (int i = 0; i < 10; i++) {
    		if (daysofrent [highest] == daysofrent [i]){
    		outfile << "Customer with the Highest Number of Rental Days: " << lastname [i] << ", " << firstname [i] << endl;
    		}
   	 	}
    
    	for (int i=0; i < 10; i++) {
    		if (balancedue [highbal] == balancedue [i]) {
    			outfile << "Customer With the Highest Balance Due: " << lastname [i] << ", " << firstname [i] << endl;
    		}
    	}
    	
  outfile.close();
  cout << endl << endl;    // provides blank line before pause display message
  system ("pause");
  return 0;
  }
Last edited on
Topic archived. No new replies allowed.