C ++赋值 - 动态分配数组并从文件中读取记录 - 挂起

时间:2015-04-20 23:02:18

标签: c++

示例.cpp我的讲师提供了导致控制台在我输入文件名后挂起,因为它达到我认为是SalesRecord_Array read_records(ifstream& in, int &n)中的for()的最后一个循环

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include "sort.h"
using namespace std;

const int MAX_FILE_NAME     =  35;
const int MAX_ID_LEN        =  10;
const int MAX_NAME_LEN      =  20;
const int RATE_CHANGE_POINT = 500;
const double RATE1 = 0.50;
const double RATE2 = 0.60;

class SalesRecord
{  private:
      char   emp_id[MAX_ID_LEN + 1];
      char   emp_name[MAX_NAME_LEN +1];
      int    quantity_sold;
      double commission_rate;
      double commission;
   public:

      SalesRecord();
      void read(ifstream& in);
      void calc_commission();
      void write(ostream & os) const;
      bool operator<(const SalesRecord& right) const;
};

typedef SalesRecord * SalesRecord_Array;


void open_input(ifstream& input, char name[]);
void open_output(ofstream& output, char name[]);

SalesRecord_Array read_records(ifstream& in, int &n);
void calc_records(SalesRecord_Array records, int n);
void write_records(SalesRecord_Array records, int n);

int  main()
{  char again;
   int  num_records;
   char infilename[MAX_FILE_NAME + 1];
   ifstream  in;
   SalesRecord_Array records = NULL;

   do
   {  open_input(in, infilename);
      records = read_records(in, num_records);
      in.close();
      if (records != NULL && num_records > 0)
      {  calc_records(records, num_records);
         sort(records, num_records);
         write_records(records, num_records);
         delete [] 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');
   } while ( again == 'y' || again == 'Y'); 

   cout <<  "\n\n***** END OF PROGRAM ******\n";
   return 0;
}

void open_input(ifstream& input, char name[])
{  int count = 0;
   do
   {  count++;
      if (count != 1)
      {  cout << "\n\aInvalid file name or file does not exist. Please try again." << endl;
      }

      cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME << " characters please)\n:> ";
      cin.get(name, MAX_FILE_NAME + 1);
      cin.ignore(256, '\n');
      input.clear();
      input.open(name, ios_base::in);
   } while (input.fail() );
}

void open_output(ofstream& output, char name[])
{  int count = 0;
   do
   {  count++;
      if (count != 1)
      {  cout << "\n\aInvalid file name. Please try again." << endl;
      }

       cout << "\nEnter the output file name (maximum of " << MAX_FILE_NAME << " characters please)\n:> ";
      cin.get(name, MAX_FILE_NAME + 1);
      cin.ignore(256, '\n');
      output.clear();
      output.open(name);
   } while (output.fail() );
}

SalesRecord::SalesRecord()
{  emp_id[0] = emp_name[0] ='\0';
   quantity_sold = 0;
   commission_rate = commission = 0.0; 
}

void SalesRecord::read(ifstream& in)
{  in.get(emp_id, MAX_ID_LEN +1);
   while (in.get() != '\n');

   in.get(emp_name, MAX_NAME_LEN +1);
   while (in.get() != '\n');

   in >> quantity_sold;
   while (in.get() != '\n');
}

void SalesRecord::calc_commission()
{  if (quantity_sold < RATE_CHANGE_POINT)
      commission_rate = RATE1;
   else
      commission_rate = RATE2;

   commission = commission_rate * quantity_sold;
}

void SalesRecord::write(ostream & os) const
{  os.setf(ios::fixed);   os.setf(ios::showpoint);   os.precision(2);

   os << emp_id << "\n" << emp_name << "\n"
       << quantity_sold << " " << commission_rate << " "
       << commission << endl;
}

bool SalesRecord::operator<(const SalesRecord& right) const
{  if (stricmp(emp_name, right.emp_name) < 0)
        return true;
   else 
        return false;
}


SalesRecord_Array read_records(ifstream& in, int &n)
{
   in >> n;
   if (n < 1 || in.fail()) 
      return NULL;

   while (in.get() != '\n');
   SalesRecord_Array records;

   records = new SalesRecord[n];
   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);
   }

   return records;
}

void calc_records(SalesRecord_Array records, int n)
{  int i;
   for(i = 0; i < n; i++) 
   {  records[i].calc_commission();
   }
}

void write_records(SalesRecord_Array records, int n)
{  char      outfilename[MAX_FILE_NAME + 1];
   ofstream  out;

   cout << "\n\nI need the output file name." << endl;
   open_output(out, outfilename);

   if (out.good())
   {  out << n << endl;
      int i;
      for(i = 0; i < n; i++) 
      {  records[i].write(out);
      }
      out.close();
   }
   else
   {  cout << "\a\nUnable to save data!" << endl;
   }
}

他提供的txt文件的内容

3
4541
Hitt, Teresa
500
6666
Addams, Wednesday
1000
2121
Jones, Mary
389

他的sort.h的内容

#ifndef SORT_H
#define SORT_H
//A generic sorting function.

template<class T>
void sort(T a[], int number_used);
//Precondition: number_used <= declared size of the array a.
//The array elements a[0] through a[number_used - 1] have values.
//Postcondition: The values of a[0] through a[number_used - 1] have
//been rearranged so that a[0] <= a[1] <= ... <= a[number_used - 1].

template<class T>
void swap_values(T& variable1, T& variable2);
//Interchanges the values of variable1 and variable2.

template<class T>
int index_of_smallest(const T a[], int start_index, int number_used);
//Precondition: 0 <= start_index < number_used. Array elements have values.
//Returns the index i such that a[i] is the smallest of the values
//a[start_index], a[star_index + 1], ..., a[number_used - 1].

template<class T>
void sort(T a[], int number_used)
{
    int index_of_next_smallest;
    for (int index = 0; index < number_used - 1; index++)
    {//Place the correct value in a[index]:
        index_of_next_smallest = 
                 index_of_smallest(a, index, number_used);
        swap_values(a[index], a[index_of_next_smallest]);
        //a[0] <= a[1] <=...<= a[index] are the smallest of the original array
        //elements. The rest of the elements are in the remaining positions.
    }
}

template<class T>
void swap_values(T& variable1, T& variable2)
{
    T temp;

    temp = variable1;
    variable1 = variable2;
    variable2 = temp;
}

template<class T>
int index_of_smallest(const T a[], int start_index, int number_used)
{
    T min = a[start_index];
    int index_of_min = start_index;

    for (int index = start_index + 1; index < number_used; index++)
        if (a[index] < min)
        {
            min = a[index];
            index_of_min = index;
            //min is the smallest of a[start_index] through a[index]
        }

    return index_of_min;
}
#endif

它在教学视频中按预期工作,但在我尝试过的两个系统上没有。

1 个答案:

答案 0 :(得分:0)

道歉。问题出现在提供的txt文件中。在我的沮丧中,我不小心添加了一个空白的最后一行,这也解决了这个问题。

我猜测for()挂在while (in.get() != '\n')