Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: File input and output, sorting issues

  1. Dec 1, 2011 #1
    1. The problem statement, all variables and given/known data

    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.

    2. Relevant equations

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

    3. The attempt at a solution
    Code (Text):
    #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: Dec 1, 2011
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted