C++ C++;分配-动态分配数组并从文件中读取记录-挂起
我的讲师提供的示例.cpp在我输入文件名后导致控制台挂起,因为它到达了我认为是C++ C++;分配-动态分配数组并从文件中读取记录-挂起,c++,C++,我的讲师提供的示例.cpp在我输入文件名后导致控制台挂起,因为它到达了我认为是SalesRecord\u数组read\u records(ifstream&in,int&n) 他那一类的内容 #ifndef SORT_H #define SORT_H //A generic sorting function. template<class T> void sort(T a[], int number_used); //Precondition: number_used <=
SalesRecord\u数组read\u records(ifstream&in,int&n)
他那一类的内容
#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
\ifndef排序
#定义排序
//通用排序函数。
模板
无效排序(T a[],使用整数);
//先决条件:我的道歉。问题似乎与提供的txt文件有关。在我的挫折中,我无意中添加了一个空白的最后一行,这也解决了这个问题
我猜for()挂在上,而(in.get()!='\n')
您的缩进样式非常奇特。有什么特别的原因吗?还有,请举个简单的例子。您必须在发出请求之前进行调试。u s es t d::v e c t o r
p l e a s e。看到typedef-SalesRecord*SalesRecord\u数组是很痛苦的代码>。使用vector,就少了一个需要担心的问题。
3
4541
Hitt, Teresa
500
6666
Addams, Wednesday
1000
2121
Jones, Mary
389
#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