File input and output, sorting issues

In summary, file input and output is the process of reading data from or writing data to a file on a computer. File sorting involves arranging data in a specific order based on a chosen criteria. Common issues with file input and output include incorrect file formats, missing or corrupted data, and problems with file permissions. To prevent errors when sorting files, it is important to carefully check the data for accuracy and consistency before beginning the sorting process. The amount of data that can be input or output from a file depends on the capabilities of the computer and file system being used, but there may be practical limitations based on available memory and storage space.
  • #1
muzak
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:
Physics news on Phys.org
  • #2


From reviewing your code and the assignment link provided, it seems that you are trying to create a program that reads in account information from a text file and sorts it based on the user's input (ID or last name). However, it appears that the program is crashing after reading in the input and before sorting and outputting the data.

There are a few potential issues that could be causing this crash. One possibility is that your program is trying to access data that is out of bounds for the array. For example, in your sort function, you have a for loop that starts at index 1 instead of 0. This could lead to accessing memory that is not allocated for your array, causing a crash. Additionally, in your sort function, the loop condition is using the totalRecords variable, which is the number of records read in from the file. However, since arrays are indexed starting at 0, the last index of the array would be totalRecords-1. So, this loop condition could also be causing a crash.

Another potential issue could be with your use of pointers and dynamically allocating memory for your accountInfo structures. It is important to make sure that you are properly allocating and deallocating memory to avoid any memory leaks or crashes. It may be helpful to use a debugger to step through your code and see where the crash is occurring.

I would also recommend breaking down your code into smaller, manageable functions and testing each one individually before trying to integrate them all together. This can help with debugging and identifying any specific issues that may be causing the crash.

I hope this helps and good luck with your assignment!
 

Related to File input and output, sorting issues

1. What is file input and output?

File input and output is the process of reading data from or writing data to a file on a computer. This allows for the storage and retrieval of data for various purposes, such as data analysis or record keeping.

2. How does file sorting work?

File sorting is the process of arranging data in a specific order based on a chosen criteria. This can be done in ascending or descending order and is often used to organize and make large amounts of data more manageable.

3. What are some common issues with file input and output?

Some common issues with file input and output include incorrect file formats, missing or corrupted data, and problems with file permissions. These issues can lead to errors in data processing and may require troubleshooting to resolve.

4. How can I prevent errors when sorting files?

To prevent errors when sorting files, it is important to carefully check the data for accuracy and consistency before beginning the sorting process. It may also be helpful to use a sorting algorithm that is appropriate for the type of data being sorted.

5. Is there a limit to the amount of data that can be input or output from a file?

The limit to the amount of data that can be input or output from a file depends on the capabilities of the computer and the file system being used. However, there may be practical limitations based on available memory and storage space.

Similar threads

  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
30
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
906
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
Back
Top