dimanche 21 novembre 2021

How to read a line in C++ [duplicate]

please help me to read multiple integers from a line using multiple scanf in C++. Is it possible, or should I use a different function?

I am a novice in programming, so a detailed answer is appreciated.

I tried and somehow discovered that:

scanf_s("%d ", &a);
scanf_s("%d ", &b);

if I input to this code line1: 3 5 The code can read both of them.

Please help me resolve the following code.

vector<int> my_vec;
int size;
scanf("%d ", &size);
for(int k = 0; k<size; k++)
{ int elements;
    my_vec[k] = scanf("%d ", &elements);
}

I am trying to read a line where the first number is the size of the vector and the following integers are the elements of the vector.

For example, line: 4 7 9 3 6 it should store vector my_vec = {7, 9, 3, 6};

When I tried this code on visual studio, it just terminated when run.

Aucun commentaire:

Enregistrer un commentaire