0

When executing this program I receive an error in local saying "Error reading characters of string" whenever it gets to "records = read_records(in, num_records);"

Here's a screen shot of the error: error snippet

And here's the snippet of code:

class SalesRecord                      // Record definition for items's sales record
{
private:
    char   item_id[MAX_ID_LEN + 1];     // Items's id
    char   item_name[MAX_NAME_LEN + 1]; // Items's name
    int    quantity_sold;               // Number of items
    double regular_price;               // Regular price of items
    double discount_price;              // discount price of items
    double total_price;                 // Total price of items
public:
    // Functions that operate on an individual record
    void read(ifstream& in);                       // Read record from input stream
    void calc_discount_price();                    // Calculate discount price
    void write(ostream & os) const;                // Write record to output stream
    int operator<(const SalesRecord& right) const; // Overloaded for use in sort
};

int  main()
// Parameters: None
// Returns:    Zero
// Calls:      open_input(), read_records(), calc_discount), sort(), write_records().
{
    char again;                                  // Does user want to go through loop again?
    int  num_records;                            // # of records 
    char infilename[MAX_FILE_NAME + 1];          // Name of file for records to be processed
    ifstream  in;                                // Input file stream
    SalesRecord_Array records = NULL;            // Pointer to array of sales records

    do
    {
        open_input(in, infilename);               // Get file name & open file
        records = read_records(in, num_records);  // Read records & get number of records
        in.close();                               // Close file
        if (num_records > 0)
        {
            calc_discounts(records, num_records);    // Calculate discount values for each record
            sort(records, num_records);            // Sort records by item name
            write_records(records, num_records);   // Save records to new file
            delete[] records;                     // Free array of records  
        }
        else
        {
            cout << "\n\n\aNo data in file: " << infilename << endl;
        }

        cout << "\nDo you want to process another file (Y/N)? ";
        cin >> again;
        cin.ignore(256, '\n');  // Remove Enter key from keyboard buffer
    } while (again == 'y' || again == 'Y');

    cout << "\n\n***** END OF PROGRAM ******\n";// Print closing message.    
    return 0;
}  // End of main()  

SalesRecord_Array read_records(ifstream& in, int &n)      // Allocate space for records & read them
// Parameters: Input stream, variable for number of records.      
// Returns   : Pointer to array of records allocated.                                                        
// Calls     : SalesRecord::read().                                                        
{
    in >> n;                       // Read number of records
    if (n < 1 || in.fail())
        return NULL;               // No records to read return

    while (in.get() != '\n');     // Clear enter after number of records in file
    SalesRecord_Array records;    // Pointer for array of records

    records = new SalesRecord[n]; // allocate space for records 
    if (records == NULL)
    {
        cout << "\aCan not allocate memory!";
        exit(1);
    }

    int i;
    for (i = 0; in.good() && !in.eof() && i < n; i++)
    {
        records[i].read(in);             // Read each record
    }

    return records;  // Return pointer to array 
} // End of read_records()
Arunas
  • 1,162
  • 1
  • 9
  • 16
Tyler Brady
  • 119
  • 3
  • You have this line `SalesRecord_Array records = NULL; ` But nowhere do you define `SalesRecord_Array`. What is it? – Arunas Apr 26 '17 at 06:07
  • Note that after calling `new`, either you get the memory, or an exception will be thrown and you will never hit the check `if (records == NULL)` - but your program will exit. – Arunas Apr 26 '17 at 06:11
  • Does `class SalesRecord` have a constructor that initializes the members? Note that until you've read something, currently any SalesRecord you have contains largely uninitialized garbage. – Arunas Apr 26 '17 at 06:14
  • I'd suggest that you also consider telling us exactly which line you've gotten to when you see the variable dump you're showing. At the line you've indicated, nothing has happened, and I'd suggest that you're looking at completely uninitialized memory. The fact that a string cannot be read is just slightly less alarming than `n= -858993460' or `i` having the same value. – Arunas Apr 26 '17 at 06:21
  • Please read [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – molbdnilo Apr 26 '17 at 07:46

0 Answers0