How to receive multiple values on the same line?

Asked

Viewed 7,739 times

1

How can I receive multiple values on the same line? For example, the user enters 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 in the same line? Then I will have to work with them.

Problem: http://olimpiada.ic.unicamp.br/pratique/programacao/nivelj/2008f2pj_acoes

  • 1

    Have you ever tried to do anything? What is your difficulty ? Put what you have managed to do so far. ;)

  • I tried to work with strings, but I had trouble with the sign " - " .

  • I tried to remove the values from the string.

  • cin >> umaVariavel reads a piece of data (separating by blank spaces). You can, for example: 1. read the amount of elements for a variable (e. g, n); 2. Create a vector of the variable size you read in the first line (e. g, vetor[n]); 3. Read each line element inside a loop. As per Vitor’s comment, it’s much easier to help you if you edit the question including an example code.

2 answers

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 making data entry always makes consistency of read values, then a slightly more realistic example would be this 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.

0


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

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

That stretch:

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

Do the reading 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 on Ideone.

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

  • The problem is that I can receive 50/100 values.

  • You want an algorithm that you pass the vector size?

  • I need an algorithm that takes n numbers (n>=4) on the same line, being n defined by the user. Then I need to "work" with these numbers. How can I receive these n numbers on the same line, so that later I can "work" with them?

  • I get it, I’ll edit the answer

  • 6 (Amount of numbers that will be informed by the user) 1 2 3 4 -5 -6 (numbers informed by the user) ?

  • @Gabrielpastori, this is an example of reading and writing for you to have a basis.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.