0

Hi..

I am trying doing this exercise.Which is require a program that could ask the user how many integer he/she want to enter,and allow him/her entering them thus lastly display them all back.

i could just simply doing it by using an array,but this time i wanna use a vector to this,but it seems that my codes did not work.So i hope somebody could correct it for me..
This is what i have done :

int main() {

vector<double> data;

int count;
cout<<"How many numbers you want to enter? "<<endl;
cin>>count;
cout<<"Please enter values : "<<endl;
double input;

for(int i=0; i<count; i++)
{
  data.push_back(input);
    }

    cout<<endl;
for(int i=0; i<data.size(); i++)
{
  cout<<data[i]<<" ";
    }

system ("pause");
return 0;

and one more thing,could somebody explain more about differences using .push_back and cin>> in vector??

TQ :)

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Narue
0
for(int i=0; i<count; i++)
{
    data.push_back(input);
}

This is your input loop, but there's no input going on. You need to give the user time to input a value, then add the value to your vector:

for (int i = 0; i < count; i++)
{
    if (cin>> input)
        data.push_back(input);
}

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.