473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Vector Problem

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
1 1168
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

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

Similar topics

7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
13
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================...
11
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any...
13
by: Steve | last post by:
I have defined the following private object: std::vector<Banana> bananas; in my header file. I have also added a method called FillVector(), which sets the size of the vector and fills it with...
18
by: imutate | last post by:
I have an integer variable and I am testing it as follows #define NULL_VAL -1 ... if (x.id != NULL_VAL) { std::cout << x.id << std::endl; ... }
0
by: acosgaya | last post by:
hi, I am working in this problem, where I have a set of N d-dimensional points, e.g. (4,5,6,8) (2,0,4,6), are 4-d points, which I have stored in a vector of vectors. I am trying to partition...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
4
by: shuisheng | last post by:
Dear All, I have a question. Assume #include <vector> using namespace std; class A { private:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.