-3
#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int>::const_iterator iter;
    int limit, input;
    vector<int> v;
    cout << " Enter the limit : ";
    cin >> limit;
    cout << "Enter the numbers for array :\n ";
    for (int i = 0; i < limit; i++)
    {
        cin >> input;
    }
    v.push_back(input);

    cout << "Sorting the array :\n";
    sort(v.begin(), v.end());
    cout << "Array numbers :\n";
    /* for (iter = v.begin(); iter != v.end(); ++iter)
    cout << *iter << endl;*/

    for (int i = 0; i < limit; i++)
    {
        cout << v[i] << " ";
    }
}

I want the vector array input from user and to sort and display it and also provide the comment from which i can understand the problem and will not make the mistakes twice

Thank You

trincot
  • 317,000
  • 35
  • 244
  • 286
  • 5
    hi! The first line of your code says you are learning from a bad source (sorry!), so maybe pick a more didactically well-structured text. Anyways. You don't say what exactly goes wrong, and what you have tried to understand and solve the problem yourself. – Marcus Müller Nov 08 '22 at 10:09
  • 1
    Take some time to explain to your [rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) how many elements you add to your vector. – Some programmer dude Nov 08 '22 at 10:09
  • What does this have to do with `dsa`??? Anyway this is trivial: your `v.push_back(input);` should be **inside** the loop. Now you are only pushing one value. Voting to close. – trincot Nov 08 '22 at 10:14
  • 1
    See [Why should I not #include ?](https://stackoverflow.com/q/31816095) and [Why using namespace std is bad practice](https://stackoverflow.com/questions/1452721). – prapin Nov 08 '22 at 10:22

1 Answers1

1

The big problem is here

for (int i = 0; i < limit; i++)
{
    cin >> input;
}
v.push_back(input);

You want to add all the numbers that are input, but this code only adds one number because you have push_back after the loop not in the loop.

This is how you should do it

for (int i = 0; i < limit; i++)
{
    cin >> input;
    v.push_back(input);
}
john
  • 85,011
  • 4
  • 57
  • 81