c++ - Inserting an element into multi dimension vector errors -
i'm writing program i'm creating lab multidimensional vectors. far have constructor asks user input number , sizes of lab. in program i'm creating function called addlab add new lab @ specified position number of computers inputted user.
while testing program make user input number of sizes of lab 3 tried adding lab 5 vector following output doesn't compile correctly:
labs: computer stations: 1: 1:empty 2:empty 3:empty 2: 1:empty 2:empty 3:empty 3: 1:empty 2:empty 3:empty want lab inserted at?
here header file
#ifndef __grade_weight_calculator__computerlabs__ #define __grade_weight_calculator__computerlabs__ #include <iostream> #include <string> #include <vector> using namespace std; class computerlabs{ public: computerlabs(); void show_labs(); void add_lab(); private: vector<vector<string>> labs; int numoflab; int numofcomp; int lab_numba; int comp_numba; }; #endif
here implementation cpp file
#include "computerlabs.h" computerlabs::computerlabs() { cout<<"input number of labs"<<endl; cin>>numoflab; cout<<"input size of labs"<<endl; cin>>numofcomp; (int i=0;i<numoflab; i++){ vector<string> row; (int j=0;j<numofcomp; j++) row.push_back("empty"); labs.push_back(row); } } void computerlabs::show_labs() { cout<<"labs: "<<"computer stations:"<<endl; int a=0; int j; (int i=0;i<numoflab; i++){ cout<<a+1<<": "; j=0; while(j<numofcomp){ cout<<" "; cout<<j+1<<":"<<labs[i][j]; ++j; } a++; cout<<endl; } } void computerlabs::add_lab() { cout<<"where want lab inserted at?"<<endl; numoflab=5;//this makes add lab 5 vector<string> row; for(int i=0;i<numofcomp;i++) { row.push_back("empty"); } labs.insert(labs.begin()+4,row); }
and here main.cpp file:
#include "computerlabs.h" int main() { computerlabs mycomp; mycomp.show_labs(); mycomp.add_lab(); mycomp.show_labs(); }
i think error might causing relates show_labs() function in terms of algorithm tried using im not entirely sure. can me figure out issues , solution fixing it?
the main problem in code in add_lab()
function.
void computerlabs::add_lab() { cout<<"where want lab inserted at?"<<endl; // problem line // not sure why needed that. // when initial value of numoflab 3, // numoflabs can changed 4 not 5. if set // 5 here, run problem in show_lab() since // there aren't 5 rows in 2d vector. numoflab=5;//this makes add lab 5 vector<string> row; for(int i=0;i<numofcomp;i++) { row.push_back("empty"); } labs.insert(labs.begin()+4,row); }
change function to:
void computerlabs::add_lab() { cout<<"where want lab inserted at?"<<endl; int pos; cin >> pos; // make sure input position valid. if ( pos > numoflab ) { cout << "invalid position." << endl; return; } // increment numoflab 1 ++numoflab; std::vector<std::string> temprow; for(int i=0;i<numofcomp;i++) { temprow.push_back("empty"); } labs.insert(labs.begin()+pos, temprow); }
general improvements
you don't need store numoflab
in class member variable. can derived labs
.
i see have updated code row
not member variable of class more.
as matter of design, it's better provide class data rather expecting class data stdin
or cin
.
here's updated version of program.
#include <iostream> #include <string> #include <vector> using namespace std; class computerlabs { public: computerlabs(int numoflab, int numofcomp); void show_labs(); void add_lab(int pos); private: vector<vector<string>> labs; int numofcomp; int lab_numba; int comp_numba; }; computerlabs::computerlabs(int numoflab, int numofcomp) : numofcomp(numofcomp) { (int = 0; < numoflab; i++){ std::vector<std::string> row; (int j = 0; j < numofcomp; j++) { row.push_back("empty"); } labs.push_back(row); } } void computerlabs::show_labs() { cout << "labs: " << "computer stations:" << endl; int numoflab = labs.size(); (int = 0; i<numoflab; i++){ cout << i+1 << ": "; (int j = 0; j < numofcomp; j++) { cout<<" "; cout << j+1 << ":" << labs[i][j]; } cout<<endl; } } void computerlabs::add_lab(int pos) { int numoflab = labs.size(); if ( pos < 0 || pos > numoflab ) { cout << "invalid position." << endl; return; } std::vector<std::string> row; for(int i=0;i<numofcomp;i++) { row.push_back("empty"); } labs.insert(labs.begin()+pos,row); } int main() { int numoflab; int numofcomp; cout << "input number of labs" << endl; cin >> numoflab; cout << "input size of labs" << endl; cin >> numofcomp; if (!cin ) { // deal error. } computerlabs mycomp(numoflab, numofcomp); mycomp.show_labs(); cout << "where want lab inserted at?" << endl; int pos; cin >> pos; if (!cin ) { // deal error. } mycomp.add_lab(pos); mycomp.show_labs(); }
update
if sizes of newly added labs different sizes of existing labs, can following:
- remove
numofcomp
member variable. - add argument
add_lab
,numofcomp
. - before calling
add_lab
, ask user number of components of lab.
#include <iostream> #include <string> #include <vector> using namespace std; class computerlabs { public: computerlabs(int numoflab, int numofcomp); void show_labs(); void add_lab(int pos, int numofcomp); private: vector<vector<string>> labs; }; computerlabs::computerlabs(int numoflab, int numofcomp) { (int = 0; < numoflab; i++){ std::vector<std::string> row; (int j = 0; j < numofcomp; j++) { row.push_back("empty"); } labs.push_back(row); } } void computerlabs::show_labs() { cout << "labs: " << "computer stations:" << endl; int numoflab = labs.size(); (int = 0; i<numoflab; i++){ cout << i+1 << ": "; int numofcomp = labs[i].size(); (int j = 0; j < numofcomp; j++) { cout<<" "; cout << j+1 << ":" << labs[i][j]; } cout<<endl; } } void computerlabs::add_lab(int pos, int numofcomp) { int numoflab = labs.size(); if ( pos < 0 || pos > numoflab ) { cout << "invalid position." << endl; return; } std::vector<std::string> row; for(int i=0;i<numofcomp;i++) { row.push_back("empty"); } labs.insert(labs.begin()+pos,row); } int main() { int numoflab; int numofcomp; cout << "input number of labs" << endl; cin >> numoflab; cout << "input size of labs" << endl; cin >> numofcomp; if (!cin ) { // deal error. } computerlabs mycomp(numoflab, numofcomp); mycomp.show_labs(); cout << "where want lab inserted at?" << endl; int pos; cin >> pos; cout << "input size of lab" << endl; cin >> numofcomp; if (!cin ) { // deal error. } mycomp.add_lab(pos, numofcomp); mycomp.show_labs(); }
Comments
Post a Comment