【发布时间】:2014-12-13 04:24:14
【问题描述】:
我是 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 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;
}
}
【问题讨论】:
-
在这一行中,为什么您将输入为“cin >> roomNumber;”。我认为你需要打印你的房间号而不是这个。
标签: c++ class pointers object vector