A slightly more idiomatic solution avoiding using new / delete and native arrays.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int vecSize;
cout << "Digite o tamanho do vetor: ";
cin >> vecSize;
vector<int> vetor;
cout << "Digite os elementos do vetor: ";
for (int i = 0; i < vecSize; i++)
{
int val;
cin >> val;
vetor.push_back(val);
}
cout << endl;
cout << "Números digitados (" << vetor.size() << "):\n";
for (int num : vetor)
{
cout << num << endl;
}
}
In practice, when data is input, consistency is always made of the read values, so a slightly more realistic example would be the one below:
#include <iostream>
#include <vector>
using namespace std;
enum { MAX_VEC_SIZE = 100 };
int main()
{
int vecSize;
// -------
// leitura do tamanho do vetor
cout << "\nDigite o tamanho do vetor: ";
if (!(cin >> vecSize))
{
cerr << "erro na leitura do tamanho do vetor\n";
exit(1);
}
if (vecSize < 1)
{
cerr << "erro, tamanho do vetor precisa ser maior que zero\n";
exit(1);
}
if (vecSize > MAX_VEC_SIZE)
{
cerr << "erro, tamanho do vetor deve ser menor que " << (MAX_VEC_SIZE+1) << "\n";
exit(1);
}
// -------
// leitura dos elementos do vetor
vector<int> vetor;
cout << "\nDigite os elementos do vetor:\n";
for (int i = 0; i < vecSize; i++)
{
int val;
if (!(cin >> val))
{
cerr << "erro na leitura do elemento " << i << " do vetor\n";
exit(1);
}
vetor.push_back(val);
}
// -------
// exibicao dos elementos do vetor
cout << "\nElementos do vetor (" << vetor.size() << "):\n";
for (int num : vetor)
{
cout << num << endl;
}
// -------
}
That's all for now folks.