簡體   English   中英

我的結構數組正在打印垃圾

[英]My array of structs is printing out garbage

我正在編寫一個程序,該程序使用文件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是一個計數器,用於計算聲明一個房間的行數。

這是我將數據輸入到struct中的方法:

   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

就像許多人評論過的那樣,您在代碼中使用了兩種截然不同的范例:您就像在使用C一樣在使用C ++。如果您確實在使用C ++,則有更簡單的方法可以做到這一點。

習慣上講,如果我使用的是C ++,我將只使用運算符重載。

像這樣:

class Room {
    // not generally a good idea to have public members, but if you must...
    public:
        std::string building_code;
        int room_number;
        int max_students;
        bool operator == (const Room & other) {
            if (max_students != other.max_students)
            {
                return false;
            }
            else return room_number == other.room_number && building_code == other.building_code;
        }
        bool operator < (const Room & other) {
            // returning the oposite to ensure descending order.
            return max_students > other.max_students;
        }

 };

然后,您可以將Room對象放在std::set ,這既可以為所需的所有房間動態分配足夠的內存, 可以將它們按降序排列,如下所示:

#include <set>

std::set<Room> rooms;
room.insert( /* populate a room and put it here */);
room.insert( /* populate a room and put it here */);

這樣您就可以像這樣輸出它們:

cout << "Arranged classrooms in descending order by classroom size: " << endl;
for (std::set<Room>::iterator i = rooms.begin(); i != rooms.end(); i++)
{
    cout << i->building_code << " " << i->room_number << " " 
        << i->max_students;
       cout << endl;

}

我所學到的有關C ++的所有知識,我可能都從這里學到

不確定,但是部分問題可能是這些行:

cout << "Arranged classrooms in descending order by classroom size: " << endl;
for (std::set<Room>::iterator i = rooms.begin(); i != rooms.end(); i++)
{
    cout << i->building_code << " " << i->room_number << " " 
        << i->max_students;
       cout << endl;

}

尤其是:

    cout << i->building_code << " " << i->room_number << " " 
        << i->max_students;

通過編寫cout << i->building_code ,您告訴cout打印出該char數組中的任何內容,即i->building_code 但是cout不知道數組有多大,因此它將不斷打印看到的內容,直到到達空字符( char a = 0 )。 這會導致cout超出打印內容中字符數組的邊界。

解決此問題的方法可能是使i->building_code為5字節數組而不是4字節數組,並在填充結構之前將數組清零:

for (int j = 0; j < num_rooms; j++)
    for (int i = 0; i < 5; i++)
        roomPtr[j]->building_code[i] = 0;

有關更多信息,請參見本頁有關C字符串 (以空字節結尾的字符數組)的信息。

另外,此代碼段:

    //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;
            }
       }
    } 

應該看起來像這樣:

   for(int i = 0; i < num_rooms - 1; i++) {
        for(int j = i + 1; j < num_rooms; j++) {
            if(roomsPtr[i].max_students < roomsPtr[j].max_students) {
              temp_value = roomsPtr[i];
              roomsPtr[i] = roomsPtr[j];
              roomsPtr[j] = temp_value;
            }
       }
    } 

如果您要使用選擇排序算法 (就像我想的那樣)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM