I want to keep inputting data into vector<int> until it's full.
My code is below but I have to input an extra int before the while () loop terminates with the right size vector and the program moves on to the next steps. I've tried cin.ignore() as shown to remove the newline character but can't seem to make it work. Any helps always appreciated, thanks.
PS: I understand there are several other ways of initializing the vector (for e.g. auto-initializing this small size vector or a do ... while loop that seems to work) but I'm focusing on getting my use of cin right here. Thanks
Code:
#include<iostream>
#include<vector>
using namespace std;
constexpr auto QUARTERS = 4;
int main(){
vector<int> v ;
int temp {0};
cout<<"Please enter the sales numbers"<<endl;
while ((cin>>temp) && (v.size() < QUARTERS)){
v.push_back(temp);
// cin.clear();
cin.ignore(1000,'\n');
}
cout<<"The size of the vector is: "<<v.size()<<endl;
cout<<"The elements of the vector are: "<<endl;
for(auto &i : v){
cout<<i<<endl;
}
}