user1995298
user1995298

Reputation: 13

C++ - Gettings size from vector in FOR loop

I have a loop that asks for user input, and adds it too a vector, then when if they type "EXIT" it will stop and display the list. What I am trying to do now is determine the number of elements with size()

This is what I have:

#include <iostream>
#include <string>
#include <unistd.h>
#include <iterator>
#include <vector>
using namespace std;

int main()
{
    write(1,"\E[H\E[2J",7);                                  
    vector<string> list;
    cout << "Enter UIDs: \n\n"; 
    for(string uid ; cin >> uid && uid != "EXIT"; list.push_back(uid))
        cout << " \n";
    copy(list.begin(), list.end(), ostream_iterator<string>(cout, "\n\n"));
    cout << "Vector size: " << uid.size() << endl;
    return 0;
}

When attempting to compile that I get the error:

g++ sof.cpp -o sof

sof.cpp: In function ‘int main()’:
sof.cpp:16:32: error: name lookup of ‘uid’ changed for ISO ‘for’ scoping
sof.cpp:13:16: error: cannot use obsolete binding at ‘uid’ because it has a destructor

Upvotes: 1

Views: 133

Answers (1)

v154c1
v154c1

Reputation: 1698

You're querying uid.size() instead of list.size()

Upvotes: 4

Related Questions