Like Tree1Likes
  • 1 Post By kmdv

vectors and C++11

This is a discussion on vectors and C++11 within the C++ Programming forums, part of the General Programming Boards category; What the heck I thought I could use the new for statement like this. In my book it says it ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    486

    vectors and C++11

    What the heck I thought I could use the new for statement like this. In my book it says it works but it fails for me. What am I missing?

    Code:
    #include <iostream>
    #include <vector>
    #include <stdexcept>
    #include <iomanip>
    
    using namespace std;
    
    void inputVector(const vector<int> &array)
    {
        for( int &item : items) //ERROR undeclared "items"
            cin>>item;
        
    }
    void outputVector(vector<int> &array)
    {
        for(int item : items)  //ERROR undeclared "items"
            cout<< item <<" ";
        cout << endl;
    
    }
    int main(int argc, const char * argv[])
    {
    
        vector<int> integers1(4);
        vector<int> integer2(5);
        
        cout<<"Enter some numbers"<<integers1.size() <<endl;
        
        inputVector(integers1);
        inputVector(integer2);
        
        outputVector(integers1);
        outputVector(integer2);
        
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Isn't the error message clear enough? There is no variable named "items" (there is only one named "array"). You have also another error in your code, regarding vectors' const-qualifiers.
    King Mir likes this.
    I never put signature, but I decided to make an exception.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    maybe it is because your vector is called array and not items?

    and how do you plan to modify const vector?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    486
    Ok i moved the const to the output, I knew better then that.

    To be honest this code is completely honest this code is pretty much plagiarized. I like to take the code from my textbook and play with it. I get a better understanding of what its really doing. Thats why I am confused. I know I didn't declare items anywhere but my book didn't either. I'll go back and stare at it some more.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    486
    Works... Not sure why the textbook said otherwise. Thanks for the help!!!

    Code:
     #include <iostream>
    #include <vector>
    #include <stdexcept>
    #include <iomanip>
    
    using namespace std;
    
    void inputVector(vector<int> &array)
    {
        for( int &item : array)
            cin>>item;
        
    }
    void outputVector(const vector<int> &array)
    {
        for(int item : array)
            cout<< item <<" ";
        cout << endl;
    
    }
    int main(int argc, const char * argv[])
    {
    
        vector<int> integers1(4);
        vector<int> integer2(5);
        
        cout<<"Enter some numbers "<<integers1.size() <<endl;
        
        inputVector(integers1);
        inputVector(integer2);
        
        outputVector(integers1);
        outputVector(integer2);
        
        
        return 0;
    }

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    A non-vector comment.
    Couldn't the message for getting the user to input be something like this:
    Code:
    cout << "Enter " << integers1.size() << " numbers" << endl;
    Note also that you could use '\n' instead of std::endl. (std::endl vs “\n”)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors in vectors - pointers and iterators
    By fisherking in forum C++ Programming
    Replies: 8
    Last Post: 07-27-2010, 10:34 AM
  2. About vectors.
    By KgNe in forum C++ Programming
    Replies: 23
    Last Post: 09-03-2008, 11:33 PM
  3. Vectors
    By Drake in forum Game Programming
    Replies: 8
    Last Post: 01-29-2006, 03:40 PM
  4. how to use vectors in VC++6.0??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-13-2004, 08:08 AM
  5. Vectors
    By Korn1699 in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2004, 03:29 AM

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