在C ++中更改向量对象中的第二个值

时间:2014-12-13 04:24:15

标签: c++ class pointers object vector

我是C ++的新手,我正在学习矢量和所有有趣的东西。我试图找出如何让用户在他选择的楼层添加房间。我已经完成了大部分问题,但只是那部分我很难搞清楚,任何帮助都将不胜感激!我认为主要问题是在ReplaceFloor函数中,但我不知道如何以用户选择Choice 4的方式进行,然后进入他想要添加房间的楼层,并让楼层矢量存储一个更多的空间。提前谢谢你的帮助!我尽力让它尽可能地理解 - 抱歉,我很新。

// Main.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Floor.h" // must include Floor header file
#include "Room.h" // must include Room header file
#include "AccessPoint.h" // must include AccessPoint header file
#include <vector>
#include <string>

using namespace std;

// functions that fills up all Floors
void fillFloor(vector<Floor>&);
void deletesFloor(vector<Floor>&);
void replaceFloor(vector<Floor>&);
// functions that fills up all Rooms
void fillRoom(vector<Room>&);
void deletesRoom(vector<Room>&);
// functions that fills up all Access Points
void fillAccessPoint(vector<AccessPoint>&);

void printFloorVector(const vector<Floor>&); // prints the information of all floors
void printRoomVector(const vector<Room>&); // prints the information of all Rooms
void printAccessPointVector(const vector<AccessPoint>&); // prints the information of all Access Points

// Variable Declaration
int roomNumber;
int accessPointNumber;
int floorID =0;
int roomID = 0;
int accessPointID =0;
int floorTotal;

// Check Floor Number To Add Room To
int floorForRoomAdd =0;

int main() {
    // Declaring Variables
    int exitCheck = 0;   // Used to loop if the user wants to check for more than date range
    int choice;

    // Creating Vector for Class Floor
    vector<Floor> Building;
    vector<Room> Floor;
    vector<AccessPoint> Room;

    // Calling the functions
    cout << "How Many Floors Does This Building Contain? ";

    cin >> floorTotal;
    cout << endl;

    for (int i = 0; i < floorTotal; i++) { // Gathers total floors number + How many rooms for each floor
    fillFloor(Building);
        cout << "How Many Rooms Are On This Floor? : " ;
        cin >> roomNumber;
        cout << endl;

        for (int b = 0; b < roomNumber; b++) { // Gathers total Room number + How many access points for each room
        fillRoom(Floor);    
            cout << "How Many Access Points Are In This Room? : ";  
            cin >> accessPointNumber;
            cout << endl;

            for (int c = 0; c < accessPointNumber; c++) { // Gather total access points + Status of each access point
            fillAccessPoint(Room);
            }
        }
    }

    cout << "##############################################################" << endl;
    cout << "#                       Done Building                        #" << endl;   
    cout << "##############################################################" << endl << endl;


    while (exitCheck <1) {

        // TITLE
        cout<<"\nNETWORK MONITORING ACCESS POINTS \n";
        cout<<"----------------------------------"; 
        cout<<"\n1 - Display Building Information"; // Check (Some minor issue)
        cout<<"\n2 - Add a Floor"; // Check
        cout<<"\n3 - Delete a Floor"; // Check
        cout<<"\n4 - Add a Room"; 
        cout<<"\n5 - Delete a Room"; 
        cout<<"\n6 - Add a Network Access Point";
        cout<<"\n7 - Delete a Network Access Point";
        cout<<"\n8 - Toggle Access Point Status";
        cout<<"\n9 - Change Access Point Date";
        cout<<"\n0 - Exit"; // Check 
        cout<<"\n";

        // Input 
        cout<<"\nChoice: ";
        cin>> choice;

        // Determine Choice
        if (choice == 1) {
        cout << "##############################################################" << endl;
        cout << "#                 The status of the building is              #" << endl;
        cout << "##############################################################" << endl << endl;
        printFloorVector(Building); // Prints end result
        cout << endl;
        printRoomVector(Floor); // Prints end result
        cout << endl;
        printAccessPointVector(Room); // Prints end result
        cout << endl;
        }
        else if (choice == 2) { // Adds a floor

            fillFloor(Building);
            cout << "Floor Has Been Created!" ;
            //cin >> roomNumber;
            //cout << endl;

        }
        else if (choice == 3) { // Deletes a floor
            deletesFloor(Building);
        }
        else if (choice == 4) { // Adds a room
            cout << "What Floor Would You Like To Add A Room To?";
            cin >> floorForRoomAdd; 
            cout << endl;
            replaceFloor(Building); //code where floor has the room info
            cout << "How Many Rooms Are On This Floor? : " ;
            cin >> roomNumber;  
            cout << endl;
        }
        else if (choice == 5) { // Deletes a room
            cout << "What Floor Is The Room That You Would Like To Have Deleted?";
            //code where floor has the room info
            deletesRoom(Floor);
        }
        else if (choice == 6) { // Adds an access point
            cout << "How Many Access Points Are In This Room? : ";  
            cin >> accessPointNumber;
            cout << endl;
        }
        else if (choice == 7) { // Deletes an access point

        }
        else if (choice == 8) { // Changes access point status

        }
        else if (choice == 9) { // Change access point date

        }
        else if (choice == 0) { // Exits
            exitCheck = 2;
        }
        else {
            cout<< "Input Not Valid. Please Provide A Valid Input";
        }
}
    // Display the Purpose and that Program has now ended
    cout << "\nThe Purpose Of This Program Is To Create A Building Network " << endl;
    cout << "Access Operation That Uses Vectors, Clas Callings, And Loops" << endl;
    cout << "\nThe Program Has Now Ended\n" << endl;
    return 0; 
}// End Program

void fillFloor(vector<Floor>& newBuilding) {
        floorID++;
        cout << "----------------------"<< endl;
        cout << "Floor Numer: "<< floorID << endl;
        cout << "----------------------"<< endl;

        Floor newFloor (floorID, roomNumber); // This istantiated the object in vector, will be passing floorID and roomNumber to the new object
        newBuilding.push_back(newFloor); // adds a new object to the newBuilding vector
        cout << endl;

}

// Deletes Floor
void deletesFloor(vector<Floor>& newBuilding) {
        int deletedFloor;   
        cout << "Enter Floor Number To Delete It"<< endl;
        cin >> deletedFloor;
        cout << "Floor Numer: "<< deletedFloor << " Has Been Deleted!" << endl;
        deletedFloor = deletedFloor - 1;
        newBuilding.erase(newBuilding.begin()+deletedFloor);

}

// Replace Floor Info
void replaceFloor(vector<Floor>& newBuilding) {
        for (unsigned int i = 0; i < newBuilding.size(); i++) {
            if (newBuilding[i].getFloorID() == floorForRoomAdd) {
                int changeRoomTotal = newBuilding[i].getRoomNumber;
                newBuilding[i].getRoomNumber = changeRoomTotal + 1;
            }
        }

}
// Insert New Room
void insertRoom(vector<Room>& newFloor) {
            roomID++;
            cout << "What Floor Will This Room Be Added In?"<< endl;
            cout << "Room Numer: "<< roomID << endl;
            cout << "----------------------"<< endl;

            Room newRoom (roomID, accessPointNumber); // This istantiated the object in vector, will be passing floorID and roomNumber to the new object
            newFloor.insert(newFloor.begin()+3, newRoom);
            cout << endl;
}

void fillRoom(vector<Room>& newFloor) {
            roomID++;
            cout << "----------------------"<< endl;
            cout << "Room Numer: "<< roomID << endl;
            cout << "----------------------"<< endl;

            Room newRoom (roomID, accessPointNumber); // This istantiated the object in vector, will be passing floorID and roomNumber to the new object
            newFloor.push_back(newRoom); // adds a new object to the newFloor vector
            cout << endl;
}
// Deletes Room
void deletesRoom(vector<Room>& newFloor) {
        int deletedRoom;    
        cout << "Enter Room Number To Delete It"<< endl;
        cin >> deletedRoom;
        cout << "Room Number: "<< deletedRoom << " Has Been Deleted!" << endl;
        deletedRoom = deletedRoom - 1;
        newFloor.erase(newFloor.begin()+deletedRoom);

}

void fillAccessPoint(vector<AccessPoint>& newRoom) {
    bool accessPointStatus;

                accessPointID++;
                cout << "----------------------"<< endl;
                cout << "Access Point Number: "<< accessPointID << endl;
                cout << "----------------------"<< endl;
                cout << "What Is The Status Of This Access Point : ";   
                cin >> accessPointStatus;

                AccessPoint newAccessPoint (accessPointID, accessPointStatus); // This istantiated the object in vector, will be passing floorID and roomNumber to the new object
                newRoom.push_back(newAccessPoint); // adds a new object to the newFloor vector
                cout << endl;       
}

// This will display all of the building information
void printFloorVector(const vector<Floor>& newBuilding) {

    cout << "The Building Has: " << floorTotal << " Floors" << endl;
    unsigned int newBuildingSize = newBuilding.size();

    for(unsigned int i = 0; i < newBuildingSize; i++) {
        cout << "Floor ID: " << newBuilding[i].getFloorID() << endl;
        cout << "Total Rooms: " << newBuilding[i].getRoomNumber() << endl;
    }   
}

void printRoomVector(const vector<Room>& newFloor) {    
    unsigned int newFloorSize = newFloor.size();

    for(unsigned int a = 0; a < newFloorSize; a++) {
            cout << "Room ID: " << newFloor[a].getRoomID() << endl;
            cout << "Total Access Points: " << newFloor[a].getAccessPointNumber() << endl;          
    }
}

void printAccessPointVector(const vector<AccessPoint>& newRoom) {
    unsigned int newRoomSize = newRoom.size();

    for(unsigned int b = 0; b < newRoomSize; b++) {
        cout << "Access Point Number: " << newRoom[b].getAccessPointID() << endl;
        cout << "Access Point Status: " << newRoom[b].getAccessPointStatus() << endl;   
        cout << endl;
    }
}

1 个答案:

答案 0 :(得分:0)

据我了解,您希望修复replaceFloor功能,以便您可以为特定楼层再添加一个房间。

在代码中,您可以使用以下行正确识别建筑物矢量中楼面元素的位置:if (newBuilding[i].getFloorID() == floorForRoomAdd)

除了您已完成的步骤之外,您还需要将房间实际添加到地板矢量中。为此,您可以访问楼层向量newBuilding[i],然后调用newBuilding[i].push_back(newRoomObject),其中newRoomObject包含您为要添加的新楼层指定的所有信息。这将导致房间被添加到地板矢量中。

如果我以任何方式误解了您的查询,请提及,以便我可以更新我的答案。我有一个查询但由于缺乏声誉而无法发表评论,为什么你在插入室功能中使用了newFloor.insert(newFloor.begin()+3, newRoom);。我可以看到+3常数的唯一原因是你可能会认为你说前两个位置是由floorID和房间号占用的。即使在那种情况下,也应该是+2。此外,即便如此,floorID和room no很可能是两个整数(或一些数字),而房间是对象。您不能在数组中混合使用这些类型。请澄清。