Difficulties in Reading Text File and Organizing Output

Hi everyone! I'm fairly new to programming, and I'm having difficulties on an assignment that was given by my professor.

I'm trying to have the program read a text file, and produce an output sorted alphabetically. I would also like to place the texts in proper columns to be organized, although I'm having difficulties with both of these tasks. Even when using setw I haven't been able to fix the offset of the output. The professor has not put much content to assist us in the program or the concepts involving it, making this even more difficult. All help would be welcomed and greatly appreciated.

The sample output the teacher posted was this. I'd rather have it organized columns, and figure out more creative ways to make it look neater.

LastName FirstName DaysofRental BalanceDue
Anderson Paul 30 20.00
Doe John 10 95.20
Foster Sam 30 15.00
O'Donell Miriam 10 24.30
Smith Joe 15 100.50
Total Balance Due for file: 255.00
Customer with the highest number of rental days are : Paul Anderson, Sam Foster
Customer with the highest balance due are: Joe Smith


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
  #include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <stdlib.h>

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

 ofstream	outfile;

                                                        // input section
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 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 ++;
    }
    
	
 	system ("PAUSE");
                                                        // process section

// remove this comment ans place your process statements here.

                           								// 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);
  
  outfile << "LastName" << setw (5) << " " << "FirstName" << setw(5) << " " << "DaysofRental" << setw(5) << " " << "BalanceDue" << endl; 
  
      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 <<  lastname[listcount] << ",      " << firstname[listcount];
          cout << "      " << daysofrent[listcount] << "      " << balancedue[listcount] << endl;
          listcount ++;
    }
    


  
  
  outfile.close();
  cout << endl << endl;    // provides blank line before pause display message
  system ("pause");
  return 0;
  }
Last edited on
He didn't ask you to make it pretty, don't spend your time on homework on prettyness until after you get the work done as assigned.

Read this

http://www.cplusplus.com/articles/NhA0RXSz/
Topic archived. No new replies allowed.