How to receive multiple values on the same line?

1

How can I receive multiple values on the same row? For example, the user types the number of values and then the values:

  

5 (how many values, greater than or equal to 4)

     

-5 1 -3 5 7 (separated by blanks)

How can I receive these values on the same line? Then I'll have to work with them.

Problem: link

    
asked by anonymous 16.12.2016 / 17:41

2 answers

0

What you want is called dynamic memory allocation, that is, the user types the desired number and the algorithm allocates the space needed for that vector in memory.

In code vetor = new int[num]; is your vector. The num will be allocated according to the entered entry.

This excerpt:

 cout << "Digite os elementos do vetor: "; 
 for(i=0;i<num;i++) 
 {     
     cin >> vetor[i]; 
 }

Read what you need.

Sample code:

#include <iostream>
using namespace std;

int main()
{
    int num; 
    int *vetor = NULL; // vetor que será alocado o espaço

    cout << "Digite o tamanho do vetor: \n";
    cin >> num;

    vetor = new int[num];

    int i;

    cout << "Digite os elementos do vetor: "; 
    for(i=0;i<num;i++){
        cin >> vetor[i]; 
    }

    cout << endl;
    cout << "Digite os números " << i << ": "; 

    for(i=0;i<num;i++) {
       cout << vetor[i] << endl;
    }

    delete [] vetor;  // Libera o espaço alocado para o vetor
    return 0; 
}

See working at Ideone .

Remember that it's important to use delete [] vetor; to free up memory.

    
16.12.2016 / 17:56
0

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.

    
17.12.2016 / 16:32