• Support PF! Buy your school textbooks, materials and every day products Here!

File input and output, sorting issues

  • Thread starter muzak
  • Start date
  • #1
44
0

Homework Statement



I think it runs up to the couts I have in my input function but then it just crashes. I'm running it on DevC++. The code is in 3 along with the assignment link. It's assignment 11 that I'm working on and the text file is next to the link for the assignment. I would appreciate any input about this, can't wrap my head around it.
Thanks.

Homework Equations



http://www2.cs.uh.edu/~acl/cs1410/assignment.html

The Attempt at a Solution


Code:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;

struct accountInfo{
       int id;
       string lastName, firstName;
       double balance;
};
class accountList {
      public:
              void input(int a, char* b[]);
              void sort(int argc, char* b[]);
              void output(int a, char* b[]);
              
      private://want an array of pointers here instead of structures array
              accountInfo *records[ ];
              int totalRecords;
}accounts;

int main (int argc, char* argv[]){
    accounts.input(argc, argv);
    accounts.sort(argc, argv);
    accounts.output(argc, argv);
    system("PAUSE");
    return 0;
}
void accountList::input(int a, char* b[]){
     int i=0;
     totalRecords = 0;
     string last, first;
     ifstream fin;
     fin.open(b[1]);
     if (fin.fail()){
        cout<<"File did not open.\n";
        exit(1);
     }
     
     while (!fin.eof()) {
           accounts.records[i] = new accountInfo;
           fin>>accounts.records[i]->id;
           fin.get();
           getline(fin, last, ' ');
           accounts.records[i]->lastName = last;
           getline(fin, first, ' ');
           accounts.records[i]->firstName = first;
           fin>>accounts.records[i]->balance;
           cout<<accounts.records[i]->id<<" "//testing input
               <<accounts.records[i]->lastName<<" "
               <<accounts.records[i]->firstName<<" "
               <<accounts.records[i]->balance<<endl;
           i++;
     }
     totalRecords = i;
     fin.close();
}
void accountList::sort(int argc, char* b[]){
     int i, j, tempId, less; //less is indexOfLowest by id
     string tempLast, tempFirst;
     double tempBalance;
     if ( strcmp(b[3], "ID") == 0){
        for (i = 0; i < totalRecords; i++){
            less = i;
            for (j = 1; j<totalRecords; j++){
                if (accounts.records[j]->id > accounts.records[less]->id){
                   less = j;
                   tempId = accounts.records[i]->id;
                   tempBalance = accounts.records[i]->balance;
                   tempLast = accounts.records[i]->lastName;
                   tempFirst = accounts.records[i]->firstName;
                   accounts.records[i]->id = accounts.records[less]->id;
                   accounts.records[i]->balance = accounts.records[less]->balance;
                   accounts.records[i]->lastName = accounts.records[less]->lastName;
                   accounts.records[i]->firstName = accounts.records[less]->firstName;
                   accounts.records[less]->id = tempId;
                   accounts.records[less]->balance = tempBalance;
                   accounts.records[less]->lastName = tempLast;
                   accounts.records[less]->firstName = tempFirst;
                }
            } 
        }
     }
     else if (strcmp(b[3], "LastName") == 0){
          for (i=0; i < totalRecords; i++){
            less = i;
            for (j=1; j<totalRecords; j++){
                if (accounts.records[j]->lastName >
                    accounts.records[less]->lastName){
                   less = j;
                   tempId = accounts.records[i]->id;
                   tempBalance = accounts.records[i]->balance;
                   tempLast = accounts.records[i]->lastName;
                   tempFirst = accounts.records[i]->firstName;
                   accounts.records[i]->id = accounts.records[less]->id;
                   accounts.records[i]->balance = accounts.records[less]->balance;
                   accounts.records[i]->lastName = accounts.records[less]->lastName;
                   accounts.records[i]->firstName = accounts.records[less]->firstName;
                   accounts.records[less]->id = tempId;
                   accounts.records[less]->balance = tempBalance;
                   accounts.records[less]->lastName = tempLast;
                   accounts.records[less]->firstName = tempFirst;
                }
            }
        }
     }
     cout<<accounts.records[totalRecords]->id<<endl;
}
void accountList::output(int a, char* b[]){
     int i;
     ofstream fout;
     fout.open(b[2]);
     if (fout.fail()){
        cout<<"Output file did not open. \n";
        exit(1);
     }
     fout.setf(ios::fixed | ios::showpoint);
     fout.precision(2);
     cout.setf(ios::fixed | ios::showpoint);
     cout.precision(2);
     for (i = 0; i < totalRecords; i++){
         fout<<accounts.records[i]->id<<" "
             <<accounts.records[i]->lastName<<" "
             <<accounts.records[i]->firstName<<" "
             <<accounts.records[i]->balance<<endl;
         cout<<accounts.records[i]->id<<" "
             <<accounts.records[i]->lastName<<" "
             <<accounts.records[i]->firstName<<" "
             <<accounts.records[i]->balance<<endl;
     }
}
 
Last edited:

Answers and Replies

Related Threads on File input and output, sorting issues

Replies
31
Views
10K
Replies
6
Views
1K
  • Last Post
Replies
3
Views
2K
Replies
1
Views
7K
Replies
4
Views
998
  • Last Post
Replies
15
Views
2K
Replies
6
Views
2K
  • Last Post
Replies
14
Views
12K
  • Last Post
Replies
0
Views
1K
Replies
2
Views
2K
Top