我的结构数组是打印垃圾

时间:2014-03-05 17:02:56

标签: c++ arrays struct

我正在编写一个程序,它使用文件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

2 个答案:

答案 0 :(得分:1)

就像许多人评论过的那样,你在代码中使用了两种截然不同的范例:你正在使用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 ++的一切,我可能是从here学到的。

答案 1 :(得分:0)

不确定,但部分问题可能是这些问题:

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-strings(以空字节终止的字符数组)。

此外,此代码段:

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

如果您要去Selection Sort Algorithm(就像我认为的那样)。