array of objects

This is a discussion on array of objects within the C++ Programming forums, part of the General Programming Boards category; Sleep is for reading the output. I already practised a little bit: Code: #include <iostream> #include <vector> using namespace std; ...

  1. #16
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Sleep is for reading the output. I already practised a little bit:

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    void pause()
    {
      cin.clear();
      cin.ignore(1000, '\n');
      cin.get();
    }
    
    void passvector(vector<int> ints)
    {
      cout << "Via function passed:\n";
      for(int i = 0; i < ints.size(); i++)
      {
        cout << ints[i] << endl;
      }
    }
    
    vector<int> returnvector()
    {
      vector<int> result;
      
      result.push_back(1);
      result.push_back(2);
      result.push_back(3);
      
      return result;
    }
    
    int main()
    { 
      int q;
      int val;
      
      cout << "Number of ints: ";
      while(!(cin >> q))
      {
        cin.clear();
        cin.ignore(1000, '\n');
        cout << "Wrong input, please re-enter: ";
      }
        
      vector<int> integers;
      
      for(int i = 0; i < q; i++)
      {
        cout << "Integer value " << i << ": ";
        while(!(cin >> val))
        {
          cin.clear();
          cin.ignore(1000, '\n');
          cout << "Wrong input, please re-enter: ";
        }
        integers.push_back(val);
      }
      
      for(int i = 0; i < q; i++)
      {
        cout << integers[i] << endl;
      }
      passvector(integers);
      
      vector<int> result = returnvector();
      cout << "Via function returned:\n";
      for(int i = 0; i < result.size(); i++)
      {
        cout << result[i] << endl;
      }
      
      pause();
      
      return 0;
    }
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #17
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I fixed the problem! It was in the private part of the class declaration. The pointers acted strange.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Adding objects to an array
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 08:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21