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

Changing a Second Value withing a Vector Object in C++

本文关键字:对象 第二个 C++      更新时间:2023-10-16

我对C++很陌生,正在学习向量和所有有趣的东西。我正在尝试弄清楚如何让用户在他选择的楼层添加一个房间。我已经完成了大部分问题,但只是我很难弄清楚的那部分,任何帮助将不胜感激!我认为主要问题是功能ReplaceFloor,但我不知道如何以用户选择选择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 Endedn" << 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;
    }
}

据我了解,您想修复 replaceFloor 函数,以便您可以为特定楼层再添加一个房间。

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

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

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