Fill two vectors of unknow lenght

Hi at everyone, I have a quite stupid question.
I should fill two vectors of unknow lenght, my attempt is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    vector<double> price;

    cout << "Fill the vector price, when you want insert  informations inside another vector enter stop" << endl;

    for(double objectprice; cin>>objectprice;)
        price.push_back(objectprice);

    cout << "Fill the vector weight, when you want end it, enter stop" << endl;

    vector<double> weight;
    for( double objectweight; cin>>objectweight;)
        weight.push_back(objectweight);

    return 0;
}


But when I enter a string or a char the program shutdown instead of let me enter the elements in the second vector.
What should I change??
Thak to everyone.
Looks like you dont quite know how for-loops work, Check out buckys tutorial on youtube - https://www.youtube.com/playlist?list=PLAE85DE8440AA6B83

He is great at teaching basics. If you specifically want to learn about for-loops watch video number 22.
After you enter "stop" and condition fails, offending input is still in input buffer. You need to clear it and reset error flag:
1
2
3
4
5
6
std::cin.clear();
//Then either:
std::string dump;
std::cin >> dump; //extracts one word
//or
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //include <limits> 


Hi! Thank you for the fast reply. I saw your videos but it doesn't help me.
I mean, the video shows the implementation of a for loop. Now this doesn't help me because i can't set the lenght of the vector in advance. What I'm searching is code that push back elemnts inside a vector and stops only when i enter a string or a char -or a particular key-.
My code works only for the first vector...
Do you have any advice?
Thank you anyway again.
Another way is to read input as string, and if it not an specific key to stop, parse it as number using std::stod() or similar
MiiNiPaa Thank you for the help it works perfectly. Thank you again!!!
Topic archived. No new replies allowed.