By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,959 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,959 IT Pros & Developers. It's quick & easy.

Vector Problem

P: 55
Hello all C++ expert programmer, i fairly new to C++ programming.

I have designed a matrix class which uses vector to store the data.

My problem is i want store the data into vector until user is enough input.

what i have did so far.

Below is my program:
Expand|Select|Wrap|Line Numbers
  1.  // Header fiel for matrix
  2.  
  3. #pragma once
  4.  
  5. #include<vector>
  6.  
  7. using std::vector;
  8.  
  9. class matrix
  10. {
  11.     vector<vector<int>> vec;
  12.     // Declare vector of vector
  13. public:
  14.     matrix() : vec(0){}
  15.     matrix(vector<vector<int>> vec)
  16.         :vec(vec){}
  17.  
  18.     void setMatrix();
  19.     matrix getMatrix();
  20.  
  21.     ~matrix(){}
  22. };
  23.  
  24.  
Implementation File :

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include<iostream>
  4. #include<vector>
  5. #include "Matrix-1.h"
  6.  
  7. using std::cout;
  8. using std::cin;
  9. using std::vector;
  10. using std::iterator;
  11.  
  12. int main(int agrc, char *argv[])
  13. {
  14.     matrix amatrix;
  15.  
  16.     amatrix.setMatrix();
  17.     return 0;
  18. }
  19.  
  20. void matrix::setMatrix()
  21. {
  22.     int data, row_loop = 0, col_loop = 0;
  23.     int column;
  24.  
  25.     do
  26.     {
  27.         cout << "Enter row " << row_loop << " column : ";
  28.         cin >> column;
  29.         vec.push_back(vector<int>(column));
  30.  
  31.         while(col_loop < column)
  32.         {
  33.             cout << "Enter Matrix " << "[" << row_loop << "]" << "[" << col_loop << "]" << " : ";
  34.             cin >> data;
  35.             vec[col_loop].push_back(data);
  36.             col_loop++;
  37.         }
  38.         row_loop++;
  39.     }while(data != -1);
  40. }
  41.  
I have a loop problem here. I really run out of idea.

Thanks for your opinion and advise.

I greatly appreciated for any help.
Jun 12 '07 #1
Share this Question
Share on Google+
1 Reply


P: 7
I didn't excute the program, just had glance. I think the problem what you are facing is not making col_loop as 0
after or before doing row_loop++ in while loop just do:
col_loop = 0;
I think this will work. If not then please get back.
Expand|Select|Wrap|Line Numbers
  1. do
  2.     {
  3.         cout << "Enter row " << row_loop << " column : ";
  4.         cin >> column;
  5.         vec.push_back(vector<int>(column));
  6.  
  7.         while(col_loop < column)
  8.         {
  9.             cout << "Enter Matrix " << "[" << row_loop << "]" << "[" << col_loop << "]" << " : ";
  10.             cin >> data;
  11.             vec[col_loop].push_back(data);
  12.             col_loop++;
  13.         }
  14.         row_loop++;
  15.         col_loop = 0;        //OVER HERE
  16.     }while(data != -1);
  17.  
Jun 12 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.