读取字符串C ++的字符时出错 - Visual Studio

时间:2017-04-26 04:30:02

标签: c++ visual-studio

执行此程序时,只要到达“records = read_records(in,num_records)”,我就会收到本地错误“读取字符串字符时出错”;

以下是错误的屏幕截图: error snippet

以下是代码片段:

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()

0 个答案:

没有答案