我正在寫一個程序,它使用文件I/O接收輸入文件並將數據存儲在文件中的結構數組中。請記住,我爲您提供了部分代碼。不是全部。我知道一切都正常,因爲我輸入數據後打印出所有結構數組。在我嘗試對它進行排序之前,正在打印的垃圾發生在函數中。我試圖對其進行排序的函數在我的讀取文件函數中調用,在該函數中打印了結構數組。在那裏,它打印完美。當它調用sort函數時,結構數組會輸出垃圾。我的結構數組打印出垃圾
這是我已經使用輸入文件的一個例子:
### building room_number capacity
SAL 210 30
OHE 100 120
OHE 120 50
### ID prefix course# sect# #minutes #mtgsperweek #students
20001 CSCI 101 01 110 2 40
20002 CSCI 101 02 110 2 60
20003 CSCI 101 03 110 2 100
20004 CSCI 103 01 90 2 50
20005 CSCI 103 02 90 2 50
20006 CSCI 103 03 90 2 75
20007 CSCI 104 01 80 2 50
20008 CSCI 104 02 80 2 50
20009 CSCI 109 01 90 1 25
20010 CSCI 109 02 90 1 25
20011 CSCI 109 03 90 1 25
20012 CSCI 109 04 90 1 25
### ID days_constraint start_constraint end_constraint
20001 MW 1000 1400
20002 MW 1000 1400
20003 TR 1000 1400
20004 TR 0800 1200
20005 TR 0800 1200
20006 TR 0800 1200
20007 MW 0800 1200
20008 MW 0800 1200
20009 M 0800 1200
20010 M 0800 1200
20011 T 0800 1200
20012 T 0800 1200
我在我讀取文件並輸入數據到結構的陣列的功能使用stringstream。我知道程序的一部分是正確的,因爲我之後打印出結構數組,並且它們都完美地打印出來。當我在我的read_File函數中調用一個新函數時,出現了這個問題,該函數稱爲sort_ByClassroomSize,其中我嘗試按教室容量以降序對結構數組進行排序。
下面是結構:
struct Room {
char building_code[4];
int room_number;
int max_students;
};
我聲明指針並根據房間用戶輸入的數目以後動態地分配。
struct Room* roomsPtr;
roomsPtr = new struct Room[room_size];
其中room_size是一個計數器,用於統計宣佈房間的行數。
下面是我輸入數據到結構:
if(!(line[0] == '-' && line[1] == '-')) {
stringstream ss;
ss << line;
ss >> roomsPtr[i].building_code;
ss >> roomsPtr[i].room_number;
ss >> roomsPtr[i].max_students;
if(ss.fail()) { //checks format; terminates program if incorrect.
cout << "The file has been formatted incorrectly." << endl;
return;
}
//prints lines in section 1.
cout << roomsPtr[i].building_code << " " << roomsPtr[i].room_number <<
" " << roomsPtr[i].max_students << endl;
問題是在排序功能腳麻:
void order_ClassroomsBySize(int num_rooms) { //arranges classrooms in descending order
by classroom size
struct Room temp_value;
//prints the array of structs before arrangement
cout << endl;
cout << "Arranged classrooms in descending order by classroom size: " << endl;
for(int i = 0; i < num_rooms; i++) {
cout << roomsPtr[i].building_code << " " << roomsPtr[i].room_number << " "
<< roomsPtr[i].max_students;
cout << endl;
}
}
//arranges the array of structs in descending order by classroom size
for(int i = 0; i < num_rooms; i++) {
for(int j = 0; j < num_rooms - 1; j++) {
if(roomsPtr[j].max_students < roomsPtr[j+1].max_students) {
temp_value = roomsPtr[j];
roomsPtr[j] = roomsPtr[j+1];
roomsPtr[j+1] = temp_value;
}
}
}
//prints the array of structs after arrangement
cout << endl;
cout << "Arranged classrooms in descending order by classroom size: " << endl;
for(int i = 0; i < num_rooms; i++) {
cout << roomsPtr[i].building_code << " " << roomsPtr[i].room_number << " "
<< roomsPtr[i].max_students;
cout << endl;
}
}
我的程序打印出如下:
### building room_number capacity
SAL 210 30
OHE 100 120
OHE 120 50
### ID prefix course# sect# #minutes #mtgsperweek #students
20001 CSCI 101 01 110 2 40
20002 CSCI 101 02 110 2 60
20003 CSCI 101 03 110 2 100
20004 CSCI 103 01 90 2 50
20005 CSCI 103 02 90 2 50
20006 CSCI 103 03 90 2 75
20007 CSCI 104 01 80 2 50
20008 CSCI 104 02 80 2 50
20009 CSCI 109 01 90 1 25
20010 CSCI 109 02 90 1 25
20011 CSCI 109 03 90 1 25
20012 CSCI 109 04 90 1 25
### ID days_constraint start_contsraint end_constraint
20001 MW 1000 1400
20002 MW 1000 1400
20003 TR 1000 1400
20004 TR 0800 1200
20005 TR 0800 1200
20006 TR 0800 1200
20007 MW 0800 1200
20008 MW 0800 1200
20009 M 0800 1200
20010 M 0800 1200
20011 T 0800 1200
20012 T 0800 1200
Arranged classrooms in descending order by classroom size:
course# sect# #minutes OHE 593851250 1667592992
t# #minutes OHE 1970170221 544433524
OHE 120 50
Arranged classrooms in descending order by classroom size:
course# sect# #minutes OHE 593851250 1667592992
t# #minutes OHE 1970170221 544433524
OHE 120 50
爲什麼不使用['std :: vector'](http://en.cppreference.com/w/cpp/container/vector)並使用['std :: sort'](http:// en。 cppreference.com/w/cpp/algorithm/sort)進行排序?當然[字符串'std :: string'](http://en.cppreference.com/w/cpp/string/basic_string)。使用[標準庫](http://en.cppreference.com/w/cpp)中的工具作爲C++程序員將從長遠角度幫助您。 –
@JoachimPileborg我是新來的C++。我還沒有學過矢量!這是我寫的第三個程序。我還沒有上過課。我剛剛完成了學習結構和指針。 –
我不想讓你失望,但你可能會在幾年內學習結構和指針。另外,結構是所有成員公開的類(可能沒有顯式成員函數)。 –